C#学习日记22---多重继承
来源:互联网 发布:两进一出网络切换器 编辑:程序博客网 时间:2024/05/22 11:58
继承是面向对象程序设计中最重要的概念之一。继承允许我们根据一个类来定义另一个类来定义一个类,一个类从另一个类派生出来时,派生类从基类那里继承特性
继承的思想实现了 属于(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("狗", "汪汪"); } }}
0 0
- C#学习日记22---多重继承
- C++与C#对比学习:多重继承
- 学习日记之桥接模式和多重继承
- C#中多重继承
- C#中多重继承
- C#多重继承
- c#多重继承
- C#中多重继承
- C++学习 多重继承
- python学习-多重继承
- C++学习 多重继承
- C#接口浅谈(多重继承)
- 学习日记02 继承
- python学习笔记 多重继承
- C++学习笔记-多重继承&虚拟继承
- Python学习09:继承与多重继承
- C++学习笔记22 关于多重继承 接口
- C#中接口多重继承的注意事项
- 在Ubuntu14.04下安装matlab2014a以及破解
- 提高编码效率-Live Template
- MongoDB 用时间筛选_id字段
- C语言经验
- iOS中的多线程技之Pthread和NSThread
- C#学习日记22---多重继承
- spoj1825 Free tour II 点分治
- 《MFC中读取.mat文件中任意位置的数据》
- js 回调(callback)
- spring的Aspect使用例子:
- Spark On Yarn(HDFS HA)详细配置过程
- vim安装过程中所涉及的问题及处理办法
- 网络编程 笔记(三) 套接字类型 协议设置
- Unity Text 插入超链接