Constructor总结
生活随笔
收集整理的這篇文章主要介紹了
Constructor总结
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一個類如果沒有構造那么系統為我們在背后創建一個0參數的構造,但是一旦我們創建了但參數的構造,那么默認的構造就沒了。
View Code 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace ConsoleApplication4 7 { 8 class Person 9 { 10 private string _name; 11 public Person(string name) 12 { 13 _name = name; 14 } 15 public void Display() 16 { 17 Console.WriteLine("Name: {0}", _name); 18 } 19 } 20 class Employee : Person 21 { 22 public Employee(string name) : base(name) 23 { 24 25 } 26 } 27 class Program 28 { 29 30 static void Main(string[] args) 31 { 32 Employee emp = new Employee("Shawn"); 33 emp.Display(); 34 Console.ReadKey(); 35 } 36 } 37 }構造函數不像方法或者property可以向繼承它的類傳遞,所以base class有構造的話,記得繼承類要調用base
轉載于:https://www.cnblogs.com/shawnzxx/archive/2013/04/23/3038998.html
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的Constructor总结的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Eclipse for android
- 下一篇: 平果6s手机多少钱啊?