强制转换对象为接口

简介: 可用is运算符检查是否支持接口,用as运算符转换接口,如: 1 /* 2 Example8_4.cs illustrates casting an object 3 to an interface 4  */ 5 6 using System; 7 8 9 ...

可用is运算符检查是否支持接口,用as运算符转换接口,如:

 
 
1 /*
2 Example8_4.cs illustrates casting an object
3 to an interface
4   */
5
6 using System;
7
8
9 // define the IDrivable interface
10 public interface IDrivable
11 {
12
13 // method declarations
14 void Start();
15 void Stop();
16
17 // property declaration
18 bool Started
19 {
20 get ;
21 }
22
23 }
24
25
26 // Car class implements the IDrivable interface
27 public class Car : IDrivable
28 {
29
30 // declare the underlying field used by the Started property
31 private bool started = false ;
32
33 // implement the Start() method
34 public void Start()
35 {
36 Console.WriteLine( " car started " );
37 started = true ;
38 }
39
40 // implement the Stop() method
41 public void Stop()
42 {
43 Console.WriteLine( " car stopped " );
44 started = false ;
45 }
46
47 // implement the Started property
48 public bool Started
49 {
50 get
51 {
52 return started;
53 }
54 }
55
56 }
57
58
59 class Example8_4
60 {
61
62 public static void Main()
63 {
64
65 // create a Car object
66 Car myCar = new Car();
67
68 // use the is operator to check that myCar supports the
69 // IDrivable interface
70 if (myCar is IDrivable)
71 {
72 Console.WriteLine( " myCar supports IDrivable " );
73 }
74
75 // cast the Car object to IDrivable
76 IDrivable myDrivable = (IDrivable) myCar;
77
78 // call myDrivable.Start()
79 Console.WriteLine( " Calling myDrivable.Start() " );
80 myDrivable.Start();
81 Console.WriteLine( " myDrivable.Started = " +
82 myDrivable.Started);
83
84 // call myDrivable.Stop()
85 Console.WriteLine( " Calling myDrivable.Stop() " );
86 myDrivable.Stop();
87 Console.WriteLine( " myDrivable.Started = " +
88 myDrivable.Started);
89
90 // cast the Car object to IDrivable using the as operator
91 IDrivable myDrivable2 = myCar as IDrivable;
92 if (myDrivable2 != null )
93 {
94 Console.WriteLine( " Calling myDrivable2.Start() " );
95 myDrivable2.Start();
96 Console.WriteLine( " Calling myDrivable2.Stop() " );
97 myDrivable2.Stop();
98 Console.WriteLine( " myDrivable2.Started = " +
99 myDrivable2.Started);
100 }
101
102 }
103
104 }
相关文章
|
10月前
通过反射获取方法返回的类型
通过反射获取方法返回的类型
|
存储 编译器 C++
C++中的转换构造函数
在 C/C++ 中,不同的数据类型之间可以相互转换。无需用户指明如何转换的称为自动类型转换(隐式类型转换),需要用户显式地指明如何转换的称为强制类型转换。 自动类型转换示例: int a = 6; a = 7.5 + a; 编译器对 7.5 是作为 double 类型处理的,在求解表达式时,先将 a 转换为 double 类型,然后与 7.5 相加,得到和为 13.5。在向整型变量 a 赋值时,将 13.5 转换为整数 13,然后赋给 a。整个过程中,我们并没有告诉编译器如何去做,编译器使用内置的规则完成数据类型的转换。强制类型转换示例: int n = 100
124 0
|
Java
对象类型转换
对象类型转换
76 0
|
弹性计算 JavaScript 开发工具
对象和接口-1:对象类型
本实验将介绍TypeScript中的对象类的基本语法
|
Java 编译器 C++
常量接口 vs 常量类 vs 枚举区别
把常量定义在接口里与类里都能通过编译,那2者到底有什么区别呢?
106 0
C++转换构造函数以及类型转换函数
🐰转换构造函数 🐰类型转换函数
|
安全 Java 编译器
枚举使用、转数组、实现接口、枚举单例
枚举使用、转数组、实现接口、枚举单例
137 0
|
编译器 程序员 C++
C++中参数需要类型转换,请不要用成员函数
C++中参数需要类型转换,请不要用成员函数
178 0
|
C语言 Android开发 C++
【C++】函数 指针类型参数 与 引用类型参数 对比 ( 修改外部变量需要传入的参数要求 | 参数作返回值 )
【C++】函数 指针类型参数 与 引用类型参数 对比 ( 修改外部变量需要传入的参数要求 | 参数作返回值 )
207 0