WEB开发网
开发学院软件开发C语言 C# 语法练习(11): 类[三] - 构造函数、析构函数、... 阅读

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(s

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();
  }
}

上一页  1 2 3 4 

Tags:语法 练习 构造

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接