C# 语法练习(11): 类[三] - 构造函数、析构函数、base、this
2009-02-23 08:16:51 来源:WEB开发网this:
using System;
class MyClass
{
private string fs = "ABC-";
public MyClass() { Console.WriteLine("MyClass"); }
public MyClass(string str) { Console.WriteLine(this.fs + str); }
public MyClass(int num) : this() { Console.WriteLine(num); }
public MyClass(int x, int y) : this("XYZ") { Console.WriteLine(x + y); }
}
class Program
{
static void Main()
{
MyClass c1 = new MyClass(); // MyClass
MyClass c2 = new MyClass("EFG"); // ABC-EFG
MyClass c3 = new MyClass(123); // MyClass / 123
MyClass c4 = new MyClass(111, 222); // ABC-XYZ / 333
Console.ReadKey();
}
}
构造函数、属性、base:
using System;
abstract class Parent
{
private byte FID;
public Parent(byte n)
{
FID = n;
}
public byte Id
{
get { return FID; }
set { FID = value; }
}
}
class Child : Parent
{
public Child(byte MyID) : base(MyID) { }
}
class Program
{
static void Main()
{
Child Rect = new Child(6);
Console.WriteLine(Rect.Id); //6
Rect.Id = 8;
Console.WriteLine(Rect.Id); //8
Console.ReadKey();
}
}
更多精彩
赞助商链接