在运行时更改继承类

大赏

我有两个带有这样的代码的类(仅是示例)

class parent1
   function f(param as integer) as integer
       return param +2
   end function
end class
'===============================
class parent2
  function f(param as integer) as integer
       return param *1
   end function
end class

然后我想根据运行时的情况创建一个引导类的继承。也许像这样:

class child
   sub new(s as string)
     'it will inherits parent1
   end sub
   sub new(i as integer)
     'it will inherits parent2
   end sub
end class

是否可以..?

穆库·瓦什尼(Mukul Varshney)

在.Net中不可能进行多重继承。现在,解决您的问题

  1. 尝试动态源代码生成和编译。您不能进行多重继承,但是在运行时确定必须继承哪种类型的类时,请使用来动态创建该类CodeDom参考msdn链接
  2. 这种方法可能适合您的需求,也可能不适合您。不过,如果您有更改设计的范围,请使用interfacesdependency injection(由于函数名是在两个类相同parent1,并parent2可以充分利用的polymorphism)。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章