using System;
class AClass
{
// 创建字段
// string name;
// public int Age = 10;
// static readonly int legs = 8, eyes = 2;
// 创建属性
// public att {
// get { return att; }
// set { att = value; }
// }
// 创建方法
// int Foo (int x) { return x * 2; }
// int Foo (int x) => x * 2;
// void Foo (int x) => Console.WriteLine (x);
// 创建重载方法
// void Foo (int x) {...}
// void Foo (double x) {...}
// void Foo (int x, float y) {...}
// void Foo (float x, int y) {...}
// 按值传递和按引用传递
// void Foo (int x) {...}
// void Foo (ref int x) {...} // OK so far
// void Foo (out int x) {...} // Compile-time error
// 创建构造器和重载构造器
// public AClass (int eyes) {
// this.eyes = eyes;
// }
// public AClass (string name, int Age, int legs, int eye) : this(eyes) {
// this.name = name;
// this.Age = Age;
// this.legs = legs;
// }
// // 创建解构器
// public void Deconstruct (string name, int Age, int legs, int eye) {
// name = this.name;
// Age = this.Age;
// legs = this.legs;
// eyes = this.eyes;
// }
}
// 继承
public class Asset
{
public string Name;
public virtual decimal Liability => 0;
}
public class Stock : Asset
{
public long SharesOwned;
}
public class House : Asset
{
public decimal Mortgage;
public override decimal Liability => Mortgage;
}
// 抽象类
public abstract class AAsset
{
// Note empty implementation
public abstract decimal NetValue { get; }
}
public class SSStock : AAsset
{
public long SharesOwned;
public decimal CurrentPrice;
// Override like a virtual method.
public override decimal NetValue => CurrentPrice * SharesOwned;
}
public class HelloWorld
{
public static void Display (Asset asset)
{
System.Console.WriteLine (asset.Name);
}
public static void Main()
{
// // 继承
// Stock msft = new Stock { Name="MSFT", SharesOwned=1000 };
// Console.WriteLine (msft.Name); // MSFT
// Console.WriteLine (msft.SharesOwned); // 1000
// House mansion = new House { Name="Mansion",Mortgage=250000 };
// Console.WriteLine (mansion.Name); // Mansion
// Console.WriteLine (mansion.Mortgage); // 250000
// // 多态
// Display(msft);
// Display(mansion);
// // 向上类型转换
// Stock msft = new Stock();
// Asset a = msft; // Upcast
// Console.WriteLine (a == msft); // True
// Console.WriteLine (a.Name); // OK
// // Console.WriteLine (a.SharesOwned); // Error: SharesOwned undefined
// // 向下类型转换
// Stock msft = new Stock();
// Asset a = msft; // Upcast
// Stock s = (Stock)a; // Downcast
// Console.WriteLine (s.SharesOwned); // <No error>
// Console.WriteLine (s == a); // True
// Console.WriteLine (s == msft); // True
// // 虚函数
// House mansion = new House { Name="McMansion", Mortgage=250000 };
// Asset a = mansion;
// Console.WriteLine (mansion.Liability); // 250000
// Console.WriteLine (a.Liability); // 250000
}
}