前面提到过,面向对象的三大特性:封装性、继承性和多态性。封装在前面类的定义里也了解的差不多透彻了,现在看看继承的特性。
继承实际是一个类对另一个类的扩展,后者称之为基类,前者称之为子类。继承就是子类拥有基类的一切属性和方法,子类还可以增加属性和方法。但是子类不能去掉父类的属性和方法。
当然这里还要提到修饰符的问题,子类拥有基类的所有属性和方法,不意味着子类可以任意访问继承的这些属性和方法。子类只能访问到public和protected修饰的属性和方法,其余无法直接访问。还有一个就是static的属性和方法是不能被继承下来的,因为静态类型之和类有关系与对象无关。
看代码:
using system;
namespace yys.csharpstudy.mainconsole
{
public class yschool
{
private int id = 0;
private string name = string.empty;
public int id
{
get
{
return this.id;
}
}
public string name
{
get
{
return name;
}
}
/// <summary>
/// 构造器
/// </summary>
public yschool()
{
this.id = 0;
this.name = @"清华大学附中";
}
/// <summary>
/// 构造器
/// </summary>
public yschool(int id, string name)
{
this.id = id;
this.name = name;
}
/// <summary>
/// 构造器
/// </summary>
public yschool(int id)
{
this.id = id;
this.name = @"陕师大附中";
}
}
public class yteacher
{
private int id = 0;
private string name = string.empty;
private yschool school = null;
private string introduction = string.empty;
private string imagepath = string.empty;
/// <summary>
/// 使用只读属性,因为是固有属性。
/// </summary>
public int id
{
get
{
return id;
}
}
public string name
{
get
{
return name;
}
}
/// <summary>
/// 这几个使用get/set属性,因为这几个属性不是固有的,可以随着某些条件改变的。
/// </summary>
public yschool school
{
get
{
if (school == null)
{
school = new yschool();
}
return school;
}
set
{
school = value;
}
}
public string introduction
{
get
{
return introduction;
}
set
{
introduction = value;
}
}
public string imagepath
{
get
{
return imagepath;
}
set
{
imagepath = value;
}
}
public yteacher(int id, string name)
{
this.id = id;
this.name = name;
}
/// <summary>
/// 给学生讲课的方法
/// </summary>
public void toteachstudents()
{
console.writeline(string.format(@"{0} 老师教育同学们: good good study,day day up!", this.name));
}
/// <summary>
/// 惩罚犯错误学生的方法
/// </summary>
/// <param name="punishmentcontent"></param>
public void punishmentstudents(string punishmentcontent)
{
console.writeline(string.format(@"{0} 的{1} 老师让犯错误的学生 {2}。", this.school.name, this.name, punishmentcontent));
}
}
/// <summary>
/// 男性老师,继承自yteacher
/// </summary>
public class mrteacher : yteacher
{
/// <summary>
/// 构造器,这里要注意,yteacher是没有提供默认构造器的,
/// 所以子类必须要有构造器,并且和基类参数列表一致。
/// </summary>
/// <param name="id"></param>
/// <param name="name"></param>
public mrteacher(int id, string name)
: base(id, name)
{
}
/// <summary>
/// 扩展的方法,刮胡子方法。
/// </summary>
public void shave()
{
console.writeline(string.format(@"{0} 老师用飞科剃须刀刮胡子。",this.name));
}
}
/// <summary>
/// 女性老师,继承自yteacher
/// </summary>
public class misteacher : yteacher
{
/// <summary>
/// 构造函数
/// </summary>
/// <param name="id"></param>
/// <param name="name"></param>
public misteacher(int id, string name)
: base(id, name)
{
}
/// <summary>
/// 扩展方法,护肤的方法
/// </summary>
public void skincare()
{
console.writeline(string.format(@"{0} 老师用香奈儿护肤霜护肤。",this.name));
}
}
}
using system;
namespace yys.csharpstudy.mainconsole
{
class program
{
static void main(string[] args)
{
mrteacher mrteacher = new mrteacher(1, @"牛轰轰");
mrteacher.toteachstudents();
mrteacher.punishmentstudents(@"背唐诗");
mrteacher.shave();
misteacher misteacher = new misteacher(2, @"郝漂靓");
misteacher.toteachstudents();
misteacher.punishmentstudents(@"默写红楼梦");
misteacher.skincare();
console.readkey();
}
}
结果:
继承是面向对象的特性,它的好处是:
第一、做项目设计类时,继承可以让我们省略量代码;
第二、符合面向对象中类的组织形式,在c#、java等高级面向对象语言中,都有一个object类,这是所有类的祖宗类,其他类都有他派生而来。
继承需要注意一点,就是子类的构造器问题。程序运行过程是子类首先调用父类的构造器。如果子类没有写构造器,那么就会调用父类的默认构造器。如果父类没有默认的构造器,即父类写了带参数的构造器,那么子类也就调用带参数的那个构造器,不过需要指明是调用的哪个带参数的构造器(见代码中base)。
以上就是c#基础知识整理:基础知识(4) 继承的内容。