.NET基础--继承
来源:互联网 发布:linux 查看java path 编辑:程序博客网 时间:2024/06/05 11:46
继承:分为基类和子类。
就像你的祖先是你的基类,而你就是你们祖先的一个子类。同时,你父亲就是你的父类,而你也是你父亲的子类。你父亲是你爷爷的子类。依次类推。
正如现实中的继承一样。你的DNA是继承你的父亲的DNA,但又有你自己的特征。
例如:
新建一个类Car
public class Car { public int width { get; set; } public int heigh { get; set; } public string name { get; set; } public void ShowCar() { Console.WriteLine("车辆名称:{0}", name); Console.WriteLine("车辆宽度:{0}", width); Console.WriteLine("车辆高度:{0}", heigh); } } public class BigCar : Car { public bool flag ; public void ShowBigCar() { Console.WriteLine("-----BigCar------"); Console.WriteLine("车辆名称:{0}", name); Console.WriteLine("车辆宽度:{0}", width); Console.WriteLine("车辆高度:{0}", heigh); if (flag == true) { Console.WriteLine("有电动座椅"); } Console.WriteLine("-----BigCar------"); } }
代码中的Car是BigCar的基类,也是父类。而BigCar是Car的子类。Car可以有很多子类。当然BigCar也可以有。
继承的规则: 子类:基类
使用方法
Car newCar=new Car(); newCar.name = "汽车"; newCar.heigh = 2220; newCar.width = 1600; newCar.ShowCar(); Console.WriteLine(); BigCar bigCar=new BigCar(); bigCar.name = "宝马"; bigCar.heigh = 2500; bigCar.width = 1700; bigCar.flag = true; bigCar.ShowCar(); bigCar.ShowBigCar(); Console.ReadKey();
子类可以调用父类的方法。反之不行。
0 0
- .Net 基础 之 继承
- .NET基础--继承
- .Net基础视频教程之面向对象继承
- 黑马程序员-.NET基础之继承和多态
- .net继承
- .NET继承本质论
- .net继承浅析
- .NET继承本质论
- .NET继承本质论
- .NET继承本质论
- Asp.net 页面 继承
- asp.net 继承身份验证
- 。net中继承
- Asp.net PageBase继承
- ASP.NET 页面继承
- .net继承实现身份验证
- vb.net 继承
- Asp.net PageBase继承
- LeetCode 226: Invert Binary Tree
- python处理Unicode规则
- 【Android】HttpUrlConnection AsyncTack
- c++中的string常用函数用法总结
- 关于TableViewCell的高度根据text来计算
- .NET基础--继承
- leetcode笔记:Linked List Cycle 2
- ios 调用系统相册、相机 显示中文标题
- 去掉xcode编译warning:ld: warning: directory not found for option '-L
- centos lsb版本冲突的解决方案
- MAP集合
- C++类型转换
- 贾扬清:希望Caffe成为深度学习领域的Hadoop
- ContentProvider+BroadcastReceiver综合练习