---------继承的案例------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Student s = new Student("langming", 12, '男', 123);
s.Studen();
Teacher t = new Teacher("daming", 50, '女', 1233);
t.Teache();
Driver d = new Driver("mingge", 21, '男', 121212);
d.Drive();
Console.ReadKey();
}
public class person
{
private string name;
private int age;
private char gender;
public string Name
{
set { name = value; }
get { return name; }
}
public int Age
{
set { age = value; }
get { return age; }
}
public char Gender
{
set { gender = value; }
get { return gender; }
}
public void CHLSS()
{
Console.WriteLine("吃饭睡觉");
}
//有参数的构造函数
public person(string name, int age, char gender)
{
this.name = name;
this.age = age;
this.gender = gender;
}
public void SayHello() {
Console.WriteLine("大家好,我叫朗明");
}
}
public class Student : person //冒号是继承谁的意思
{
private int id;
public int Id
{
set { id = value; }
get { return id; }
}
//引用父类的构造函数
public Student(string name, int age, char gender, int id)
: base(name, age, gender)
{
this.Id = id;
}
public void Studen() {
Console.WriteLine("我是{0},年龄是{1},性别是{2},id是{3}",Name,Age,Gender,Id);
}
public new void SayHello()
{
Console.WriteLine("大家好,我叫大名");
}
}
public class Teacher : person
{
private int salary;
public int Salary
{
set { salary = value; }
get { return salary; }
}
public Teacher(string name, int age, char gender, int salary)
: base(name, age, gender)
{
this.Salary = salary;
}
public void Teache() {
Console.WriteLine("我是{0},年龄是{1},性别是{2},薪水是{3}", Name, Age, Gender,Salary);
}
}
public class Driver : person
{
private int driverTime;
public int DriverTime {
set { driverTime = value; }
get { return driverTime; }
}
public Driver(string name, int age, char gender,int driverTime)
:base(name,age,gender)
{
this.driverTime = driverTime;
}
public void Drive() {
Console.WriteLine("我是{0},年龄是{1},性别是{2},开车的时间为是{3}", Name, Age,Gender, DriverTime);
}
}
}
}
//new 关键字的作用1,创建对象 2, 隐藏从父类哪里继承过来的同名成员
网友评论