![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace Demo001 7 { 8 class Program 9 { 10 static void Main(string[] args) 11 { 12 #region MyRegion 13 //Person p = new Person(); 14 //p.Name = "王金彪"; 15 //p.Age = 12; 16 //p.Gender = true; 17 //p.Height = 176; 18 //p.SayHi(); 19 //创建对象的时候就给对象的属性赋初始值. 20 //1. 对象的初始化器 可以快速的为这个对象的各个属性赋值 但是想给谁赋就给谁赋 21 //Person p = new Person() { Age = 12, Country = "中国", Gender = false, Name = "王金彪", IsGoodMan = false, Height = 167 }; 22 //Person p1 = new Person() { Country="日本", IsGoodMan=false }; 23 //p1.SayHi(); 24 //p.SayHi(); 25 //调用方法的时候 会用到小括弧 26 //Person p = new Person(); 27 Person p = new Person("张小花", "中国");//括弧是用来调用构造方法的.... 28 Console.WriteLine(p.Name); 29 //new 关键字做的事情 30 //1. 开辟合适大小的空间 2创建对象 3调用构造函数 4 返回引用地址 31 #endregion 32 33 // 构造函数是自动被调用的。 34 //创建这个类的对象的时候 自动调用构造函数... 35 //Dog d = new Dog(); 36 //Dog d1 = new Dog("旺财",2,true); 37 //Console.WriteLine(d1.name); 38 Cat c = new Cat("旺财", 2, true); 39 40 //Pig pig = new Pig(3,"猪猪"); 41 Pig p1 = new Pig(1, "花花"); 42 // Console.WriteLine(pig.Age); 43 44 45 Console.ReadKey(); 46 } 47 } 48 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace Demo001 7 { 8 /// <summary> 9 /// 人类 10 /// </summary> 11 class Person 12 { 13 public int Age { get; set; } 14 public string Name { get; set; } 15 public string Country { get; set; } 16 public bool Gender { get; set; } 17 public double Height { get; set; } 18 public bool IsGoodMan { get; set; } 19 //构造函数 20 //构造函数是1个特殊的函数(方法) 21 //1. 访问修饰符一般情况下是 public 22 //2. 没有返回值(连void都不要写) 23 //3. 方法名要与类名一致 24 // 方法是用来干什么的? 25 // 急需解决的问题 26 //1. 这个构造函数什么时候被调用? 27 // 创建这个类的对象的时候 会调用这个构造函数.... 28 //2. 构造函数只能在创建对象的时候被调用吗? 29 // 构造函数只能创建对象的时候 被CLR自动调用 不能被程序员手动调用. 30 //3. 方法有返回值 构造函数可以有返回值吗? 31 // 构造方法是绝对不能有返回值的 连 VOID都不要写了... 32 //public int Person() 33 //{ 34 // Console.WriteLine("构造函数被调用啦...."); 35 // return 1; 36 //} 37 //4. 方法体里return 38 //5. 除了返回值不能有 其他的用法与普通的方法一致。。。。 39 public Person() 40 { 41 Console.WriteLine("构造函数被调用1...."); 42 Console.WriteLine("构造函数被调用2...."); 43 } 44 //构造函数的作用.... 45 //当我们创建这个类的对象的时候 如果需要做什么事情 可以讲代码些在构造函数中..... 46 //一般情况下 我们是在构造函数中为对象的属性赋初始值..... 47 //方法重载的要素 48 //1. 在同1个类中 2方法名相同 3 参数的个数或者类型不同 4 与返回值无关。 49 50 public Person(string name,string country) 51 { 52 this.Name = name; 53 this.Country = country; 54 } 55 public void SayHi() 56 { 57 58 Console.WriteLine("大家好,我是{0},我是大大的{1}民,我今年{2}了...",this.Name,(IsGoodMan?"良":"***"),this.Age); 59 } 60 } 61 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace Demo001 7 { 8 /// <summary> 9 /// 狗狗类 10 /// </summary> 11 class Dog 12 { 13 public string name; 14 private int age; 15 private bool gender; 16 17 18 public Dog() 19 { 20 Console.WriteLine("构造函数被调用了."); 21 } 22 23 public Dog(string name,int age,bool gender) 24 { 25 //this代表当前对象 26 this.name = name; 27 this.age = age; 28 this.gender = gender; 29 } 30 31 } 32 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace Demo001 7 { 8 class Pig 9 { 10 public int Age { get; set; } 11 public string Name { get; set; } 12 13 //public Pig() 14 //{ 15 16 //} 17 public Pig(int age,string name) 18 { 19 this.Name = name; 20 this.Age = 2; 21 Shout(); 22 } 23 //在构造函数中是可以调用其他的普通成员方法的。。。 24 25 public void Shout() 26 { 27 Console.WriteLine("嘻嘻嘻........"); 28 } 29 //public Pig(int age) 30 //{ 31 // this.Age = age; 32 //} 33 } 34 }