简体   繁体   English

如何为儿童定义访问器?

[英]How to define accessors for a children?

I have 3 classes : 我有3节课:

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 ? 我应该如何做才能使用ChildrenWorld属性调用Parent的get访问器?

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". 父母将显示“ Hello World”,孩子将显示“ 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 . 在这里, p的静态类型是Parent The run time type is Children . 运行时类型为Children This behavior is called Polymorphism (C# Programming Guide) . 这种行为称为多态性(C#编程指南)

There is no way for a real Parent to know about the Suffix "Again". 真正的Parent无法知道后缀“ 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. 子类override基本行为,但通过base关键字具有对基本类的钩子。

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: 但是,您需要一个受保护的set函数...在构造函数中进行设置,如下所示:

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") { }
}

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

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