继续的头脑完成了 属于(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---多重继续的内容,更多相关内容请关注ki4网(www.ki4.cn)!