简体   繁体   中英

How to define accessors for a children?

I have 3 classes :

public interface IParent
{
    String World { get; }
}

public class Parent : IParent
{
    public String World;
    {
        get
        {
            return "Hello " + this.World;
        }
    }
}

public class Children : Parent
{
    public String World = "World";
}

How should I do so that the get accessor of Parent is being called with the World attribute of Children ?

You can use a second property for the suffix and make it virtual to allow the descendants to overwrite it

public class Parent : IParent
{
    protected virtual string Suffix => "World";

    public String World => "Hello " + Suffix;
}

public class Children : Parent
{
    protected override string Suffix => "Again";
}

Parents will display "Hello World", Children will display "Hello Again". This is only true for the run time type. The static (ie compile time) type does not matter.

Parent p = new Children();
Console.WriteLine(p.World); // Displays "Hello Again"!

Here, the static type of p is Parent . The run time type is Children . This behavior is called Polymorphism (C# Programming Guide) .

There is no way for a real Parent to know about the Suffix "Again".

You can use additional private field which will be virtual, so you can override it in the child.

Try this:

public interface IParent
{
    string HelloWorld { get; }
}

public class Parent : IParent
{
    protected virtual string World { get; }

    public string HelloWorld
    {
        get
        {
            return "Hello " + World;
        }
    }
}

public class Children : Parent
{
    protected override string World { get; } = "World";
}

Or you can also pass a string through the constructor, then you can set value at runtime.

public interface IParent
{
    string HelloWorld { get; }
}

public class Parent : IParent
{
    private readonly string world;

    public Parent(string world)
    {
        this.world = world;
    }

    public string HelloWorld
    {
        get
        {
            return "Hello " + world;
        }
    }
}

public class Children : Parent
{
    public Children(string world) : base(world)
    {
    }
}

Use it like this:

var children = new Children("World");
Console.WriteLine(children.HelloWorld);

This is back to front.

It's down to the inheriting class to include base class behaviour from overrides if required . The subclass override s the base behaviour, but has a hook to the base class via the base keyword.

As such:

public interface IParent
{
    String World { get; }
}

public class Parent : IParent
{
    public virtual String World
    {
        get
        {
            return "Hello";
        }
    }
}

public class Children : Parent
{
    public override String World
    {
        get
        {
            return base.World + " World!";
        }
    }
}

The property is already part of the parent. You'll want a protected set function however... Set it in the constructor like so:

public interface IParent
{
    String World { get; }
}

public class Parent : IParent
{
    public String World { get; protected set; }
    public Parent() { World = "Hello World"; }
}

public class Children : Parent
{
    public Children() { World = "World"; }
}

However, perhaps you are looking for something more like this:

public interface IParent
{
    String World { get; }
}

public class Parent : IParent
{
    public String World { get; private set; }
    public Parent(String thing) { World = "Hello " + thing; }
}

public class Children : Parent
{
    public Children() : base("World") { }
}

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