1 构造函数
作用:帮助我们初始化对象(给对象的每个属性依次的赋值)
构造函数是一个特殊的方法:
1)、构造函数没有返回值,连void也不能写。
2)、构造函数的名称必须跟类名一样。
创建对象的时候会执行构造函数。
构造函数是可以有重载的。(如下例中的构造函数1/2/3/4实现了重载)
类当中会有一个默认的无参数的构造函数,当你写一个新的构造函数之后,不管是有参数的还是无参数的,那个默认的无参数的构造函数都被干掉了。(如下例构造函数4,即使是新建了无参数的构造函数,也不是默认的那个构造函数了)
2、new关键字
Person zsPerson=new Person();
new帮助我们做了3件事儿:
1)、在内存中开辟一块空间
2)、在开辟的空间中创建对象
3)、调用对象的构造函数进行初始化对象
3、this关键字
1)、代表当前类的对象
2)、在类当中显示的调用本类的构造函数 :this
4 限定非法值的输入(3种方法)
1 set方法限定(判断的是value)
2 get方法限定(判断的是字段)
3 构造函数中判断(判断传入值,如下例构造函数2)
举例:
Student.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _04面向对象练习
{
public class Student
{
/**析构函数**/
//当程序结束的时候 析构函数才执行,帮助我们释放资源
~Student()
{
Console.WriteLine("我是析构函数");
}
/**构造函数**/
//构造函数1
public Student(string name, int age, char gender, int chinese, int math, int english)
{
this.Name = name;
this.Age = age;
this.Gender = gender;
this.Chinese = chinese;
this.Math = math;
this.English = english;
}
//构造函数2
public Student(string name, int age, char gender)
{
this.Name = name;
this.Age = age;
if (gender != '男' && gender != '女')
{
gender = '男';
}
this.Gender = gender;
}
//构造函数3 (使用this避免代码冗余,此例中只需要初始化name、chinese、math和english,对于age和gender可以随便赋初值)
public Student(string name, int chinese, int math, int english) : this(name, 0, 'c', chinese, math, english)
{
}
//构造函数4
public Student()
{
Console.WriteLine("构造函数,但是不是默认的");
}
/**字段及属性**/
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
private int _age;
public int Age
{
get { return _age; }
set
{
if (value < 0 || value > 100)
{
value = 0;
}
_age = value;
}
}
private char _gender;
public char Gender
{
get
{
if (_gender != '男' && _gender != '女')
{
return _gender = '男';
}
return _gender;
}
set { _gender = value; }
}
private int _chinese;
public int Chinese
{
get { return _chinese; }
set { _chinese = value; }
}
private int _math;
public int Math
{
get { return _math; }
set { _math = value; }
}
private int _english;
public int English
{
get { return _english; }
set { _english = value; }
}
/**方法**/
public void SayHello()
{
Console.WriteLine("我叫{0},我今年{1}岁了,我是{2}生", this.Name, this.Age, this.Gender);
}
public void ShowScore()
{
Console.WriteLine("我叫{0},我的总成绩是{1},平均成绩是{2}", this.Name, this.Chinese + this.Math + this.English, (this.Chinese + this.Math + this.English) / 3);
}
}
}
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _04面向对象练习
{
class Program
{
static void Main(string[] args)
{
//构造函数1
Student zsStudent = new Student("张三", 18, '男', 100, 100, 100);
/* (如果不使用构造函数,那么每初始化一个Student类,如: 张山、小兰、李四都要加下面这么一坨代码,就有代码冗余)
Student zsStudent = new Student();
zsStudent.Name = "张山";
zsStudent.Age = 18;
zsStudent.Gender = '男';
zsStudent.Chinese=100;
zsStudent.English=100;
zsStudent.Math=100;
*/
zsStudent.SayHello();
zsStudent.ShowScore();
//构造函数2
Student lisi = new Student("李四", 20, '男');
lisi.SayHello();
lisi.ShowScore();
//构造函数3
Student xiaolan = new Student("小兰", 100, 100, 100);
xiaolan.SayHello();
xiaolan.ShowScore();
Console.ReadKey();
}
}
}
输出结果:
我叫张三,我今年18岁了,我是男生
我叫张三,我的总成绩是300,平均成绩是100
我叫李四,我今年20岁了,我是男生
我叫李四,我的总成绩是0,平均成绩是0
我叫小兰,我今年0岁了,我是男生
我叫小兰,我的总成绩是300,平均成绩是100