您的位置:首页 > 编程语言 > C#

C#第四天

2015-11-02 20:08 295 查看
2、类
语法:
[public] class 类名
{
字段;
属性;
方法;
}
写好了一个类之后,我们需要创建这个类的对象,
那么,我们管创建这个类的对象过程称之为类的实例化。使用关键字 new.
this:表示当前这个类的对象。类是不占内存的,而对象是占内存的。
3、属性
属性的作用就是保护字段、对字段的赋值和取值进行限定。
属性的本质就是两个方法,一个叫get()一个叫set()。
既有get()也有set()我们诚之为可读可写属性。
只有get()没有set()我们称之为只读属性
没有get()只有set()我们称之为只写属性

public class Student
{
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;
}
public Student(string name,int chinese,int math,int english):this(name,0,'男',chinese,math,english)
{
//this.Name = name;
//this.Chinese = chinese;
//this.Math = math;
//this.English = english;
}
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; }
}
}


View Code
10,析构函数

当程序结束的时候,执行析构函数,帮助我们释放资源

~Studengt()

{

Console.WriteLine("我是用来释放资源的");

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: