internal class Program
{
static void Main(string[] args)
{
//构造函数
//作用:给对象的每个属性依次赋值
//注意:构造函数没有返回值,连void都不能写,构造函数名称必须跟类名一样
//new的作用
//1.在内存中帮我们开辟了一空间
//2.在开辟空间中创建对象
//3.调用对象的构造函数进行初始化对象
//this的作用
//代表当前类的对象,在类中显示的调用本类的构造函数
//析构函数
//当程序结束的时候才被调用
//作用:析构函数是用来释放资源的
Person b = new Person("张三", 22, '男', 89, 100, 87);
Person p = new Person("张三",100,89,67);
p.MyGrade();
Console.ReadKey();
}
}
public class Person
{
//字段
public string _name;
private int _age;
private char _gender;
public int _language;
public int _mathematics;
public int _english;
//属性
public string Name { get; set; }
public int Age
{
get { return _age; }
set
{
if (value > 100 && value < 0)
{
value = 0;
}
_age = value;
}
}
public char Gender
{
get
{
if (_gender != '男' && _gender != '女')
{
return _gender = '男';
}
return _gender;
}
set { }
}
public int Language { get; set; }
public int Mathematics { get; set; }
public int English { get; set; }
//方法
public void MyGrade()
{
Console.WriteLine("我的名字叫{0},我的年龄是{1}岁,我的性别是{2}生,我的语文成绩是{3},我的数学成绩是{4},我的英语成绩是{5},我的平均成绩是{6}",
this.Name,
this.Age,
this.Gender,
this.Language,
this.Mathematics,
this.English,
(this.Language + this.Mathematics + this.English) / 3);
}
//构造函数
public Person(string name, int age, char gender, int language, int mathematics, int english)
{
this.Name = name;
this.Age = age;
this.Gender = gender;
this.Language = language;
this.Mathematics = mathematics;
this.English = english;
}
//重载
public Person(string name, int language, int mathematics, int english) : this(name, 0, 'c', language, mathematics, english)
{
}
//析构函数
~Person()
{
Console.WriteLine("我是析构函数");
}
}