您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。