【例题】虚拟化子类

  勇芳 2017-3-31 6940

'' 所有方法(除了构造函数)都可以是虚拟的,
'' 允许它们在运行时被子类重写
''

'' 基类
type ClassA extends object
	declare virtual sub hello( )
end type

sub ClassA.hello( )
	print "hello from A"
end sub

'' 子类
type ClassB extends ClassA
	declare sub hello( )
end type

sub ClassB.hello( )
	print "hi from B"
end sub

'' 测试仪,可以给任何类对象的子类
sub test( byval p as ClassA ptr )
	p->hello( )
end sub

	var a = new ClassA
	var b = new ClassB

	test( a )
	test( b )

	delete b
	delete a

建议虚拟的少些,一般都是用抽象的

抽象允许占坑不拉屎,虚的话,必须有一个值,一般来说,用来给基础类预留接口

比如定义一个空的类,这个类会被三个接口相同但功能不同的东西集成,就用抽象把接口预留
例如 UI、游戏对象
很常用,也可以用来处理事件
把事件留作抽象函数,定义新空间写一个实现就行了这样


因国家互联网安全管理要求,关闭回帖功能。大家需要留言,请使用【勇芳软件客服】即时联系勇芳点此打开->>勇芳软件客服
返回
联系勇芳
发新帖 搜索 反馈 回顶部