简体   繁体   English

如何在具有不同类作为输入参数的抽象类中定义方法?

[英]How to define a method in abstract class with different classes as input parameter?

I have an abstract class which looks like this for example:我有一个看起来像这样的抽象类,例如:

public abstract class CPD 
{
     public double[,] Func(double[] data, double value, SomeClass item) 
     {
         // Some code
     }

     public abstract double[] Fun1(double x);
     public abstract void Fun2(double x);
    
}

And I also have 3 different classes, with different constructors, functions, and etc. I wanted to define somehow my method "Func" which is in code, to be able to use as an input parameter (instead of "SomeClass") one of those 3 different classes I have.而且我还有 3 个不同的类,具有不同的构造函数、函数等。我想以某种方式定义我的代码中的方法“Func”,以便能够用作输入参数(而不是“SomeClass”)之一我有这 3 个不同的课程。

So, whenever I call some of those 3 classes, I can override the methods "Fun1" and "Fun2", but I can use always in either of those 3 classes the Method "Func".所以,每当我调用这 3 个类中的一些时,我可以覆盖方法“Fun1”和“Fun2”,但我总是可以在这 3 个类中的任何一个中使用方法“Func”。

How could I do that?我怎么能那样做?

If you want to use just one class instead of three classes, you can create a base class or interface and put the same behaviour into this newly created abstract class.如果您只想使用一个类而不是三个类,您可以创建一个基类或接口,并将相同的行为放入这个新创建的抽象类中。 And then just call it in your Func method.然后在你的Func方法中调用它。

For example, we can create an abstract class Display :例如,我们可以创建一个抽象类Display

public abstract class Display 
{
    public abstract string Show();
}

And then it is necessary to create concrete implementations of this base class Display :然后有必要创建这个基类Display的具体实现:

public class SamsungDisplay : Display
{
    public override string Show()
    {
        return "I am Samsung";
    }
}

public abstract class SonyDisplay : Display
{
    public override string Show()
    {
        return "I am Sony";
    }
}

public abstract class DellDisplay : Display
{
    public override string Show()
    {
        return "I am Dell";
    }
}

and then we can use any derived class instead of base class:然后我们可以使用任何派生类而不是基类:

// Here you can pass SamsungDisplay, SonyDisplay or DellDisplay 
public void UseDisplay(Display display) 
{
    display.Show();
}

As others have said, you can use interfaces or a common base class to accomplish this.正如其他人所说,您可以使用接口或通用基类来完成此操作。 But for completeness: you can also use a generic:但为了完整性:您也可以使用泛型:

public class SomeClass
{
    private static readonly Random Random = new Random();

    public virtual double Get()
    {
        return Random.Next(100);
    }
}

public class Class1 : SomeClass { }

public class Class2 : SomeClass { }

public class Class3 : SomeClass { }

public abstract class CPD
{
    public double[,] Func<T>(double[] data, double value, T item)
    where T : SomeClass
    {
        var doubles = new double[3, 4];
        doubles[0, 0] = item.Get();
        return doubles;
    }

    public abstract double[] Fun1(double x);
    public abstract void Fun2(double x);
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM