如何在 C# 中使用派生返回类型重写方法

本文关键字:派生 返回类型 重写 方法 | 更新日期: 2023-09-27 17:49:35

我想用派生类类型覆盖虚拟方法。 目前最好的方法是什么? 到目前为止,我发现了两种方法:

  1. 对每个派生类型使用abstractclass;使用protected方法进行桥接。
  2. protected实现与public访问器配合使用。

基本情况(未实现解决方案,Clone始终返回基本类型 A1(:

    public class A1
    {
        public int X1 { get; set; }
        public A1(int x1) { this.X1 = x1; }
        public virtual A1 Clone() { return new A1(X1); }
    }
    public class A2 : A1
    {
        public int X2 { get; set; }
        public A2(int x1, int x2) : base(x1)  { this.X2 = x2; }
        public override A1 Clone() { return new A2(X1, X2); }  //can't explicitly return A2
    }
    public class A3 : A2
    {
        public int X3 { get; set; }
        public A3(int x1, int x2, int x3) : base(x1, x2) { this.X3 = x3; }
        public override A1 Clone() { return new A3(X1, X2, X3); }  //can't explicitly return A3
    }

解决方案 #1(对每个派生类型使用abstract基类,并带有protected桥(:

    public class B1
    {
        public int X1 { get; set; }
        public B1(int x1) { this.X1 = x1; }
        public virtual B1 Clone() { return new B1(X1); }
    }
    public abstract class B2_Base : B1
    {
        public B2_Base(int x1) : base(x1) { }
        public sealed override B1 Clone() { return this.CloneAsB1(); }
        protected abstract B1 CloneAsB1();
    }
    public class B2 : B2_Base
    {
        public int X2 { get; set; }
        public B2(int x1, int x2) : base(x1) { this.X2 = x2; }
        protected sealed override B1 CloneAsB1() { return this.Clone(); }
        public new virtual B2 Clone() { return new B2(X1, X2); }  //CAN explicitly return B2
    }
    public abstract class B3_Base : B2
    {
        public B3_Base(int x1, int x2) : base(x1, x2) { }
        public sealed override B2 Clone() { return this.CloneAsB2(); }
        protected abstract B2 CloneAsB2();
    }
    public class B3 : B3_Base
    {
        public int X3 { get; set; }
        public B3(int x1, int x2, int x3) : base(x1, x2) { this.X3 = x3; }
        protected sealed override B2 CloneAsB2() { return this.Clone(); }
        public new virtual B3 Clone() { return new B3(X1, X2, X3); }  //CAN explicitly return B3
    }

解决方案 #2(将protected实现与public访问器一起使用(:

    public class C1
    {
        public int X1 { get; set; }
        public C1(int x1) { this.X1 = x1; }
        public C1 Clone() { return this.CloneImplementation(); }
        protected virtual C1 CloneImplementation() { return new C1(X1); }
    }
    public class C2 : C1
    {
        public int X2 { get; set; }
        public C2(int x1, int x2) : base(x1) { this.X2 = x2; }
        public new C2 Clone() { return this.CloneImplementation() as C2; }  //trusts CloneImplementation to return a C2
        protected override C1 CloneImplementation() { return new C2(X1, X2); }
    }
    public class C3 : C2
    {
        public int X3 { get; set; }
        public C3(int x1, int x2, int x3) : base(x1, x2) { this.X3 = x3; }
        public new C3 Clone() { return this.CloneImplementation() as C3; }  //trusts CloneImplementation to return a C3
        protected override C1 CloneImplementation() { return new C3(X1, X2, X3); }
    }

据我所知,解决方案 #1 是最严格的方法,但它需要为每个派生class提供一个abstractclass,该想要替换基class的返回类型。

解决方案#2更简单,更容易理解,但它在内部类型安全性方面有一点突破。 具体而言,每个派生类型的public访问器相信其protected方法将返回正确的类型。 因此,可以断开内部类型,例如:

    public class C2 : C1
    {
        public int X2 { get; set; }
        public C2(int x1, int x2) : base(x1) { this.X2 = x2; }
        public new C2 Clone() { return this.CloneImplementation() as C2; }  //trusts CloneImplementation to return a C2
        protected override C1 CloneImplementation() { return new C1(X1); }
    }

对于使用派生类型重写方法,是否有正确的(普遍接受的(最佳做法?

如何在 C# 中使用派生返回类型重写方法

你可以使基类成为泛型:

public abstract class Base<TDerived> where TDerived : Base {
  public abstract TDerived Clone();
}
public class Derived1 : Base<Derived1> {
  public override Derived1 Clone() { ... }
}
public class Derived2 : Base<Derived2> {
  public override Derived2 Clone() { ... }
}

然而,这让我想知道拥有一个公共基类有多大用处。也许 Derived1 和 Derived2 的克隆实现不需要成为公共接口的一部分。

无论如何,new 关键字都会隐式"覆盖"基本功能。除非出于某种原因您特别希望override出现在代码中,否则单个new修饰符就足够了。我还将探索将克隆功能简化到接口中,它允许您在以后在代码中做出更多假设。

public interface ICloneable<out T>
{
    T Clone();
}
public class A1 : ICloneable<A1>
{
    public int X1 { get; set; }
    public A1(int x1) { this.X1 = x1; }
    public virtual A1 Clone()
    {
        return new A1(X1);
    }
}
public class A2 : A1, ICloneable<A2>
{
    public int X2 { get; set; }
    public A2(int x1, int x2)
        : base(x1)
    {
        this.X2 = x2;
    }
    public virtual new A2 Clone()
    {
        return new A2(X1, X2);
    }
}
public class A3 : A2, ICloneable<A3>
{
    public int X3 { get; set; }
    public A3(int x1, int x2, int x3)
        : base(x1, x2)
    {
        this.X3 = x3;
    }
    public virtual new A3 Clone()
    {
        return new A3(X1, X2, X3);
    }
}

编辑:由此产生的可能行为:

public class A4 : A3, ICloneable<A4>
{
    public int X4 { get; set; }
    public A4(int x1, int x2, int x3, int x4)
        : base(x1, x2, x3)
    {
        this.X4 = x4;
    }
    public override A3 Clone()
    {
        return ((ICloneable<A4>)this).Clone();
    }
    A4 ICloneable<A4>.Clone()
    {
        return new A4(X1, X2, X3, X4);
    }
}

我建议不要这样做。 只需坚持使用此类内容的标准接口和模式即可。 实施系统。

http://msdn.microsoft.com/en-us/library/system.icloneable(v=vs.110(.aspx

Object Clone()

简单不是吗?

如果你必须偏离,我会像安德鲁·凯南(Andrew Kennan(建议的那样使用泛型。但是,我仍然会实现System.ICloneable,因为它使该类与其他框架更具互操作性。

此外,ICloneable应使用受保护的构造函数实现,例如

public class A1 : ICloneable
{
    public A1(int x1) { this.X1 = x1; }
    protected A1(A1 copy) { this.X1 = copy.X1; }
    public int X1 { get; set; }
    public virtual object Clone()
    {
        return new A1(this); // Protected copy constructor
    }
}

这样你就可以继承A1了...

public class B1 : A1, ICloneable
{
    public B1(int x1, int y1) : base(x1) { this.Y1 = y1; }
    protected B1(B1 copy) : base(copy) { this.Y1 = copy.Y1; }
    public int Y1 { get; set; }
    public virtual object Clone()
    {
        return new B1(this); // Protected copy constructor
    }
}