您好,登錄后才能下訂單哦!
namespace _40繼承
{
class Program
{
static void Main(string[] args)
{
Student s = new Student("學生", 18, '男', 101); //新建學生對象并賦值
s.CHLSS(); //調用Person的方法
s.Study(); //調用Study的方法
Teacher t = new Teacher("老師",30,'女',5000); //新建老師對象并賦值
t.CHLSS(); //調用Person類的方法
t.Teach(); //調用Teach的方法
Driver d = new Driver("司機", 25, '男', 10); //新建司機對象并賦值
d.Drive(); //調用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結構
{
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; //繼承的三個屬性可以不用重復再寫了
//this.Age = age; //繼承的三個屬性可以不用重復再寫了
//this.Gender = gender; //繼承的三個屬性可以不用重復再寫了
this.Id = id; //不屬于繼承的屬性需要賦值
Console.WriteLine("我叫{0},我今年{1}歲了,我是{2}生,我的學號是{3}", name, age, gender, id);
}
private int _id; //不屬于繼承的字段需要繼續寫
public int Id //屬性
{
get { return _id; }
set { _id = value; }
}
public void Study() //方法
{
Console.WriteLine("學生會學習");
}
}
public class Teacher :Person //Teacher繼承Person類
{
public Teacher(string name, int age, char gender, double salary)
: base(name, age, gender) //結構繼承了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結構
{
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("司機會開車");
}
}
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。