[英]tclass extends an abstract class and implements interface with same signature method
[英]Signature of interface's method includes a class, which implements the interface itself
考慮到良好的 OOP 和設計的規則,在具有作為輸入類型的接口或實現該接口的 output 類型類的接口中編寫方法是否正確?
我給你一個關於復數我必須做的非常簡單的練習的例子:
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;
}
}
考慮IComplex接口:編寫Conjugate和Add方法是否正確,以便它們具有作為 class ComplexImpl 實例化的輸入(和/或輸出)對象?
考慮到 class ComplexImpl 在定義此方法時實現接口。
編輯:
對於那些回答的人:首先謝謝你,但是我有以下問題。 如果我在接口和 class 中將“ComplexImpl”替換為“IComplex”,則會在Add方法中出現此錯誤:
“ ‘IComplex’不包含‘imaginary’的定義,並且找不到接受‘IComplex’類型的第一個參數的可訪問擴展方法‘imaginary’ ”。
解決這個問題的唯一方法是通過使用 Generics?
我建議你在接口中使用接口 IComplex 而不是 ComplexImpl。 這仍然會產生所需的結果,而不依賴於接口的實現。
public interface IComplex
{
double GetImaginary();
double GetReal();
IComplex Conjugate();
IComplex Add(IComplex 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 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;
}
}
請注意,引用接口時不能訪問私有成員。 所有接口定義都是public
的。 因此,您不能使用y.imaginary
例如,但由於接口為此私有字段GetImaginary()
定義了一個訪問器,您可以使用它。
這件事打破了使用接口的原因:抽象實現。 一個簡單且可以忍受的解決方法是:
public interface IComplex
{
double GetImaginary();
double GetReal();
IComplex Conjugate();
IComplex Add(IComplex y);
}
這種方式返回一個實現,但接口保持干凈並且不了解實現。
您可以使接口或方法成為通用的,約束在您的接口上。
選擇 1
public interface IComplex<TComplex>
where TComplex : IComplex
{
double GetImaginary();
double GetReal();
TComplex Conjugate();
TComplex Add(TComplex y);
}
選擇 2
public interface IComplex
{
double GetImaginary();
double GetReal();
TComplex Conjugate<TComplex>() where TComplex : IComplex;
TComplext Add<TComplex>(TComplex y) where TComplex : IComplex;
}
我認為更好的設計是在接口的方法簽名中使用IComplex
:
public interface IComplex
{
double GetImaginary();
double GetReal();
// Changed:
IComplex Conjugate();
IComplex Add(IComplex y);
}
並在 Conjuate( Conjuate()
和 Add() 中調用.GetReal()
和.GetImaginary()
(而不是直接獲取real
和imaginary
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.