C# 面向对象:
类
public class Student
{
//构造函数。
//构造函数函数名和类名必须相同,并且不能有返回值。
public Student()
{
}
//构造函数重载。
public Student(string name,string age,string gender,string sid)
{
this.Name = name;
this.Age = age;
this.Gender = gender;
this.SId = sid;
}
//创建类的属性。
public string Name
{
get;
set;
}
public string Age
{
get;
set;
}
public string Gender
{
get;
set;
}
public string SId
{
get;
set;
}
//类中的方法体;
public void SayHi()
{
Console.WriteLine("hi!!!");
}
}
实例化类:
static void Main(string[] args)
{
Student stu = new Student("王云飞", "21", "研一", "321");
Console.WriteLine(stu.Name);
Console.ReadKey();
}
添加引用与导入命名空间的区别:
- 添加引用(前提): 添加程序集
- 导入命名空间 : namespace(shift+alt+F10)
变量作用域: 离声明该变量最近的那对包含声明语句的大括号内部。
1. 封装:
- 字段可以封装成为属性。
属性封装的用处: 可以对字段进行限制。
private int _age;
public int _Age
{
get { return _age; }
set
{
if(value<0 || value>=120)
{
throw new Exception("年龄有错!");
}
else
{
_age = value;
}
}
}
2. 继承:
继承的好处:
一、代码重用;
//基类
class Person
{
public string Name
{
get;
set;
}
public int Age
{
get;
set;
}
public int Hright
{
get;
set;
}
}
//类的继承
//继承具有 “单根性”。 子类只能继承自一个父类。
//继承具有 ”传递性”。
class Student :Person
{
public void SayHi()
{
Console.WriteLine("hi!");
}
}
//类的继承
class Teacher :Person
{
}
- 任何一个类,若没有继承,那他默认是继承自object。
- 继承具有 “单根性”。 子类只能继承自一个父类。
- 继承具有 ”传递性”。
继承中构造函数的问题:
当一个子类继承父类之后,该子类中的所有构造函数默认情况下,在自己被调用之前都会先调用一次父类的无参数的构造函数。如果此时父类中没有无参数的构造函数,则会报错。
该问题的解决方法有:
- 解决方法一:在父类中添加无参构造函数。
//解决方法一:在父类中添加无参构造函数。
//无参数构造函数
public Person()
{
}
//基类 构造函数的重载。
public Person(string name,int age,int height)
{
this.Name = name;
this.Age = age;
this.Height = height;
}
- 解决方法二:在子类的构造函数后面通过:base()的方式,指明指定要调用父类中的那个构造函数。
:base() 表示调用父类的构造函数。 //构造函数是不能被继承的。只能被调用。
//解决方法二:在子类的构造函数后面通过:base()的方式,指明指定要调用父类中的那个构造函数。
public Student(string name,int age,int height,string sid)
:base(name,age,height) //:base() 表示调用父类的构造函数。 //构造函数是不能被继承的。只能被调用。
{
this.SId = sid;
}
class Person
{
//解决方法一:在父类中添加无参构造函数。
//无参数构造函数
public Person()
{
}
//基类 构造函数的重载。
public Person(string name,int age,int height)
{
this.Name = name;
this.Age = age;
this.Height = height;
}
public string Name
{
get;
set;
}
public int Age
{
get;
set;
}
public int Height
{
get;
set;
}
}
class Student:Person
{
//当一个子类继承父类之后,该子类中的所有构造函数默认情况下,在自己被调用之前都会先调用一次父类的无参数的构造函数。如果此时父类中没有无参数的构造函数,则会报错。
//构造函数
public Student(string name,int age,int height,string sid)
{
this.Name = name;
this.Age = age;
this.Height = height;
this.SId = sid;
}
//解决方法二:在子类的构造函数后面通过:base()的方式,指明指定要调用父类中的那个构造函数。
public Student(string name,int age,int height,string sid)
:base(name,age,height) //:base() 表示调用父类的构造函数。 //构造函数是不能被继承的。只能被调用。
{
this.SId = sid;
}
public string SId
{
get;
set;
}
}
class Teacher:Person
{
//构造函数
public Teacher(string name, int age, int height, string enpid)
{
this.Name = name;
this.Age = age;
this.Height = height;
this.EmpId = enpid;
}
public string EmpId
{
get;
set;
}
}
使用this 调用自己的构造函数。
使用this来调用当前类(本类)中的其他构造函数。
class Person
{
public Person()
{
}
public Person(string name,int age,int height)
{
this.Name = name;
this.Age = age;
this.Height = height;
}
//使用this来调用当前类(本类)中的其他构造函数。
public Person(string name)
:this(name,0,0)
{
// this.Name = name;
}
......
}
3. 多态 —— 增加程序的可扩展性、灵活性。
通过虚方法实现多态。
多态: 同样一句话,根据对象的不同,调用的方法不同。
class Program
{
static void Main(string[] args)
{
//Person p1 = new American();
Person[] pers = new Person[5];
pers[0] = new American();
pers[1] = new Japanese();
pers[2] = new Chinese();
pers[3] = new Japanese();
pers[4] = new American();
//遍历person数组
for(int i=0;i<pers.Length;i++)
{
//希望此处只有一句话,说出各个国家的国籍
pers[i].SayNationlity(); //该句代码体现了多态。(同样一句话,根据对象的不同,调用的方法不同。)
}
Console.ReadKey();
}
}
class Person
{
public Person()
{
}
public Person(string name,int age,int height)
{
this.Name = name;
this.Age = age;
this.Height = height;
}
public Person(string name)
:this(name,0,0)
{
}
public string Name
{
get;
set;
}
public int Age
{
get;
set;
}
public int Height
{
get;
set;
}
//第一步:将父类中的对应方法前加virtual关键字。即:将对应的方法变为“虚方法”。
public virtual void SayNationlity()
{
Console.WriteLine("地球人");
}
}
class American :Person
{
//通过使用override关键字,将父类Person中的虚方法“SayNationlity”重写为子类自己想要的。
//说出自己的国籍。
public override void SayNationlity()
{
Console.WriteLine("usa");
}
}
class Japanese :Person
{
//说出自己的国籍。
public override void SayNationlity()
{
Console.WriteLine("日本");
}
}
class Chinese:Person
{
//说出自己的国籍。
public override void SayNationlity()
{
Console.WriteLine("中国");
}
}