如何扩展 MembershipUser 类而不使用多重继承?
在顶层我有一个 Person 类。接下来我希望 .NET 的 MembershipUser 类继承它,因为成员是一个人。但是,我想扩展 MembershipUser 类,这意味着,我认为,我需要创建自己的 MembershipUser 类,该类继承自 MembershipUser,然后添加我自己的属性。但是一旦我这样做了,由于多重继承规则(ASP.NET 2.0),我就不能再从 Person 继承。
At the top level I have a Person class. Next I want .NET's MembershipUser class to inherit from it, because a member is a person. However I want to extend the MembershipUser class which means, I think, I need to create my OWN MembershipUser class which inherits from MembershipUser and then adds my own properties. But once I do that, I can no longer inherit from Person due to multiple inheritance rules (ASP.NET 2.0).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过创建一个接口
IPerson
并将具体的Person
类替换为继承自MembershipUser
并实现IPerson 的类来解决此问题
。您还可以保留具体的
Person
,创建IPerson
并让您自己的类封装Person
实例并从MembershipUser
继承> 在实现IPerson
时。无论哪种情况,您曾经使用过具体
Person
类型的任何地方都应该替换为IPerson
(例如方法参数)。或者,您可以继续使用 Person 并让它封装一个 MembershipUser 实例(作为构造函数的一部分),并在需要时将 Person 显式转换为 MembershipUser...
我会自己使用接口实现解决方案。
You can solve this by creating an interface,
IPerson
and replacing your concretePerson
class with a class which inherits fromMembershipUser
and implementsIPerson
.You could also keep your concrete
Person
, createIPerson
and have your own class encapsulate aPerson
instance and inherit fromMembershipUser
while implementingIPerson
.In either case, anywhere where you once used a concrete
Person
type you should replace withIPerson
(such as method arguments).Alternatively you could continue using Person and have it encapsulate a MembershipUser instance (as part of a constructor) and include an explicit cast for Person to MembershipUser when needed...
I would go with the interface implementation solution myself.