简体   繁体   English

在C#中扩展抽象类

[英]Extending abstract classes in c#

I am a Java developer and I have noticed some differences in extending abstract classes in c# as opposed to Java. 我是Java开发人员,并且注意到在c#中扩展抽象类(与Java相对)方面存在一些差异。 I was wondering how ac# developer would achived the following. 我想知道ac#开发人员如何实现以下目标。

1) Covarience 1)协方差

public abstract class A {
   public abstract List<B> List();
}
public class BList : List<B> {
}
public abstract class C : A { 
   public abstract BList List();
}

So in the above hierarchy, there is covarience in C where it returns a type compatible with what A returns. 因此,在上面的层次结构中,C中存在协方差,它返回与A返回的类型兼容的类型。 However this gives me an error in Visual Studio. 但这给我Visual Studio中的错误。 Is there a way to specify a covarient return type in c#? 有没有一种方法可以在C#中指定协变返回类型?

2) Adding a setter to a property 2)将设置器添加到属性

public abstract class A {
   public abstract String Name { get; }
}
public abstract class B : A {
   public abstract String Name { get; set }
}

Here the compiler complains of hiding. 在这里,编译器抱怨隐藏。 Any suggestions? 有什么建议么?

Please do not suggest using interfaces unless that is the ONLY way to do this. 请不要建议使用接口,除非这是唯一的方法。

Somehow I don't understand your Intention. 不知何故我不明白你的意图。 Why don't you solve it like this ? 你为什么不这样解决呢?

public class BList : List<T> where T : B {} same =  public class BList : List<B> {}

public class C : A
{
    public override List<B> Liste()
    {
        return new BList();
    }
}

About your second Question : 关于第二个问题:

What you want to do there is bad design in my eyes. 您想做的是我眼中的设计不好。 You can use the keyword virtual on both and ignore the warnings, but why do you want to do something like this ? 您可以在两者上都使用关键字virtual,而忽略警告,但是为什么要这样做呢?

public abstract class A
{
    public virtual string Name { get; protected set; }

}

public abstract class B : A
{
    public virtual string Name { get; set;}
}

Neither of these is supported in C#. C#不支持这两种方法。 Covariant returns are also not supported in the underlying runtime (the CLR). 基础运行时(CLR)也不支持协变返回。

It appears that it may be possible to do something along the lines of your property example on the CLR, but not in C#. 似乎有可能按照CLR(而不是C#)上的属性示例的方式进行操作。 Under the hood, a property essentially consists of a name, the property type, and references to the getter and setter methods (as applicable), which are typically named get_<Property> and set_<Property> (but which are not required to follow that convention in non-CLS-compliant classes). 在本质上,属性本质上由名称,属性类型以及对getter和setter方法的引用(如果适用)组成,这些方法通常被命名为get_<Property>set_<Property> (但不需要遵循)不符合CLS的类中的约定)。 It is possible to create a property in a base class which has only a getter, and then another property in the derived class with the same name and getter, but which has its own setter. 可以在仅具有getter的基类中创建一个属性,然后在派生类中具有相同名称和getter但具有自己的setter的另一个属性中创建一个属性。 However, I don't know that any .NET languages allow such a definition. 但是,我不知道任何.NET语言都允许这样的定义。

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

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