直接实例化使用
public class ABC
{
string this [ string key]
{
get { return "value" ; }
}
}
static class Program
{
public static ABC a= new ABC ( ) ;
static void Main ( string [ ] args)
{
var c = a[ "0" ] ;
}
}
子类实例化,使用父类属性
class Program
{
static void Main ( string [ ] args)
{
var test = new test_A ( ) ;
var a = test[ "0" ] ;
}
}
class test_A : ABC { }
class ABC
{
public string this [ string index] {
get
{
return "" ;
}
}
}
建造者模式示例
实例化后,直接接中括号填写参数
vehicle[ "frame" ] = "Car Frame"
class Vehicle
{
private string _vehicleType;
private Dictionary< string , string > _parts =
new Dictionary< string , string > ( ) ;
public Vehicle ( string vehicleType)
{
this . _vehicleType = vehicleType;
}
public string this [ string key]
{
get { return _parts[ key] ; }
set { _parts[ key] = value ; }
}
public void Show ( )
{
Debug. Log ( "\n---------------------------" ) ;
Debug. Log ( "Vehicle Type: " + _vehicleType) ;
Debug. Log ( " Frame : " + _parts[ "frame" ] ) ;
Debug. Log ( " Engine : " + _parts[ "engine" ] ) ;
Debug. Log ( " #Wheels: " + _parts[ "wheels" ] ) ;
Debug. Log ( " #Doors : " + _parts[ "doors" ] ) ;
}
}
class CarBuilder : VehicleBuilder
{
Vehicle vehicle;
public CarBuilder ( )
{
vehicle = new Vehicle ( "Car" ) ;
}
public override void BuildFrame ( )
{
vehicle[ "frame" ] = "Car Frame" ;
}
public override void BuildEngine ( )
{
vehicle[ "engine" ] = "2500 cc" ;
}
public override void BuildWheels ( )
{
vehicle[ "wheels" ] = "4" ;
}
public override void BuildDoors ( )
{
vehicle[ "doors" ] = "4" ;
}
}