C# 日常补漏 不断更新ing

封装   

数据安全性 / 黑箱特性 不必关注具体实现 

继承

避免冗余 代码重用 / 多态  

LSP\\里氏替换原则

public class Student : Person

...

Person per = new Student();

多态

可拓展性 / 灵活性

构造函数

子类中可用base指定要调用的父类构造函数 否则会默认调用空参的父类构造函数

同理在本类中用this调用自身的构造函数

public class Person{

        public Person(string name, int age);

}

public class Student : Person{

        public Student(id,name,age):base(name,age)

}

访问修饰符

private : 当前类内部

protected : 当前类及子类内部

internal : 当前程序集内部  不同项目间即便添加引用也不能调用

public : 

当类中属性未指定修饰符时即默认 private

类本身未指定修饰符即默认 internal

虚方法

virtual & override 在子类中重写父类的虚方法

猜你喜欢

转载自blog.csdn.net/weixin_38807994/article/details/87933720
今日推荐