隐式覆盖
指iso-c++的函数覆盖,不同的是必须在子类覆盖函数最后加上关键字override
如:
{}
隐藏覆盖
表示子类与父类同名的函数不再覆盖相应父类函数,而是新建这个函数。即与父类没有多态关系。
{
virtual void Print();
}
ref class ChildClass
{
virtual void Print() new;
}
ParentClass ^ pp = gcnew ChildClass;
pp -> Print();
调用的是父类的Print()。
显式覆盖
显式覆盖是C++/CLI的新特性,有如下3种性质:
1.可以覆盖不同名的父类函数
{
public:
virtual void Yip () = Dog::Speak
{
}
} ;
2.可以覆盖不是直接父类的函数
{
virtual void Speak(){};
}
ref class Cat: public Aniaml
{
virtual void Speak()overrid{};
}
ref class Tiger : public Cat
{
public:
virtual void Growl () = Animal::Speak
{
}
} ;
3.可以覆盖两个以上的函数
{
public:
virtual void Speak() override = Animal::Speak
{
Console::WriteLine("Tiger says Growl");
}
} ;
Tiger::Speak()的override关键字表示Tiger::Speak()覆盖新建的Cat::Speak(),
但是使用override必须保证覆盖函数和父类函数同名。
“=Animal::Speak”表示Tiger::Speak()还覆盖Animal::Speak()
也可以这样做:
{
public:
virtual void Growl() = Animal::Speak, Cat::Speak
{
Console::WriteLine("Tiger says Growl");
}
} ;
使用两个异名覆盖可以完成上述相同的功能,好处是无需和父类函数同名。
下面列出完整的测试代码:
using namespace System;
ref class Animal
{
public:
virtual void Speak ()
{Console::WriteLine("Animal is Mysteriously Silent");}
virtual void Eat()
{Console::WriteLine("Animal is Eating");}
} ;
ref class Cat : public Animal
{
public:
virtual void Speak() override
{Console::WriteLine("Cat says Meow");}
virtual void Eat() override
{Console::WriteLine("Cat is Eating");}
} ;
ref class Tiger : public Cat
{
public:
virtual void TigerEat()=Cat::Speak,Animal::Eat
{Console::WriteLine("Tiger is Speaking and Eating");}
virtual void Speak() override
{Console::WriteLine("Tiger says Grrrr");}
} ;
void main()
{
// Array of Animal handles
array<Animal^>^ animals = gcnew array<Animal^>
{
gcnew Animal(),
gcnew Cat(),
gcnew Tiger()
};
for each ( Animal ^a in animals)
{
a->Speak();
}
Console::WriteLine();
for each ( Animal ^a in animals)
{
a->Eat();
}
Tiger^ dd = gcnew Tiger;
dd->Speak();
Console::ReadLine();
}
最后的结果
Animal is Mysteriously Silent
Cat says Meow
Tiger is Speaking and Eating
Animal is Eating
Cat is Eating
Tiger is Speaking and Eating
Tiger is Speaking and Eating
很奇怪,第三个输出是Tiger is Speaking and Eating,而不是Tiger says Grrrr。
Tiger类使用了两个异名覆盖:Animal::Speak,Animal::Eat。同时也覆盖了Cat的方法Speak。
但是调用时,调用了对Animal的覆盖,没有调用对Cat的覆盖。是不是对基类的覆盖优先呢。暂时可以这么理解吧。