Java中的private函数是不能被继承的, 即基类中如果有一个private函数func(), 现在在子类中实现一个与其同名,参数列表也相同的func
()函数,这两个函数之间只是简单的覆盖关系,不能使用多态.
但是C++中可以被继承,使用多态性质.
class Base
{
private:
virtual void func()
{
cout<<"this is func() in Base."<<endl;
}
public:
Base(void);
virtual ~Base(void);
void callfunc()
{
cout<<"callfunc() in Base."<<endl;
func();
}
};
class derived1 :
public Base
{
private:
void func()
{
cout<<"this is func() in derived1."<<endl;
}
public:
derived1(void);
virtual ~derived1(void);
};
int main()
{
Base* one = new Base();
Base* second = new derived1();
one->callfunc();
second->callfunc();
delete one;
delete second;
}
打印结果:
callfunc() in Base.
this is func() in Base.
callfunc() in Base.
this is func() in derived1.