using System;
using System.Collections.Generic;
using System.Text;
namespace myCar
{
public class Car
{
public Car()
{
}
internal protected int Odometer = 0;
public virtual void MoveForward()
{
Console.WriteLine("Moving Forwards");
Odometer += 2;
Console.WriteLine("Odometer reading :{0}", Odometer);
}
} // 如果Car 的修饰符是internal的话 那么RacingCar的定义必须在这个程序集里面
public class RacingCar : Car
{
public override void MoveForward()
{
Console.WriteLine("Moving Dangerously fast forwards");
Odometer += 30;
Console.WriteLine("Odometer in racing car:{0}", Odometer);
}
}
public static class Program
{
public static void Main()
{
Car myCar = new Car();
myCar.MoveForward();
RacingCar myRacingCar = new RacingCar();
myRacingCar.MoveForward();
}
}
}
编译没有问题
如果Car的修饰符是internal
编译有问题,问题处在RacingCar不可以继承Car,代码必须这样修改过可以编译
using System;
using System.Collections.Generic;
using System.Text;
namespace myCar
{
public class Car
{
public Car()
{
}
internal protected int Odometer = 0;
public virtual void MoveForward()
{
Console.WriteLine("Moving Forwards");
Odometer += 2;
Console.WriteLine("Odometer reading :{0}", Odometer);
}
public class RacingCar : Car
{
public override void MoveForward()
{
Console.WriteLine("Moving Dangerously fast forwards");
Odometer += 30;
Console.WriteLine("Odometer in racing car:{0}", Odometer);
}
}
}
public static class Program
{
public static void Main()
{
Car myCar = new Car();
myCar.MoveForward();
Car.RacingCar myRacingCar = new Car.RacingCar(); //Car.RacingCar myRacingCar = new Car.RacingCar(); 是编译不成功
myRacingCar.MoveForward();
}
}
}