C#初识8 多态之接口(interface)
来源:互联网 发布:域名注册好了怎么使用 编辑:程序博客网 时间:2024/06/05 12:41
多态
不同对象收到相同消息时,会产生不同行为,同一个类在不同的场合下表现出不同的行为特征 -->虚方法 vitrual override --->父类能创建对象 父类的函数有意义 -->抽象类 abstract override --->父类不能创建对象 父类的成员抽象函数没有实际意义(其他和普通类似 如添加构造函数 set get属性) -->接口 interface 作用: 把不同的子类对象都当作父类来看,可以屏蔽不同子类对象之间的差异,写出通用的代码,做出通用的编程,以适应需求的不断变化。
接口(interface)
//默认访问修饰符为public,与其他类的默认类型private不同 interface T1 { void Test(); }
<1>指一坨没有实现函数成员的引用类型<2>接口是一个能力、规范<3>接口的好处:多重继承
接口和抽象类的区别
接口注意事项
<1>其与抽象类一样,创建对象也没意义。 <2>实现接口的子类需实现其接口全部成员,除非实现类本身是抽象类 <3>接口中只能有方法、属性、索引器、事件,不能有“字段”和构造函数。接口成员不能有new、static、abstract、override、virtual修饰符。 <4>接口只能继承于接口,而类既可以继承接口,也可以继承类 <5>书写规定:class Test:A,B,T1,因为类是单继承的。 <6>显示实现接口的目的:解决方法的重名问题
测试代码:
接口的使用1
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace _11接口的使用{ class Program { static void Main(string[] args) { //实现多态 IKouLan k1 = new NoTuiPerson();//new Teacher();//new Driver();//new Student(); k1.KouLan(); Console.ReadKey(); } } class Person { public void CHLSS() { Console.WriteLine("人可以吃拉啥谁"); } } // class Student : Person ,NBAPlayer class Student : Person,IKouLan //实现 重写的区别override 可以继承多个接口 { public void KouLan() { Console.WriteLine("学生可以扣篮"); } } interface IKouLan { void KouLan(); } class NoTuiPerson:Person { } //class NoTuiPerson:Person,IKouLan //{ // public void KouLan() // { // Console.WriteLine("没有腿的人可以扣篮"); // } //} class NBAPlayer :Person { public void KouLan() { Console.WriteLine("NBA球员可以扣篮"); } } class Teacher : Person,IKouLan { public void KouLan() { Console.WriteLine("老师也可以扣篮"); } } class Driver :Person,IKouLan { public void KouLan() { Console.WriteLine("司机也可以扣篮"); } }}
接口使用2
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace _12接口案例{ class Program { static void Main(string[] args) { //麻雀 会飞 企鹅不会飞 鸵鸟不会飞 鹦鹉会飞 飞机 会飞 IFly[] flys = { new MaBird(), new YingWu(), new Plane() }; for (int i = 0; i < flys.Length; i++) { flys[i].Fly(); } Console.ReadKey(); } } class Bird { public void CHLSS() { Console.WriteLine("鸟都会吃喝拉撒睡"); } } interface IFly { void Fly(); // void DownEggs();//飞机不会下蛋 } class MaBird : Bird, IFly { public void Fly() { Console.WriteLine("麻雀会飞"); } } class TuoBird : Bird { } class YingWu : Bird, IFly //基类必须在任何接口之前 { public void Fly() { Console.WriteLine("鹦鹉也会飞"); } } class QQ : Bird { } class Plane : IFly { public void Fly() { Console.WriteLine("飞机也会飞"); } }/////////////////////////////////////////////////////////////////////////// interface IStrong //接口不能继承类 { void Strong(); } interface NKWC //内裤外穿 { void NKWC(); } interface ISupperMan : IFly, IStrong, NKWC { } class Student : ISupperMan { public void Fly() { throw new NotImplementedException(); } public void Strong() { throw new NotImplementedException(); } public void NKWC() { throw new NotImplementedException(); } }}
显示实现接口
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace _13显示实现接口{ class Program { static void Main(string[] args) { T1 i = new Person(); i.Test(); Console.ReadKey(); } } class Person : T1 { public void Test() { Console.WriteLine("这个Test函数是属于Person的"); } //显示实现接口:告诉编译器 这个函数才是接口的 不是类的 //签名要一致 void T1.Test() { Console.WriteLine("显示是实现接口的Test函数"); } } interface T1 { void Test(); }}
抽象类实现接口
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace _14抽象类实现接口{ class Program { static void Main(string[] args) { T1 i = new PersonDaughter();//new PersonSon();//new Person i.Test(); Console.ReadKey(); } } abstract class Person :T1 { public void Test() { Console.WriteLine("抽象类实现接口"); } } class PersonSon: Person { } class PersonDaughter: Person { } interface T1 { void Test(); }}
阅读全文
0 0
- C#初识8 多态之接口(interface)
- C#之接口(Interface)
- 初识接口interface
- C#编程利器之三:接口(Interface)
- C#编程利器之三:接口(Interface)
- # c#基础系列之C# 接口(interface)
- C#接口Interface
- C#接口Interface
- C#接口Interface
- C#接口Interface
- C# interface 接口理解
- C# 接口使用 (interface)
- c# 接口interface 入门
- c# 接口 interface
- C# interface 接口理解
- C# Interface 接口
- C# 接口 interface
- C# 接口(Interface)
- Vulkan规范:第八章 8.11 ~ 8.13
- seo优化中怎么做聚合页-优秀聚合页参考
- 在React的Redux中,如何方便的查看next state的状态?
- Vulkan规范:第九章
- 【推荐系统】概率矩阵分解 probabilistic matrix factorization
- C#初识8 多态之接口(interface)
- 【AngularJS】数据绑定(双向绑定)
- Vulkan规范:第九章 9.1
- 推荐系统(RS),麻雀虽小,五脏俱全
- 数组初始化,清空数组,数组元素逆置
- 图像颜色空间转换-CSC
- 巩固与修炼(六)
- 查找数组元素位置
- Android高效加载OOM异常避免OOM