Ruby的继承语法为<
例子:
#定义基类
class Base
def meth(info)
puts "This is Base #{info}"
end
end
#定义继承类
class Derived < Base
def meth(info)
puts "This is derived #{info}"
super
end
end
obj1 = Derived.new
obj1.meth("My CU Article")
运行的结果如下:
This is derived My CU Article
This is Base My CU Article
其顺序类似C++中析构函数的顺序
我们再来看看使用C++来实现
class Base
{
public:
/*无论我们是否在此处添加virtual变量,都不能同时调用父类和子类的完全相同的方法meth方法*/
virtual void meth(char* info)
{
cout<<"Base "< }
};
class Derived : public Base
{
public:
virtual void meth(char* info)
{
cout<<"Derived "< }
};
int _tmain(int argc, _TCHAR* argv[])
{
Base *d = new Derived();
d->meth("hello");
return 0;
}
阅读(637) | 评论(0) | 转发(0) |