base this 关键字
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Collections.Specialized;
using System.Threading;
using System.Reflection;
using System.Data;
namespace TestArray
{
class Program
{
static void Main(string[] args)
{
Audi audi = new Audi();
audi[1] = "A6";
audi[2] = "A8";
Console.WriteLine(audi[1]);
audi.Run();
audi.ShowResult();
Console.ReadLine();
}
}
public class Action
{
public static void ToRun(Vehicle vehicle)
{
Console.WriteLine("{0} is running.", vehicle.ToString());
}
}
public class Vehicle
{
private string name;
private int speed;
private string[] array = new string[10];
public Vehicle() { }
public Vehicle(string name, int speed)
{
this.name = name;
this.speed = speed;
}
public virtual void ShowResult()
{
Console.WriteLine("the top speed of {0} is {1}.", name, speed);
}
public void Run()
{
//传递当前实例参数
Action.ToRun(this);
}
public string this[int param]
{
get { return array[param]; }
set { array[param] = value; }
}
}
public class Car : Vehicle
{
//派生类和基类通信,以base实现,基类首先被调用
//指定创建派生类实例时应调用的基类构造函数
public Car() : base("Car", 200) { }
public Car(string name, int speed) : this() { }
public override void ShowResult()
{
//调用基类上已被其他方法重写的方法
base.ShowResult();
Console.WriteLine("It's a car's result.");
}
}
public class Audi : Car
{
public Audi()
: base("Audi", 300) { }
public Audi(string name, int speed)
: this()
{ }
public override void ShowResult()
{
//由三层继承可以看出,base只能继承起直接基类成员
base.ShowResult();
base.Run();
Console.WriteLine("It's audi's result.");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Collections.Specialized;
using System.Threading;
using System.Reflection;
using System.Data;
namespace TestArray
{
class Program
{
static void Main(string[] args)
{
Audi audi = new Audi();
audi[1] = "A6";
audi[2] = "A8";
Console.WriteLine(audi[1]);
audi.Run();
audi.ShowResult();
Console.ReadLine();
}
}
public class Action
{
public static void ToRun(Vehicle vehicle)
{
Console.WriteLine("{0} is running.", vehicle.ToString());
}
}
public class Vehicle
{
private string name;
private int speed;
private string[] array = new string[10];
public Vehicle() { }
public Vehicle(string name, int speed)
{
this.name = name;
this.speed = speed;
}
public virtual void ShowResult()
{
Console.WriteLine("the top speed of {0} is {1}.", name, speed);
}
public void Run()
{
//传递当前实例参数
Action.ToRun(this);
}
public string this[int param]
{
get { return array[param]; }
set { array[param] = value; }
}
}
public class Car : Vehicle
{
//派生类和基类通信,以base实现,基类首先被调用
//指定创建派生类实例时应调用的基类构造函数
public Car() : base("Car", 200) { }
public Car(string name, int speed) : this() { }
public override void ShowResult()
{
//调用基类上已被其他方法重写的方法
base.ShowResult();
Console.WriteLine("It's a car's result.");
}
}
public class Audi : Car
{
public Audi()
: base("Audi", 300) { }
public Audi(string name, int speed)
: this()
{ }
public override void ShowResult()
{
//由三层继承可以看出,base只能继承起直接基类成员
base.ShowResult();
base.Run();
Console.WriteLine("It's audi's result.");
}
}
}