简体   繁体   English

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

[英]Inherit from abstract class with properties of another type (.NET 3.5, C#)

I have 3 following classes: 我有以下3个课程:

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: 我正在尝试从Base派生以下类:

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

But I'm getting an error that "set" and "get" methods are not implemented. 但是我收到的错误是“set”和“get”方法没有实现。 Is it just the syntax I'm using or my way of thinking is wrong? 它只是我正在使用的语法或我的思维方式是错误的吗?

Thank you! 谢谢!

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; }
}

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: 您可以隐藏旧属性(Base类中的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
}

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

相关问题 可以抽象类继承另一个抽象类c# - can abstract class inherit another abstract class c# C# 如何从抽象泛型 class 继承 class 并使用泛型 class 作为类型参数 - C# How to inherit class from abstract generic class with generic class as type parameter 我怎样才能拥有一个实现接口并继承自抽象 class 的 class? (C# .NET 3.5) - How can I have a class that implements an interface and inherits from an abstract class? (C# .NET 3.5) 如何从用C#编写的抽象基类继承 - How to inherit from an abstract base class written in C# 在C#中使用依赖接口从Abstract类继承的正确方法 - Correct way to inherit from Abstract class with dependent interfaces in C# 不知道如何从 C# 中声明为抽象类类型的对象中检索属性 - Don't kwow how to retrieve properties from objects declared as abstract class type in C# 在C#中,一个类可以继承自另一个类和接口吗? - In C#, can a class inherit from another class and an interface? 在C#NET 3.5中使用组合框属性 - Using combobox properties in C# NET 3.5 F#类型是否可以从C#类“继承”? - Is it possible for a F# type to “inherit” from a C# class? 继承自抽象 class 的泛型 class 类型的 C# - C# type of generic class that inherits from abstract class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM