c#基础

属性

public abstract class Shape
{
private string myId;
public string Id
{
get {return myId;}
set { myId = value; }
}
  }

属性是 私有变量的 代言人存取器. 紧挨着变量声明就好

函数重载

public abstract class Shape
{
public abstract double Area
{
get; }
}

public class Squre : Shape
{
public override double Area
{
get{ return mySide * mySide;}}
}

索引器, 模板

class SampleCollection<T>
{
// Declare an array to store the data elements.
private T[] arr = new T[100];

// Define the indexer, which will allow client code
// to use [] notation on the class instance itself.
// (See line 2 of code in Main below.)
public T this[int i]
{
get
{
// This indexer is very simple, and just returns or sets
// the corresponding element from the internal array.
return arr[i];
}
set
{
arr[i]
= value;
}
}
}

// This class shows how client code uses the indexer.
class Program
{
static void Main(string[] args)
{
// Declare an instance of the SampleCollection type.
SampleCollection<string> stringCollection = new SampleCollection<string>();

// Use [] notation on the type.
stringCollection[0] = "Hello, World";
System.Console.WriteLine(stringCollection[
0]);
}
}

内容都是由变量存的, 任何行为都要定义存取。

<T>模板. 

public T this[int i]: 下标 返回 string, get{}set{}


posted on 2011-02-25 10:29  oleeceo  阅读(242)  评论(0)    收藏  举报

导航