抽象和接口
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace is和as { class Program { static void Main(string[] args) { //is是来判断值是否是指定类型的,返回的是布尔值 double i = 3; if (i is int) { Console.WriteLine("i是int类型的。"); } else { Console.WriteLine("i不是int类型的。"); } object o = 123;//object是所有类型的父类 string s = o as string;// as 代表的是尝试去做类型转换,如果转换成功,返回正确的结果,如果尝试不成功,则返回null值。 Console.WriteLine("s得值是" + s); object b = "abc"; string s2 = b as string;//尝试类型转换成功的情况,会返回正确的值 Console.WriteLine("s2的值为:"+s2); //程序等待 Console.ReadLine(); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 抽象类 { class Program { //包含抽象方法的类一定要定义为抽象类 //抽象类可以有普通方法 //抽象类可以不包含抽象方法(但是不建议这么做) //抽象类不能实例化 //抽象方法必须继承,每个继承的子类都要实现所有抽象方法 //抽象类 abstract class Animal { //抽象方法 abstract public void cry(); abstract public void eat(); public void cr() { Console.WriteLine("这是一个正常的方法"); } } class Dog:Animal { public override void cry() { Console.WriteLine("小狗旺旺叫!"); } public override void eat() { Console.WriteLine("小狗吃饭!"); } } class Cat : Animal { public override void cry() { Console.WriteLine("小猫喵喵叫!"); } public override void eat() { Console.WriteLine("小猫吃饭!"); } } static void Main(string[] args) { Animal a = new Dog(); a.cry(); Animal b = new Cat(); b.cry(); Dog c = new Dog(); c.cry(); Cat d = new Cat(); d.cry(); //程序等待 Console.ReadLine(); } //------------------------------------------------------------------- } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 抽象类与接口_19_5_14 { class Program { //生肖的问题--根据年份得到今年的生肖 static public string calxs(int year) { int i = year % 12; string[] name = { "猴", "鸡", "狗", "猪", "鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊" }; return name[i]; } static void Main(string[] args) { Console.WriteLine("请输入年月:"); int yaer = int.Parse(Console.ReadLine()); Console.WriteLine("{0}年的生肖为:{1}", yaer, calxs(yaer)); //程序等待 Console.ReadLine(); Animal a = new Dog(); a.cry(); Animal b = new Cat(); b.cry(); Dog c = new Dog(); c.cry(); Cat d = new Cat(); d.cry(); //程序等待 Console.ReadLine(); ////is是来判断值是否是制定类型的,返回的是布尔值 //int i = 3; //if (i is int) //{ // Console.WriteLine("i是int类型的。"); //} //else //{ // Console.WriteLine("i不是int类型的。"); //} //object o = 123;//object是所有类型的父类 //string s = o as string;// as 代表的是尝试去做类型转换,如果转换成功,返回正确的结果,如果尝试不成功,则返回null值。 //Console.WriteLine("s得值是" + s); //object b = "abc"; //string s2 = b as string;//尝试类型转换成功的情况,会返回正确的值 //Console.WriteLine("s2的值为:" + s2); ////程序等待 //Console.ReadLine(); } //------------------------------------------------------------------- //抽象类 abstract class Animal { //抽象方法 abstract public void cry(); } class Dog:Animal { public override void cry() { Console.WriteLine("小狗旺旺叫!"); } } class Cat : Animal { public override void cry() { Console.WriteLine("小猫喵喵叫!"); } } //------------------------------------------------------------------- } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 接口 { interface Interface1//必须以“I”开头命名 { //都是没有(不需要)实现的方法,但是不能加抽象关键字,更简单, //public int fangfa1();默认public,但是不能写 int fangfa();//这是一个接口的方法 void fangfa2();//这是一个没返回值的接口方法 } interface Itest { void say(); void fangfa2();//不同接口之间的同名方法 } //基类--普通类 class test { public void study() { Console.WriteLine("好好学习,天天向上"); } } //子类 //允许实现()多个接口 //如果一个类同时继承了父类,又继承了接口,就必须吧继承的 父类 写在 接口 前 class myfangfas : test, Interface1, Itest {//继承了接口的子类 必须 要实现(类似于重写)接口中的所有方法, //实现接口的方法不用override关键字 public int fangfa()//接口Interface1第一个方法 { Console.WriteLine("子类myfangfa实现了接口中的fangfa();方法"); return 6; } public void fangfa2()//接口Interface1第二个方法 { Console.WriteLine("子类myfangfa实现了接口中的fangfa2();方法"); } public void say()//接口Itest第一个方法 { Console.WriteLine("子类myfangfa实现了接口Itest中的say();方法"); } //接口Itest第一个方法 //子类如果实现不同接口中的同名方法,那么要(显式)指定是实现的哪个接口的哪个方法 //此时public无效,要删掉 void Itest.fangfa2() { Console.WriteLine("子类myfangfa实现了接口Itest中的fangfa2();方法"); } void Itest.say() { Console.WriteLine("子类myfangfa实现了接口Itest中的say();方法"); } } class Program { static void Main(string[] args) { /*为了增加可读性,按 * * * * */ myfangfas a = new myfangfas(); int i = a.fangfa(); Console.WriteLine(i); //a.fangfa2();//同名方法可以用同名方法去实现多个同名方法 // Itest b=new //接口不能实例化,但是继承他的子类对象可以赋值给它 Itest b = new myfangfas(); b.say();//只能看到接口里的两个方法 //两个接口的方法可以同名,但是在实例化的时候是同样的方法 Console.ReadKey(); } } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构