简体   繁体   English

接口方法的签名包括一个class,它实现了接口本身

[英]Signature of interface's method includes a class, which implements the interface itself

Considering the rules of good OOP and design, is it correct to write a method in an interface which has as input types or output types classes which implements the interface?考虑到良好的 OOP 和设计的规则,在具有作为输入类型的接口或实现该接口的 output 类型类的接口中编写方法是否正确?

I give you an example of a very simple exercise I must do, regarding complex numbers:我给你一个关于复数我必须做的非常简单的练习的例子:

public interface IComplex
{
    double GetImaginary();

    double GetReal();
    
    ComplexImpl Conjugate();

    ComplexImpl Add(ComplexImpl y);

}

/*----------------------------------------------------*/


public class ComplexImpl : IComplex
    {
    private double real;

    private double imaginary;

    public ComplexImpl(double real, double imaginary) { this.real = real; this.imaginary = imaginary; }

    public double GetReal() { return this.real; }

    public double GetImaginary() { return this.imaginary; }

    public ComplexImpl Conjugate()
    {
        ComplexImpl conjugate = new ComplexImpl(this.real, -this.imaginary);
        return conjugate;
    }

    public ComplexImpl Add(ComplexImpl y)
    {
        ComplexImpl result = new ComplexImpl(this.real + y.real, this.imaginary + y.imaginary);
        return result;

    }

}

Considering the interface IComplex : is it correct to write the methods Conjugate and Add such that they have as input (and/or output) objects which are instantiations of the class ComplexImpl?考虑IComplex接口:编写ConjugateAdd方法是否正确,以便它们具有作为 class ComplexImpl 实例化的输入(和/或输出)对象?

Consider that the class ComplexImpl implements the interface when this methods are defined.考虑到 class ComplexImpl 在定义此方法时实现接口。

EDIT :编辑

For those of you who answer: first of all thank you, however I have the following problem.对于那些回答的人:首先谢谢你,但是我有以下问题。 If I substitute "ComplexImpl" with "IComplex" both in the interface and in the class, I obtain this error in the method Add :如果我在接口和 class 中将“ComplexImpl”替换为“IComplex”,则会在Add方法中出现此错误:

" 'IComplex' does not contain a definition for 'imaginary' and no accessible extension method 'imaginary' accepting a first argument of type 'IComplex' could be found ". ‘IComplex’不包含‘imaginary’的定义,并且找不到接受‘IComplex’类型的第一个参数的可访问扩展方法‘imaginary’ ”。

The only way to solve this is through the use of Generics?解决这个问题的唯一方法是通过使用 Generics?

I suggest you use the interface IComplex instead of ComplexImpl in the interface.我建议你在接口中使用接口 IComplex 而不是 ComplexImpl。 This will still yield the desired results without relying on an implementation of the interface.这仍然会产生所需的结果,而不依赖于接口的实现。

public interface IComplex
{
    double GetImaginary();

    double GetReal();
    
    IComplex Conjugate();

    IComplex Add(IComplex y);

}

This will be your concrete type implementation:这将是您的具体类型实现:


        public class ComplexImpl : IComplex
        {
            private double real;

            private double imaginary;

            public ComplexImpl(double real, double imaginary) { this.real = real; this.imaginary = imaginary; }

            public double GetReal() { return this.real; }

            public double GetImaginary() { return this.imaginary; }

            public IComplex Conjugate()
            {
                ComplexImpl conjugate = new ComplexImpl(this.real, -this.imaginary);
                return conjugate;
            }

            public IComplex Add(IComplex y)
            {
                ComplexImpl result = new ComplexImpl(this.real + y.GetReal(), this.imaginary + y.GetImaginary());
                return result;

            }

        }

Note that you cannot access private members when referring to an interface.请注意,引用接口时不能访问私有成员。 All interface definitions are public .所有接口定义都是public的。 Therefore you cannot use y.imaginary for example but since the interface defines an accessor for this private field GetImaginary() , you can use that.因此,您不能使用y.imaginary例如,但由于接口为此私有字段GetImaginary()定义了一个访问器,您可以使用它。

This thing is breaking the reason to use interfaces: To abstract implemntations.这件事打破了使用接口的原因:抽象实现。 An easy and bearable fix to this would be:一个简单且可以忍受的解决方法是:

public interface IComplex
{
   double GetImaginary();

   double GetReal();

   IComplex Conjugate();

   IComplex Add(IComplex y);

}

This way an implmentation is returned, but the interface stays clean and has no knowledge about an implementation.这种方式返回一个实现,但接口保持干净并且不了解实现。

You can make the interface or methods generic, constrained over your interface.您可以使接口或方法成为通用的,约束在您的接口上。

Choice 1选择 1

public interface IComplex<TComplex>
    where TComplex : IComplex
{
    double GetImaginary();

    double GetReal();
    
    TComplex Conjugate();

    TComplex Add(TComplex y);
}

Choice 2选择 2

public interface IComplex
{
    double GetImaginary();

    double GetReal();
    
    TComplex Conjugate<TComplex>() where TComplex : IComplex;

    TComplext Add<TComplex>(TComplex y) where TComplex : IComplex;
}

I think a better design would be to use IComplex in the method signatures in the interface:我认为更好的设计是在接口的方法签名中使用IComplex

public interface IComplex
{   
    double GetImaginary();

    double GetReal();


    // Changed:
    
    IComplex Conjugate();

    IComplex Add(IComplex y);
}

and call .GetReal() and .GetImaginary() (rather than getting the real and imaginary values directly) inside both Conjuate() and Add() :并在 Conjuate( Conjuate()和 Add() 中调用.GetReal().GetImaginary() (而不是直接获取realimaginary Add()

public class ComplexImpl : IComplex
{
    private double real;

    private double imaginary;

    public ComplexImpl(double real, double imaginary)
    {
        this.real = real;
        this.imaginary = imaginary;
    }

    public double GetReal() { return this.real; }

    public double GetImaginary() { return this.imaginary; }


    // Changed:

    public IComplex Conjugate()
    {
        ComplexImpl conjugate = new ComplexImpl(this.GetReal(), -this.GetImaginary());

        return conjugate;
    }

    public IComplex Add(IComplex y)
    {
        ComplexImpl result = new ComplexImpl(
            this.GetReal() + y.GetReal(), this.GetImaginary() + y.GetImaginary());

        return result;
    }
}

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

相关问题 tclass扩展了一个抽象类,并使用相同的签名方法实现了接口 - tclass extends an abstract class and implements interface with same signature method 取实现接口的类 - Take class which implements interface 接口方法返回类型是实现接口的类 - Interface method return type to be class that implements the interface 从它实现的接口创建 class 的实例 - Create Instance of a class from interface which it implements 通过接口方法实现接口的类的汇编版本 - Getting Assembly Version of a class that implements interface through interface method 这种设计模式是否有名称,其中具体类实现了一个特定接口,该接口实现了 CRUD 操作的基本接口? - Is there a name for this design pattern where a concrete class implements a specific interface which implements a base interface for CRUD operations? 从抽象类继承并实现接口的模拟类 - Mock class which inherit from abstract class and implements interface 如何处理实现接口并具有基类的类? - How to approach a class which implements an interface and has a base class? 在c#接口和接口的方法签名之一中实现枚举 - Implement enum in c# Interface and in one of interface's method signature 如何协变/逆变重写从接口实现的虚方法? - How to covariant/contravariant in override virtual method which implements from interface?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM