简体   繁体   中英

c# Properties in Abstract Base Classes

I have a strange problem that I could not solve. When I try to compile the following snipped I get this error:

'AbstractClass' does not implement interface member 'Property' (Compiler Error CS0535)

The online help tells me to make my AbstractClass abstract, which it is. Can anybody tell me where I went wrong?

Cheers Rüdiger

public interface IBase {
    string Property { get; }
}

public abstract class AbstractClass : IBase
{
    public override string ToString()
    {
        return "I am abstract";
    }
}

public class ConcreteClass : AbstractClass
{
    string Property { 
        get {
            return "I am Concrete";
        }
    }
}

Your AbstractClass needs to provide an implementation for Property from the IBase interface, even if it's just abstract itself:

public abstract class AbstractClass : IBase
{
    public override string ToString()
    {
        return "I am abstract";
    }

    public abstract string Property { get; }
}

Update: Luke is correct that the concrete implementation will need to specify that Property is an override, otherwise you'll get a "does not implement inherited abstract member" error:

public class ConcreteClass : AbstractClass
{
    public override string Property { 
        get {
            return "I am Concrete";
        }
    }
}

You have to add abstract implementation of the property to your AbstractClass :

public abstract class AbstractClass : IBase
{
    public override string ToString()
    {
        return "I am abstract";
    }

    public abstract string Property { get; }

}

And the override keyword to the Concrete class

AbstractClass应该实现包含Property的IBase,你还没有实现它

You abstract class does not implement the interface IBase . Just add the property Property to AbstractClass .

public abstract String Property { get; }

You need to declare Property in AbstractClass so that it fulfills the IBase contract.

If you want ConcreteClass to be able to override Property then you should declare it as abstract or virtual . Then you'll need to declare the ConcreteClass implementation of Property with override .

    public interface IBase
    {
        string Property { get; }
    }

    public abstract class AbstractClass : IBase
    {
        public abstract string Property { get; }

        public override string ToString()
        {
            return "I am abstract";
        }
    }

    public class ConcreteClass : AbstractClass
    {
        public override string Property
        {
            get
            {
                return "I am Concrete";
            }
        }
    }

You need to implement the IBase -property Property like this:

public abstract class AbstractClass : IBase
{
    public override string Property()
    {
        return "This is the base-class implementation";
    }
}

Or make it abstract .

您必须在抽象类中实现Property属性。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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