1.使用基类的引用
派生类的实例由基类的实例加上派生类新增的成员组成。派生类的引用指向整个类对象,包括基类部分。
如果有一个派生类对象的引用,就可以获取该对象基类部分的引用(使用类型转换运算符)
MyDerivedClass derived=new MyDerivedClass();//创建一个对象
MyBaseClass mybc=(MyBaseClass)derived;//转换引用
==派生类的引用可以看到完整的MyDerivedClass对象,而mybc只能看到对象的MyBaseClass部分!==
2.虚方法和覆写方法
我们知道当使用基类引用访问派生类对象时,得到的是基类的成员虚方法可以使基类的引用访问”升至“派生类内
可以使用基类引用调用派生类的方法,只需满足以下条件:
1. 派生类的方法和基类的方法有相同的签名和返回类型
2. ==基类==的方法用virtual标注
3. ==派生类==的方法使用override标注
class MyBaseClass
{
virtual public void Print()
{Console.WriteLine("This is the base class.")}
}
class MyDerivedClass:MyBaseClass
{
override public void Print()
{Console.WriteLine("This is the derived class.")}
}
class Program
{
static void Main()
{
MyDerivedClass derived = new MyDerivedClass();
MyBaseClass mybc=(MyBaseClass)derived;
derived.Print();
mybc.Print();
}
}
输出:This is the derived class.
This is the derived class.
==当使用基类引用(mybc)调用Print方法时,方法调用被传递到派生类并执行。==
- 覆写与被覆写的方法必须有相同的可访问性,即不能一个为public一个为private
- 不能覆写static方法或非虚方法
- 方法,属性和所引起,以及事件,都可以被声明为virtual和override
覆写标记为override的方法
覆写方法可以在继承的任何层次出现,也就是不管你继承了几次
- 当使用对象积累部分的引用调用一个覆写的方法时,方法的调用被沿派生层次上溯执行,一直到标记为override的方法的最高派生(most-derived)版本
- 如果在更高的派生级别有该方法的其他声明,但没有被标记为override,那么他们不会被调用。
class MyBaseClass
{
virtual public void Print()
{Console.WriteLine("This is the base class.")}
}
class MyDerivedClass:MyBaseClass
{
override public void Print()
{Console.WriteLine("This is the derived class.")}
}
class SecondDerived:MyDerivedClass
{
下面分情况讨论
}
1. 使用override声明Print
如果把SecondDerived的Print方法声明为override, 那么它会覆写全部两个低派生级别的版本
class SecondDerivedClass:MyDerivedClass
{
override public void Print()
{Console.WriteLine("This is the second derived class.")}
}
class Program
{
static void Main()
{
SecondDerivedClass derived = new SecondDerivedClass();
MyBaseClass mybc=(MyBaseClass)derived;
derived.Print();
mybc.Print();//调用被沿着继承层次向上传递
}
}
输出:This is the second derived class.
This is the second derived class.
2.使用new声明Print
class SecondDerivedClass:MyDerivedClass
{
new public void Print()
{Console.WriteLine("This is the second derived class.")}
}
class Program
{
static void Main()
{
SecondDerivedClass derived = new SecondDerivedClass();
MyBaseClass mybc=(MyBaseClass)derived;
derived.Print();
mybc.Print();//调用被沿着继承层次向上传递只传递了一级
}
}
输出:This is the second derived class.
This is the derived class.
覆盖其他成员类型
我们已经学习了如何在方法上使用virtual/override,其实在属性事件以及所引起上也是一样的,下面的代码演示了名为MyProperty的只读属性,其中使用了virtual/override.
class MyBaseClass
{
private int _myInt=5;
virtual public int MyProperty
{
get{return _myInt;}
}
}
class MyDerivedClass:MyBaseClass
{
private int _myInt=10;
override public int MyProperty
{
get{return _myInt;}
}
}
class Program
{
static void Main()
{
MyDerivedClass derived = new MyDerivedClass();
MyBaseClass mybc=(MyBaseClass)derived;
Console.WriteLine(derived.MyProperty);
Console.WriteLine(mybc.MyProperty);
}
}
输出:10
10