ylbtech- .NET-Basic: A.4.1-类的继承(implement)

 A.4.1-类的继承(implement)

1.A,源代码​返回顶部​

A.4.1,Person.cs



A.4.1-类的继承(implement)_继承A.4.1-类的继承(implement)_C#_02View Code


using System;

namespace 继承
{
class Person
{

//字段
int id; //编号
string name; //姓名
string sex; //性别

//空参构造
public Person()
{
this.id = 1000;
this.name = "xiao'yu";
this.sex = "女";
}
//全参构造
public Person(int id, string name, string sex)
{
this.id = id;
this.name = name;
this.sex = sex;
}

//展示
public void Show()
{
Console.WriteLine("编号:{0}",id);
Console.WriteLine("姓名:{0}",name);
Console.WriteLine("性别:{0}",sex);
}

public int Id
{
get { return id; }
set { id = value; }
}
public string Name
{
get { return name; }
set { name = value; }
}
public string Sex
{
get { return sex; }
set { sex = value; }
}


}
}


A.4.2,Student.cs



A.4.1-类的继承(implement)_继承A.4.1-类的继承(implement)_C#_02View Code


using System;

namespace 继承
{
//学生类继承人类
class Student:Person
{

string grade; //学期


//空参构造
public Student()
: base()
{
this.grade = "1期";
}
//全参构造
public Student(int id, string name, string sex, string grade)
: base(id, name, sex)
{
this.grade = grade;
}

//使用new关键字重写父类方法
new public void Show()
{
base.Show();
Console.WriteLine("学期:{0}",grade);
}


public string Grade
{
get { return grade; }
set { grade = value; }
}
}
}


A.4.3,Teacher.cs



A.4.1-类的继承(implement)_继承A.4.1-类的继承(implement)_C#_02View Code


using System;

namespace 继承
{
class Teacher:Person
{

//字段
string zhuanYe;//专业


//空参构造

//全参构造

//使用new重写基类的什么方法Show

public string ZhuanYe
{
get { return zhuanYe; }
set { zhuanYe = value; }
}
}
}


A.4.0,Program.cs




using System;

namespace 继承
{
class Program
{
static void Main(string[] args)
{

//实例化对象

//多行注释/* */
/*
//1
Person p = new Person();

p.Show();
*/

/*
//2
Person p = new Person(2000, "yuanbo", "男");

p.Show();
* */

/*
//3
Student s = new Student();

s.Show();
* */
/*
//4
Student s = new Student(2000, "mike", "男", "3期");
s.Show();
* */

Console.ReadLine();
}
}
}


A.4.1-类的继承(implement)_implement_07

作者:ylbtech