从具有其他类型属性的抽象类继承(.NET 3.5、C#)

发布于 2024-11-08 00:07:59 字数 438 浏览 6 评论 0原文

我有 3 个以下类:

public class BaseProperty1{
    public string Property1 {get; set;}
}

public class ChildProperty1 : BaseProperty1 {

}

public abstract class Base{
    public abstract BaseProperty1 bp1 {get; set;}
}

我试图从 Base 派生一个以下类:

public class Child : Base{
    public ChildProperty1 bp1 {get; set;}
}

但我收到一个错误,指出“set”和“get”方法未实现。这只是我使用的语法还是我的思维方式错误?

谢谢你!

I have 3 following classes:

public class BaseProperty1{
    public string Property1 {get; set;}
}

public class ChildProperty1 : BaseProperty1 {

}

public abstract class Base{
    public abstract BaseProperty1 bp1 {get; set;}
}

I'm trying to derive a following class from Base:

public class Child : Base{
    public ChildProperty1 bp1 {get; set;}
}

But I'm getting an error that "set" and "get" methods are not implemented. Is it just the syntax I'm using or my way of thinking is wrong?

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谁的年少不轻狂 2024-11-15 00:07:59

您将无法使用自动属性,因为您必须完全匹配基类的类型。您必须采用老式方法:

public abstract class Child : Base
{
    private ChildProperty1 _bp1;

    public BaseProperty1 bp1
    {
        get { return _bp1; }

        // Setter will be tricky. This implementation will default to null
        // if the cast is bad.
        set { _pb1 = value as ChildProperty1; }
    }
}

您也可以使用泛型来解决问题:

public abstract class Parent<TProp> where TProp : BaseProperty1
{
    public abstract T bp1 { get; set; }
}

public abstract class Child : Parent<ChildProperty1>
{
    public ChildProperty1 bp1 { get; set; }
}

You won't be able to use Auto Properties since you have to match the type of the base class exactly. You'll have to go the old fashioned way:

public abstract class Child : Base
{
    private ChildProperty1 _bp1;

    public BaseProperty1 bp1
    {
        get { return _bp1; }

        // Setter will be tricky. This implementation will default to null
        // if the cast is bad.
        set { _pb1 = value as ChildProperty1; }
    }
}

You might also be able to use generics to solve the problem:

public abstract class Parent<TProp> where TProp : BaseProperty1
{
    public abstract T bp1 { get; set; }
}

public abstract class Child : Parent<ChildProperty1>
{
    public ChildProperty1 bp1 { get; set; }
}
拥抱影子 2024-11-15 00:07:59

如果将方法或属性标记为抽象,则必须在继承类中实现它。
您可以隐藏旧属性(基类中的 bp1)并使用另一种返回类型编写新属性,如下所示:

public abstract class Base{
    public BaseProperty1 bp1 {get; set;} //without abstract identifier
}

public class Child : Base
{
       public new ChildProperty1 bp1 { get; set; } // with new modifier
}

If you mark method or property as an abstract, you must implement it in inherited class.
You can hide old property (bp1 in Base class) and write new one with another return type like this:

public abstract class Base{
    public BaseProperty1 bp1 {get; set;} //without abstract identifier
}

public class Child : Base
{
       public new ChildProperty1 bp1 { get; set; } // with new modifier
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文