类的继承
继承语法:子类:父类名称
- 在继承中子类只能继承父类中的非私有成员
- protected修饰的成员变量只能被自己子类内部访问
- 子类在创建的时候会默认调用父类中空的构造函数初始化父类中的成员变量,之后再调用子类中的构造函数与初始化成员变量
- 通过base控制父类初始化的构造函数
//父类
public class Person
{
public int? Id { set; get; }
public string Name { set; get; }
protected int age { set; get; }
protected void Emo()
{
}
public Person()
{
Console.WriteLine("person");
}
public Person(string canshu)
{
this.Name= canshu;
Console.WriteLine($"person{canshu}");
}
}
//继承
public class Student:Person
{
public string School { set; get; }
public string Class { set; get; }
public void NName()
{
//只能直接访问父类中保护类型的成员 protected
this.age = 10;
this.Emo();
Console.WriteLine($"我的名字叫做{Name}");
}
//默认调用父类中空的构造函数
//public Student()
//{
// Console.WriteLine("Student");
//}
显式调用父类中空的构造函数
//public Student():base()
//{
// Console.WriteLine("Student");
//}
//显式调用父类中带参数的构造函数
public Student() : base("canshu")
{
Console.WriteLine($"Student的名字{Name}");
}
}
多态
多态:一个事物的多种表现形式,分别是继承、方法重载
继承
- 在子类中进行父类方法的重写,方法签名(方法修饰符与名称)和参数类型完全一致
//父类
public class Person
{
public int? Id { set; get; }
public string Name { set; get; }
protected int age { set; get; }
protected void Emo()
{
}
public void Whoo()
{
Console.WriteLine($"我是{Name},是一个人");
}
public Person()
{
Console.WriteLine("person");
}
public Person(string canshu)
{
this.Name= canshu;
Console.WriteLine($"person{canshu}");
}
}
//子类
public class Student:Person
{
public string School { set; get; }
public string Class { set; get; }
public void NName()
{
//只能直接访问父类中保护类型的成员 protected
this.age = 10;
this.Emo();
Console.WriteLine($"我的名字叫做{Name}");
}
//默认调用父类中空的构造函数
//public Student()
//{
// Console.WriteLine("Student");
//}
显式调用父类中空的构造函数
//public Student():base()
//{
// Console.WriteLine("Student");
//}
//显式调用父类中带参数的构造函数
public Student() : base("canshu")
{
Console.WriteLine($"Student的名字{Name}");
}
//对父类中的方法重写
public void Whoo()
{
Console.WriteLine($"我是{Name},是一个学生");
}
}
//测试
public void Test()
{
Student student = new Student();
student.Name = "dgbfd";
student.NName();
student.Whoo();
}
- 为保证子类方法重写完全覆盖父类,必须在父类方法上添加virtual关键字,在子类方法上添加override关键字
//父类
public class Person
{
public int? Id { set; get; }
public string Name { set; get; }
protected int age { set; get; }
protected void Emo()
{
}
public virtual void Whoo()
{
Console.WriteLine($"我是{Name},是一个人");
}
public Person()
{
Console.WriteLine("person");
}
public Person(string canshu)
{
this.Name= canshu;
Console.WriteLine($"person{canshu}");
}
}
public class Student:Person
{
public string School { set; get; }
public string Class { set; get; }
public void NName()
{
//只能直接访问父类中保护类型的成员 protected
this.age = 10;
this.Emo();
Console.WriteLine($"我的名字叫做{Name}");
}
//默认调用父类中空的构造函数
//public Student()
//{
// Console.WriteLine("Student");
//}
显式调用父类中空的构造函数
//public Student():base()
//{
// Console.WriteLine("Student");
//}
//显式调用父类中带参数的构造函数
public Student() : base("canshu")
{
Console.WriteLine($"Student的名字{Name}");
}
//对父类中的方法重写
public override void Whoo()
{
Console.WriteLine($"我是{Name},是一个学生");
}
}
public class Farmer : Person
{
public void NName()
{
//只能直接访问父类中保护类型的成员 protected
this.age = 30;
this.Emo();
Console.WriteLine($"我的名字叫做{Name}");
}
//对父类中的方法重写
public override void Whoo()
{
Console.WriteLine($"我是{Name},是一个农名");
}
}
public class Teacher : Person
{
public void NName()
{
//只能直接访问父类中保护类型的成员 protected
this.age = 30;
this.Emo();
Console.WriteLine($"我的名字叫做{Name}");
}
//对父类中的方法重写
public override void Whoo()
{
Console.WriteLine($"我是{Name},是一个老师");
}
}
public void Test()
{
Student student = new Student();
student.Name = "dgbfd";
student.NName();
student.Whoo();
Student student0 = new Student();
student0.Name = "dgbfd";
student0.Whoo();
Person farmer = new Farmer();
farmer.Name = "dgs";
farmer.Whoo();
Person teacher = new Teacher();
teacher.Name = "fbfd";
teacher.Whoo();
}
3.面向对象的封装
//面向对象的封装
public static void WhoIm(Person person)
{
person.Whoo();
}
public void Test8()
{
Student student = new Student();
student.Name = "dgbfd";
student.NName();
student.Whoo();
Student student0 = new Student();
student0.Name = "dgbfd";
student0.Whoo();
Person farmer = new Farmer();
farmer.Name = "dgs";
farmer.Whoo();
Person teacher = new Teacher();
teacher.Name = "fbfd";
teacher.Whoo();
//封装方法的使用
WhoIm(student0);
WhoIm(farmer);
WhoIm(teacher);
}
方法重载
同名不同参
public class Calculator
{
public int sum(int a, int b)
{
return a + b;
}
public int sum(int a, int b, int c)
{
return a + b + c;
}
public double sum(double a, double b)
{
return a + b;
}
public float sum(float a, float b)
{
return a + b;
}
}