简体   繁体   English

当所有子类都具有相同的基类(包括新类)时,如何继承多个子类?

[英]How to inherit multiple partial classes when all have the same base class (including the new class)?

So I'm not very good with OOP, and am a little stumped. 所以我对OOP不太好,有些困惑。 If I already have classes that are FruitHandlers from the FruitHandler library that look something like this: 如果我已经有来自FruitHandler库的FruitHandlers类,则看起来像这样:

public partial class AppleCleaner : FruitHandler {

    public AppleCleaner(int fruitSize) {
        CleanApple(fruitSize);
    }

    void CleanApple(int size) {
        //code to clean an apple;
    }
}

public partial class PearCleaner : FruitHandler {

    public PearCleaner(int fruitSize) {
        CleanPear(fruitSize);
    }

    void CleanPear(int size) {
        //code to clean a pear;
    }
}

public partial class BananaCleaner : FruitHandler {

    public BananaCleaner(int fruitSize) {
        CleanBanana(fruitSize);
    }

    void CleanBanana(int size) {
        //code to clean a banana;
    }
}

and I want to make a class that also has the base class FruitHandler, but is capable of using CleanApple, CleanPear, or CleanBanana that looks something like this: 我想创建一个类,该类也具有FruitHandler基类,但能够使用看起来像这样的CleanApple,CleanPear或CleanBanana:

public partial class FruitEater : FruitHandler {

    public FruitEater(Fruit fruit) {
        if (fruit.Name == "Apple") {
            CleanApple(fruit.size);
        } else if (fruit.Name == "Pear") {
            CleanPear(fruit.size);
        } else if (fruit.Name == "Banana") {
            CleanBanana(fruit.size);
        }

        EatFruit(fruit);
    }

    void EatFruit(Fruit fruit) {
        // eat it;
    }
}

I can refactor these pretty liberally, but the caveat is that the base class for all of them must be a FruitHandler (because in real life these are all Forms and the base class must be Form). 我可以很自由地重构它们,但需要注意的是,所有它们的基类必须是一个FruitHandler(因为在现实生活中,这些都是Forms,而基类必须是Form)。

You could do something like this: 您可以执行以下操作:

interface IFruitCleaner {
    void Clean(int size)
}

public partial class AppleCleaner : FruitHandler, IFruitCleaner {

    public AppleCleaner(int fruitSize) {
        Clean(fruitSize);
    }

    void Clean(int size) {
        //code to clean an apple;
    }
}


public partial class FruitEater : FruitHandler {

    public FruitEater(Fruit fruit, IFruitCleaner cleaner) {
        cleaner.Clean(fruit.size);
        EatFruit(fruit);
    }

    void EatFruit(Fruit fruit) {
        // eat it;
    }
}

Seems like you would want to pass the fruit to AppleCleaner as opposed to just the size, but I've left it as you have it. 似乎您想将水果传递给AppleCleaner ,而不只是大小,但我将其保留给您。

You should use interfaces for this. 您应该为此使用接口。 Unless each class is going to execute code in their base class (which isn't the case here), you should use an interface to define the common methods across the classes: 除非每个类都将在其基类中执行代码(在这里不是这种情况),否则应使用接口定义各类的通用方法:

public interface IFruit {
    void Clean(int Size);
}

public partial class AppleCleaner : IFruit, FruitHandler {

    public AppleCleaner(int fruitSize) {
        Clean(fruitSize);
    }

    void Clean(int size) {
        //code to clean an apple;
    }
}

public partial class PearCleaner : IFruit, FruitHandler {

    public PearCleaner(int fruitSize) {
        Clean(fruitSize);
    }

    void Clean(int size) {
        //code to clean a pear;
    }
}

public partial class BananaCleaner : IFruit, FruitHandler {

    public BananaCleaner(int fruitSize) {
        Clean(fruitSize);
    }

    void Clean(int size) {
        //code to clean a banana;
    }
}

This also simplifies your FruitEater class quite a bit: 这也大大简化了FruitEater类:

public partial class FruitEater : FruitHandler {

    public FruitEater(IFruit fruit, int size) {
        fruit.Clean(size);
        EatFruit(fruit);
    }

    void EatFruit(IFruit fruit) {
        // eat it;
    }
}

Normally you'd put clean in the base class, but as you said it was a Form in real world example, you could use an interface. 通常,您会把基类放在干净的位置,但是正如您在实际示例中所说的那样,它可以使用接口。

public interface IFruitCleaner  {
   void Clean(int size);
}

public partial class AppleCleaner : FruitHandler, IFruitCleaner
{

    public AppleCleaner(int fruitSize)
    {
        Clean(fruitSize);
    }

    void Clean(int size)
    {
        //code to clean an apple;
    }
}

public partial class PearCleaner : FruitHandler, IFruitCleaner
{

    public PearCleaner(int fruitSize)
    {
        Clean(fruitSize);
    }

    void Clean(int size)
    {
        //code to clean a pear;
    }
}


public partial class BananaCleaner : FruitHandler, IFruitCleaner
{

    public BananaCleaner(int fruitSize)
    {
        Clean(fruitSize);
    }

    void Clean(int size)
    {
        //code to clean a banana;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果部分类继承自某个类,那么具有相同名称的所有其他部分类也应该继承相同的基类? - If a partial class inherits from a class then all other partial classes with the same name should also inherit the same base class? 在基类上向构造函数添加新参数意味着我必须重构所有从其继承的类? - Adding a new parameter to constructor on the base class means I have to refactor all of the classes that inherit from it? 如何让不同的部分类使用 Base Class 来覆盖相同的部分方法? - How to have Different Partial classes use a Base Class to override same partial Method? 如何编写通用方法来复制均从同一抽象基类继承的类的实例? - How do I write a general method to copy instances of classes that all inherit from the same abstract base class? 如何从基类继承 - How to inherit from base class C#-如何使以下类成为基类,以便子类可以具有相同的属性和方法? - C# - How to make the following class a base class so that child classes can have the same properties, methods? 如何跨多个注入服务继承 DatabaseContext 作为基础 class - How to inherit DatabaseContext as a base class across multiple injected services 同时继承基类和接口 - Inherit a base class and an interface same time 两个类同名时如何设置默认类 - How to set the default class when two classes have the same name 从基类继承所有IEnumerator - Inherit ALL IEnumerator from base class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM