面向对象的三大特性:封装、继承、多态
什么水是类?什么是对象,类和对象的区别
类是模具,创建对象的模具,抽象的
类是一种数据类型,用户自定义的数据类型
类组成:字段,属性,方法,构造函数等
对象是具体的,是类的具体实例。对象具有属性(特征)和方法(行为)。
Ctrl r e 字段封装到属性
ctrl+K+D 快速格式化代码
this,当前对象
string str="12312313456";
int i=str.Count(t=>t=='1');
封装,类和对象本身就是封装的体现,把可能变化的东西封装起来,即打包
1.属性封装字段2.方法的多个参数封装成一个对象3.将一堆代码封装到一个方法中,
4.将一些功能封装到了几个类中,5将一些具有相同功能的代码封装到一个程序集中(dll,exe),并且对外提供统一的访问接口(属性名,方法
名等)
private void button1_Click(object sender, EventArgs e)
{
//MessageBox.Show(comboBox1.Text);
province p =(province)comboBox1.SelectedItem;
MessageBox.Show(p.name + "......" + p.id);
}
private void Form1_Load(object sender, EventArgs e)
{
province p1 = new province();
p1.name = "贵港";
p1.id = 1;
province p2 = new province() { name="安徽",id=2};//对象初始化器和上面的相同
province p3 = new province() { name = "上冻", id = 3 };
province p4 = new province() { name = "江苏", id = 4};
comboBox1.Items.Add(p1);
comboBox1.Items.Add(p2);
comboBox1.Items.Add(p3);
comboBox1.Items.Add(p4);
comboBox1.SelectedIndex = 0;
}
namespace 封装参数
{
public class province
{
public string name
{
get;
set;
}
public int id
{
get;
set;
}
public override string ToString()
{
return name;
}
}
}
继承是类和类之间的关系
对象不能谈继承,对象是类的具体实例(例如我(对象)人(类))
继承的好处:
代码重用
LSP里氏替换原则、多态(程序的课扩展性、灵活性)方法重写override。
虚方法的原理:虚方法表
子类继承父类的属性和方法,使创建子类变得很简单,实现了代码重用、以及多态。
类的单根继承性、传递性、继承时构造函数的问题
1.继承的时候构造函数不能被继承,2.子类的构造函数会默认去调用父类中的无参数数的构造函数。
:base调用指定父类构造函数
public class person
{
public person()
{
Console.WriteLine("调用父类构造函数");
}
public person(string name,string sex)
{
this.name = name;
this.sex = sex;
}
public string name
{
get;
set;
}
public string sex
{
get;
set;
}
public string age
{
get;
set;
}
}
public class student:person
{
public student(string name,string sex,string age,double soc):base("10","女")//base指明调用哪一个父类构造函数
{
this.name = name;
this.sex = sex;
this.socre = soc;
this.age = age;
}
public double socre
{
get;
set;
}
}
}
public class teacher:person
{
public double Salary
{
get;
set;
}
}
通过this调用自己类中的其他构造函数
public person(string name, string sex, string age)
{
this.sex = sex;
this.name = name;
this.age = age;
}
public person(string name,string sex)
:this(name,sex,null)//调用上一个构造函数,没有值得赋初值
{
//this.name = name;
//this.sex = sex;
}
public person(string name)
: this(name, null, null)
{
}
所有的类直接或者间接的继承自object。查看IL代码
对象初始化器:
person p =new person(){name="王春祥",age=18}
等同于p.Name="" ,p.age=""
集合初始化器:
List<int> list = new List<int>() { 10,20,30,40};
foreach(int item in list)
{
Console.Write(item);
}
等同于下面
Lsit<int> list =new List<int>();
list.Add(10);
list.Add(20);
forech(int item in list);
{
console.WriteLine(item);
}
什么水是类?什么是对象,类和对象的区别
类是模具,创建对象的模具,抽象的
类是一种数据类型,用户自定义的数据类型
类组成:字段,属性,方法,构造函数等
对象是具体的,是类的具体实例。对象具有属性(特征)和方法(行为)。
Ctrl r e 字段封装到属性
ctrl+K+D 快速格式化代码
this,当前对象
string str="12312313456";
int i=str.Count(t=>t=='1');
封装,类和对象本身就是封装的体现,把可能变化的东西封装起来,即打包
1.属性封装字段2.方法的多个参数封装成一个对象3.将一堆代码封装到一个方法中,
4.将一些功能封装到了几个类中,5将一些具有相同功能的代码封装到一个程序集中(dll,exe),并且对外提供统一的访问接口(属性名,方法
名等)
private void button1_Click(object sender, EventArgs e)
{
//MessageBox.Show(comboBox1.Text);
province p =(province)comboBox1.SelectedItem;
MessageBox.Show(p.name + "......" + p.id);
}
private void Form1_Load(object sender, EventArgs e)
{
province p1 = new province();
p1.name = "贵港";
p1.id = 1;
province p2 = new province() { name="安徽",id=2};//对象初始化器和上面的相同
province p3 = new province() { name = "上冻", id = 3 };
province p4 = new province() { name = "江苏", id = 4};
comboBox1.Items.Add(p1);
comboBox1.Items.Add(p2);
comboBox1.Items.Add(p3);
comboBox1.Items.Add(p4);
comboBox1.SelectedIndex = 0;
}
namespace 封装参数
{
public class province
{
public string name
{
get;
set;
}
public int id
{
get;
set;
}
public override string ToString()
{
return name;
}
}
}
继承是类和类之间的关系
对象不能谈继承,对象是类的具体实例(例如我(对象)人(类))
继承的好处:
代码重用
LSP里氏替换原则、多态(程序的课扩展性、灵活性)方法重写override。
虚方法的原理:虚方法表
子类继承父类的属性和方法,使创建子类变得很简单,实现了代码重用、以及多态。
类的单根继承性、传递性、继承时构造函数的问题
1.继承的时候构造函数不能被继承,2.子类的构造函数会默认去调用父类中的无参数数的构造函数。
:base调用指定父类构造函数
public class person
{
public person()
{
Console.WriteLine("调用父类构造函数");
}
public person(string name,string sex)
{
this.name = name;
this.sex = sex;
}
public string name
{
get;
set;
}
public string sex
{
get;
set;
}
public string age
{
get;
set;
}
}
public class student:person
{
public student(string name,string sex,string age,double soc):base("10","女")//base指明调用哪一个父类构造函数
{
this.name = name;
this.sex = sex;
this.socre = soc;
this.age = age;
}
public double socre
{
get;
set;
}
}
}
public class teacher:person
{
public double Salary
{
get;
set;
}
}
通过this调用自己类中的其他构造函数
public person(string name, string sex, string age)
{
this.sex = sex;
this.name = name;
this.age = age;
}
public person(string name,string sex)
:this(name,sex,null)//调用上一个构造函数,没有值得赋初值
{
//this.name = name;
//this.sex = sex;
}
public person(string name)
: this(name, null, null)
{
}
所有的类直接或者间接的继承自object。查看IL代码
对象初始化器:
person p =new person(){name="王春祥",age=18}
等同于p.Name="" ,p.age=""
集合初始化器:
List<int> list = new List<int>() { 10,20,30,40};
foreach(int item in list)
{
Console.Write(item);
}
等同于下面
Lsit<int> list =new List<int>();
list.Add(10);
list.Add(20);
forech(int item in list);
{
console.WriteLine(item);
}