標籤:new pre int main ons 實值型別 console over this
一、之實值型別與參考型別
public static void Main(string[] args) { int age = 10;//實值型別在記憶體中開闢一塊地區存放 object age1 = age;//參考型別存的是地址 此時指向age的初始地址 age += 1;//age重新開闢新的地區 此時為11, Console.WriteLine("fist="+age1);//指向老的地區 值為10; int age2=(int)age1;//參考型別轉化實值型別 為10; Console.WriteLine("test="+age2); age2 += 1; Console.WriteLine("secend="+age); Console.ReadKey(); }
輸出結果 10 10 11
二、之繼承重寫
public class Program { public static void Main(string[] args) { Employee yee = new Employee(); Console.ReadKey(); } } class Person { protected int age = 10; public Person() { this.Output(); } public virtual void Output() { Console.WriteLine("Person"+this.age); } } class Employee : Person { public Employee() : base() { base.age = 20; } public override void Output() { Console.WriteLine("Employee"+base.age); } }
輸出結果 10
C# 基礎篇·面試題