Chinaunix首页 | 论坛 | 博客
  • 博客访问: 443521
  • 博文数量: 155
  • 博客积分: 786
  • 博客等级: 军士长
  • 技术积分: 1561
  • 用 户 组: 普通用户
  • 注册时间: 2012-09-01 23:37
个人简介

在路上

文章分类

全部博文(155)

文章存档

2016年(2)

2015年(36)

2014年(45)

2013年(34)

2012年(38)

我的朋友

分类: C#/.net

2015-08-17 16:28:12



7、构造函数
作用:帮助我们初始化对象(给对象的每个属性依次的赋值)
构造函数是一个特殊的方法:
1)、构造函数没有返回值,连void也不能写。
2)、构造函数的名称必须跟类名一样。


创建对象的时候会执行构造函数
构造函数是可以有重载的。
***
类当中会有一个默认的无参数的构造函数,当你写一个新的构造函数之后,不管是有参数的还是
无参数的,那个默认的无参数的构造函数都被干掉了。
8、new关键字
Person zsPerson=new Person();
new帮助我们做了3件事儿:
1)、在内存中开辟一块空间
2)、在开辟的空间中创建对象
3)、调用对象的构造函数进行初始化对象




9、this关键字
1)、代表当前类的对象
调用方法

点击(此处)折叠或打开

  1. namespace _01面向对象
  2. {
  3.     public class Person
  4.     {
  5.         private string _name;
  6.         public string Name
  7.         {
  8.             //当你输出属性的值得时候 会执行get方法
  9.             get { return _name; }
  10.             //当你给属性赋值的时候 首先会执行set方法
  11.             set { _name = value; }
  12.         }
  13.         private int _age;
  14.         public int Age
  15.         {
  16.             get { return _age; }
  17.             set
  18.             {

  19.                 if (value < 0 || value > 100)
  20.                 {
  21.                     value = 0;
  22.                 }

  23.                 _age = value;
  24.             }
  25.         }
  26.         private char _gender;
  27.         public char Gender
  28.         {
  29.             get
  30.             {
  31.                 if (_gender != '男' && _gender != '女')
  32.                 {
  33.                     return _gender = '男';
  34.                 }
  35.                 return _gender;

  36.             }
  37.             set { _gender = value; }
  38.         }
  39.         public void CHLSS()
  40.         {
  41.             Console.WriteLine("我叫{0},我今年{1}岁了,我是{2}生,我可以吃喝拉撒睡哟~~~", this.Name, this.Age, this.Gender);
  42.             while (true)
  43.             {
  44.             
  45.             };
  46.         }
  47.     }
  48. }


2)、在类当中显示的调用本类的构造函数  :this

点击(此处)折叠或打开

  1. namespace _04面向对象练习
  2. {
  3.     public class Student
  4.     {
  5.         //字段、属性、方法、构造函数

  6.        //析构函数 构造函数



  7.         //当程序结束的时候 析构函数才执行
  8.         //帮助我们释放资源
  9.         //GC Garbage Collection
  10.         ~Student()
  11.         {
  12.             Console.WriteLine("我是析构函数");
  13.         }


  14.         public Student(string name, int age, char gender, int chinese, int math, int english)
  15.         {
  16.             this.Name = name;
  17.             this.Age = age;
  18.             this.Gender = gender;
  19.             this.Chinese = chinese;
  20.             this.Math = math;
  21.             this.English = english;
  22.         }
  23.         public Student(string name, int chinese, int math, int english):this(name,0,'c',chinese,math,english)
  24.         {
  25.             //this.Name = name;
  26.             //this.Chinese = chinese;
  27.             //this.Math = math;
  28.             //this.English = english;
  29.         }
  30.         public Student(string name, int age, char gender)
  31.         {
  32.             this.Name = name;
  33.             if (age < 0 || age > 100)
  34.             {
  35.                 age = 0;
  36.             }
  37.             this.Age = age;
  38.             this.Gender = gender;
  39.         }

  40.         public Student()
  41.         {
  42.             
  43.         }



  44.         private string _name;
  45.         public string Name
  46.         {
  47.             get { return _name; }
  48.             set { _name = value; }
  49.         }
  50.         private int _age;
  51.         public int Age
  52.         {
  53.             get { return _age; }
  54.             set
  55.             {
  56.                 if (value < 0 || value > 100)
  57.                 {
  58.                     value = 0;
  59.                 }
  60.                 _age = value;
  61.             }
  62.         }
  63.         private char _gender;
  64.         public char Gender
  65.         {
  66.             get
  67.             {
  68.                 if (_gender != '男' && _gender != '女')
  69.                 {
  70.                     return _gender = '男';
  71.                 }
  72.                 return _gender;
  73.             }
  74.             set { _gender = value; }
  75.         }
  76.         private int _chinese;
  77.         public int Chinese
  78.         {
  79.             get { return _chinese; }
  80.             set { _chinese = value; }
  81.         }
  82.         private int _math;
  83.         public int Math
  84.         {
  85.             get { return _math; }
  86.             set { _math = value; }
  87.         }
  88.         private int _english;
  89.         public int English
  90.         {
  91.             get { return _english; }
  92.             set { _english = value; }
  93.         }


  94.         public void SayHello()
  95.         {
  96.             Console.WriteLine("我叫{0},我几年{1}岁了,我是{2}生", this.Name, this.Age, this.Gender);
  97.         }

  98.         public void ShowScore()
  99.         {
  100.             Console.WriteLine("我叫{0},我的总成绩是{1},平均成绩是{2}", this.Name, this.Chinese + this.Math + this.English, (this.Chinese + this.Math + this.English) / 3);
  101.         }

  102.     }
  103. }



阅读(468) | 评论(0) | 转发(0) |
0

上一篇:C#命名规则

下一篇:C# 命名空间

给主人留下些什么吧!~~