您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息
三六零分类信息网 > 景德镇分类信息网,免费分类信息发布

C#学习日记22---多重继承

2024/2/17 20:47:03发布20次查看
继承是面向对象程序设计中最重要的概念之一。继承允许我们根据一个类来定义另一个类来定义一个类,一个类从另一个类派生出来时,派生类从基类那里继承特性
继承的思想实现了 属于(is-a) 关系。例如,哺乳动物 属于(is-a) 动物,狗属于(is-a) 哺乳动物,因此狗 属于(is-a) 动物。
基类与派生类:
c#中派生类从他的直接基类继承成员,方法、属性、域、事件、索引指示器但是除开构造函数与析构函数。
下面写个实例。
using system; using system.collections.generic; using system.linq; using system.text; namespace test { class anlimal //定义一个基类 { protected int foot = 4; protected double weight = 22.4; protected void say(string type, string call) { console.writeline("类别:{0},叫声:{1} ",type,call); } } //dog 继承anlimal class dog:anlimal { static void main(string[] args) { dog dog = new dog(); int foot = dog.foot; double weight = dog.weight; console.writeline("dog foot: {0}\ndog weight:{1}",foot,weight); dog.say("狗", "汪汪"); } } }
结果
多重继承:
c# 不支持多重继承。但是,您可以使用接口来实现多重继承,上面的例子我们为他添加一个smallanlimal接口
using system; using system.collections.generic; using system.linq; using system.text; namespace test { class anlimal //定义一个基类 { protected int foot = 4; protected double weight = 22.4; protected void say(string type, string call) { console.writeline("类别:{0},叫声:{1} ",type,call); } } public interface smallanlimal //添加一个接口 接口只声明方法在子类中实现 { protected void hight(double hight); } //dog 继承anlimal class dog:anlimal,smallanlimal { public void hight(double hight) //实现接口 { console.writeline("hight: {0}",hight); } static void main(string[] args) { dog dog = new dog(); int foot = dog.foot; double weight = dog.weight; dog.hight(23.23); console.writeline("dog foot: {0}\ndog weight:{1}",foot,weight); dog.say("狗", "汪汪"); } } }
以上就是 c#学习日记22---多重继承的内容。
景德镇分类信息网,免费分类信息发布

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录