Partial Class中继承的替代方法是什么(因为已经存在) [英] What is an alternative to inheritance in Partial Class (because already exists)

查看:93
本文介绍了Partial Class中继承的替代方法是什么(因为已经存在)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设你有2个两个部分类。每个部分类中的一个派生自基类。 

Say you have 2 two partial classes. One of each of the partial classes derives from a base class. 

问题:需要让两个类具有通常用基类处理的相同属性。

Problem: need to have both classes have same properties that would normally be taken care of with base class.

partial class One: SomeBaseClass {}
partial class One {}
partial class Two: SomeOtherBaseClass {}
partial class Two{}
public class HasCommonNeededPropsAndFuncutions
{
  public string foo {get; set:}
  public string bar {get; set:}
}

如果部分类(没有分配基类)可以从HasCommon ...类继承,那将是很好但是不可能。

If the partial classes (without base classes assigned) could inherit from the HasCommon... class that would be great but it's not possible.

这里有什么好方法?在每个类中创建一个初始化HasCommonNeeded ...类的新实例的属性?

What would be a good solution here? Create a property in each class that initializes a new instance of the HasCommonNeeded... class?

所以...可能:

partial class One 
{
  public HasCommonNeededPropsAndFuncutions Prop1 {get; set;}
}

partial class Two
{
  public HasCommonNeededPropsAndFuncutions  Prop2 {get; set;}
}

推荐答案

为什么SomeBaseClass和SomeOtherBaseClass不能继承HasCommonNeededPropsAndFunctions?

Why can't SomeBaseClass and SomeOtherBaseClass inherit HasCommonNeededPropsAndFunctions?

如果他们不能,那么是的,包括一个实例两个可能是要走的路。如果HasCommonNeededPropsAndFunctions可以是静态的(如Math类),那么唯一可能出现的另一种可能性是,那么你不需要继承
或封装。

If they can't, then yes, including an instance in One and Two would probably be the way to go. The only other possibility that pops to mind would be if HasCommonNeededPropsAndFunctions could be static (like the Math class), then you wouldn't need to inherit or encapsulate.


这篇关于Partial Class中继承的替代方法是什么(因为已经存在)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆