当前位置:网站首页>is 和 as的用法
is 和 as的用法
2022-01-15 02:02:52 【罗迪尼亚的熔岩】
using System;
namespace _08_继承
{
class Program
{
static void Main(string[] args)
{
int age = 18;
char gender = '男';
string name = "王五";
double drive_time = 18.8;
string interest = "偷拍";
Driver_1 driv = new Driver_1(name, age, gender, drive_time);
Reporter_ rep = new Reporter_(name, age, gender, interest);
//里氏转换
//1) 子类可以赋值给父类,如果有一个地方需要一个父类做参数,我们可以给一个子类代替
Student_1 s = new Student_1(); //子类
Person p = s;
Person p = new Student_1();
string str = string.Join("|", new string[] {
"1", "2", "3", "4", "5", });
Console.WriteLine(str); //1|2|3|4|5
//2) 如果父类中装的是子类对象,那么可以将这个父类强转为子类对象
if (p is Teacher_1)
{
Teacher_1 ss = (Teacher_1)p; //强制转换 异常
ss.TeacherSayHello();
}
else
{
Console.WriteLine("转换失败");
Console.ReadKey();
}
//is 的用法
if (p is Student_1)
{
Student_1 ss = (Student_1)p; //强制转换
ss.Xuesheng();
}
else
{
Console.WriteLine("转换失败");
Console.ReadKey();
}
Student_1 t = p as Student_1; //p 是 student
Teacher_1 t = p as Teacher_1; //把p父类对象转为teacher;
//as 如果能转换,则返回一个对应的对象,否则返回NUll
// t值为 null
Student_1 t = p as Student_1; //可以成功
Console.ReadKey();
rep.Rep();
driv.CHLSS();
}
}
}
版权声明
本文为[罗迪尼亚的熔岩]所创,转载请带上原文链接,感谢
https://blog.csdn.net/helldoger/article/details/121895339
边栏推荐
- Résumé de fin d'année 2021
- Trois JS rendent des tailles qui ne sont pas des tailles de formulaire de navigateur et sont vérifiées pour résoudre les problèmes
- AndroidAPP启动速度优化;冷启动和热启动解析,2021Android研发必问高级面试题
- 2021.10.10 deduction - stock price fluctuation
- Androidstudio下一个工程生成不同的app,多亏这份《秋招 金九银十-腾讯面试题合集》跳槽薪资翻倍
- Winfo link database addition, deletion, modification and query
- Sqlstatehy0001045access denied for userrootipusing passwords yes
- Sqlstatehy0001045access denied for userroot my ipusing passwords yes
- Using WinDbg to analyze magicodes A wrong writing of IE leads to a sharp increase in memory
- Using WinDbg to analyze magicodes A wrong writing of IE leads to a sharp increase in memory
猜你喜欢
随机推荐
- backtrader和tushare进行量化交易
- 一些开源
- 程序员的基本功:关于JVM
- 程序员不能不懂的一些事:代码片段
- 通过故事的方式尝试和大家说说线程池
- 花5分钟学习TiDB
- Unity3D开发
- How to turn this MySQL statement into an opentsdb Jason statement?
- Watermelon video game station
- 使用MRFT(混合现实功能工具)为 HoloLens 2 创建 Unity 项目
- PyCharm汉化(官方)
- Unity改变图片文本颜色
- After the H3C 5170 switch 802.1x is configured, the win terminal cannot normally issue a security ACL
- Unity change la couleur du texte de l'image
- Pycharm Chinese (Official)
- Créer un projet d'Unit é pour hololens 2 en utilisant mrft (Hybrid Reality Function Tool)
- Unity改變圖片文本顏色
- rds执行sql报错
- 证券公司开户安全吗?股票开户选择哪家证券所
- Jimureport building block report v1 Version 4.2 release, free visual low code report
- JVM - automatic memory management - 2 - garbage collector and memory allocation strategy
- Web page integration calls third-party exe -- writing registry
- Spotmax update: scalable statistics, instance preheating, intelligent storage, providing multiple cost reduction guarantees
- 黑五 圣诞节
- 【TiChoo资讯站】
- 行业分享 | TiChoo数据CEO 陆诗冬展望全球视频电商未来蓝图
- tiktok 数据分析平台
- TikTok数据分析选品平台 - TiChoo数据
- 使用S7.net通信库
- 使用Text.json解析Json文件
- 常用的SQL语句
- 使用多线程写winform程序
- Modbus协议编写与测试
- 使用多线程,Invoke和Action 访问SQL数据库
- Access数据库练习
- 泛型类, 泛型接口的继承, 委托
- 泛型类,泛型接口
- 跨域请求无法携带Cookie的问题
- I think code is a work of art. She's beautiful
- Try the map and slice of the model version in go 1.18