码迷,mamicode.com
首页 > 其他好文 > 详细

面向对象的复习_封装

时间:2014-10-12 02:22:57      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   ar   strong   数据   sp   

封装
字段:存储数据,访问修饰符应该设置为private私有的

string _name;

属性:保护字段,对字段的取值和赋值进行限定

public string Name
{
    get { return _name; }
    set { _name = value; }
}

构造函数:初始化对象,当创建对象的时候会调用构造函数

public Person(string name)
{
    this.Name = name;           
}

new关键字:

Person p = new Person();

1.在堆中开辟空间

2.在开辟的空间中创建对象

3.调用对象的构造函数


this关键字:

1.代表当前类的对象

2.调用自己的构造函数

bubuko.com,布布扣
 1     class Person
 2     {
 3         //字段 属性 构造函数 方法 接口
 4         string _name;//字段
 5         public string Name//属性
 6         {
 7             get { return _name; }
 8             set//限定_name的值
 9             {
10                 if (value != "张三")
11                 {
12                     value = "张三";
13                 }
14                 _name = value;
15             }
16         }
17         int _age;
18         public int Age
19         {
20             get//限定_age的取值范围
21             {
22                 if (_age < 0 || _age > 100)
23                 {
24                     return _age = 0;
25                 }
26                 return _age;
27             }
28             set { _age = value; }
29         }
30         char _gender;
31         public char Gender
32         {
33             get { return _gender; }
34             set { _gender = value; }
35         }
36         int _chinese;
37         public int Chinese
38         {
39             get { return _chinese; }
40             set { _chinese = value; }
41         }
42         int _math;
43         public int Math
44         {
45             get { return _math; }
46             set { _math = value; }
47         }
48         int _english;
49         public int English
50         {
51             get { return _english; }
52             set { _english = value; }
53         }
54         //构造函数
55         public Person(string name, int age, char gender, int chinese, int math, int english)
56         {
57             this.Name = name;
58             this.Age = age;
59             if (gender !=  && gender != )//对字段的取值进行限定
60             {
61                 gender = ;
62             }
63             this.Gender = gender;
64             this.Chinese = chinese;
65             this.Math = math;
66             this.English = english;
67         }
68 
69         //通过this调用自身的构造函数
70         public Person(string name, int age, char gender)
71             : this(name, age, gender, 0, 0, 0)
72         {
73             this.Name = name;
74             this.Age = age;
75             this.Gender = gender;
76         }
77 
78 
79         public void SayHello()
80         {
81             Console.WriteLine("{0}------{1}-----{2}", this.Name, this.Age, this.Gender);
82         }
83     }
View Code

 

面向对象的复习_封装

标签:style   blog   http   color   os   ar   strong   数据   sp   

原文地址:http://www.cnblogs.com/amixc/p/4019983.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!