Chinaunix首页 | 论坛 | 博客
  • 博客访问: 62316
  • 博文数量: 33
  • 博客积分: 841
  • 博客等级: 准尉
  • 技术积分: 340
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-20 20:54
文章分类

全部博文(33)

文章存档

2011年(33)

分类: Python/Ruby

2011-02-18 14:47:13

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;
}
 
阅读(598) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:Ruby学习之特殊方法

给主人留下些什么吧!~~