namespace _40繼承
{
class Program
{
static void Main(string[] args)
{
滁州網(wǎng)站制作公司哪家好,找成都創(chuàng)新互聯(lián)!從網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、成都響應(yīng)式網(wǎng)站建設(shè)公司等網(wǎng)站項目制作,到程序開發(fā),運(yùn)營維護(hù)。成都創(chuàng)新互聯(lián)于2013年開始到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗和運(yùn)維經(jīng)驗,來保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選成都創(chuàng)新互聯(lián)。
Student s = new Student("學(xué)生", 18, '男', 101); //新建學(xué)生對象并賦值
s.CHLSS(); //調(diào)用Person的方法
s.Study(); //調(diào)用Study的方法
Teacher t = new Teacher("老師",30,'女',5000); //新建老師對象并賦值
t.CHLSS(); //調(diào)用Person類的方法
t.Teach(); //調(diào)用Teach的方法
Driver d = new Driver("司機(jī)", 25, '男', 10); //新建司機(jī)對象并賦值
d.Drive(); //調(diào)用drive的方法
Console.WriteLine();
Console.ReadKey();
}
}
public class Person //新建Person類
{
private string _name; //字段
public string Name //屬性
{
get { return _name; }
set { _name = value; }
}
private int _age; //字段
public int Age //屬性
{
get { return _age; }
set { _age = value; }
}
private char _gender; //字段
public char Gender //屬性
{
get { return _gender; }
set { _gender = value; }
}
public void CHLSS() //Person方法
{
Console.WriteLine("吃喝拉撒睡");
}
public Person(string name, int age, char gender) //Person結(jié)構(gòu)
{
this.Name = name;
this.Age = age;
this.Gender = gender;
}
}
public class Student : Person //Student繼承person類
{
public Student(string name, int age, char gender, int id)
: base(name, age, gender) //繼承了Person類的name,age,gender三個屬性
{
//this.Name = name; //繼承的三個屬性可以不用重復(fù)再寫了
//this.Age = age; //繼承的三個屬性可以不用重復(fù)再寫了
//this.Gender = gender; //繼承的三個屬性可以不用重復(fù)再寫了
this.Id = id; //不屬于繼承的屬性需要賦值
Console.WriteLine("我叫{0},我今年{1}歲了,我是{2}生,我的學(xué)號是{3}", name, age, gender, id);
}
private int _id; //不屬于繼承的字段需要繼續(xù)寫
public int Id //屬性
{
get { return _id; }
set { _id = value; }
}
public void Study() //方法
{
Console.WriteLine("學(xué)生會學(xué)習(xí)");
}
}
public class Teacher :Person //Teacher繼承Person類
{
public Teacher(string name, int age, char gender, double salary)
: base(name, age, gender) //結(jié)構(gòu)繼承了Person的三個屬性
{
this.Salary = salary;
Console.WriteLine("我是{0},我今年{1}歲了,我是{2}生,我的工資是{3}", name, age, gender, salary);
}
private double _salary; // 字段
public double Salary //屬性
{
get { return _salary; }
set { _salary = value; }
}
public void Teach() //方法
{
Console.WriteLine("老師會講課");
}
}
public class Driver:Person //繼承person類
{
public Driver(string name, int age, char gender, int driveTime)
: base(name, age, gender) //繼承Person結(jié)構(gòu)
{
this.DirveTime = driveTime;
Console.WriteLine("我是{0},我今年{1}歲了,我是{2}生,我駕齡有{3}年了", name, age, gender, driveTime);
}
private int _dirveTime; //字段
public int DirveTime //屬性
{
get { return _dirveTime; }
set { _dirveTime = value; }
}
public void Drive() //方法
{
Console.WriteLine("司機(jī)會開車");
}
}
}