用父项的值扩展对象 [英] Extend object with values from parent

查看:59
本文介绍了用父项的值扩展对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我说一堂课

public class Parent
{
    public string Name { get; set; }
    public string City { get; set; }
}

在某些函数中,我得到类型为Parent的对象列表,接下来,我想用具有某些值的新字段扩展这些对象,所以我要声明一个扩展类,如下所示:

public class Child : Parent
{
    public Child(Parent parent)
    {
        Name = parent.Name;
        City = parent.City;
    }
    public int Age { get; set; }
}

并为每个扩展对象调用构造函数.有没有更好的方法可以做到这一点?如果父项"中有多个属性该怎么办?也许有一些更优雅的方法可以实现这一目标?

解决方案

我认为您可能正在寻找复制构造函数模式.每个级别定义一个protected构造函数,该构造函数复制相关属性:

public class Parent
{
    public string Name { get; set; }
    public string City { get; set; }

    //normal constructor
    public Parent()
    {

    }

    protected Parent(Parent copy)
    {
        this.Name = copy.Name;
        this.City = copy.City;
    }
}

Child将从Parent继承,向下传递给copy-constructor,然后根据需要附加其新值:

public class Child : Parent
{
    public string NewInfo { get; set; }

    public Child(Parent copy)
        : base(copy)
    {

    }
}

用法可能如下:

Parent parent = new Parent() { Name = "Name", City = "StackOverflow"};

Child child = new Child(parent) { NewInfo = "Something new!" };

Console.WriteLine(child.Name); //Name
Console.WriteLine(child.City); //StackOverflow
Console.WriteLine(child.NewInfo); //Something new!

这样做的好处是,您可以具有多个继承级别,每个级别都可以管理自己的属性.

鉴于您的最新评论:

这个问题的动机是在这样的情况下 带有数据的对象列表,并希望显示该数据,但带有一些 其他字段,而无需涉及基类.

也许更好的方法是包装基类:

public class Child
{
    private readonly Parent WrappedParent;

    public string NewInfo { get; set; }

    public string Name 
    { 
        get { return WrappedParent.Name; }
        set { WrappedParent.Name = value; }
    }

    public string City 
    { 
        get { return WrappedParent.City; }
        set { WrappedParent.City = value; }
    }

    public Child(Parent wrappedParent)
    {
        this.WrappedParent = wrappedParent; 
    }
}

缺点是您必须重新声明每个属性,并且不再继承(不能认为是)"Parent",但是然后您肯定会不再接触"基类了.如果更适合您,可以将"Parent"属性移到IParent界面中,但这样做又是接触"基类,因为您必须在其类定义中添加IParent接口声明.

Let's say I've got a class:

public class Parent
{
    public string Name { get; set; }
    public string City { get; set; }
}

and in some function I'm getting the list of objects type Parent, next I'd like to extend those objects with new field with some value, so I'm declaring an extended class like this:

public class Child : Parent
{
    public Child(Parent parent)
    {
        Name = parent.Name;
        City = parent.City;
    }
    public int Age { get; set; }
}

and call the costructor for each extended object. Is there a better way to do that? What if there will be multiple properties in Parent? Maybe there is some more elegant way to achieve that?

解决方案

I think maybe you're looking for a copy-constructor pattern. Each level defines a protected constructor which copies the relevant properties:

public class Parent
{
    public string Name { get; set; }
    public string City { get; set; }

    //normal constructor
    public Parent()
    {

    }

    protected Parent(Parent copy)
    {
        this.Name = copy.Name;
        this.City = copy.City;
    }
}

The Child would inherit from Parent, pass it down through to the copy-constructor, then append its new values as desired:

public class Child : Parent
{
    public string NewInfo { get; set; }

    public Child(Parent copy)
        : base(copy)
    {

    }
}

Usage might look like:

Parent parent = new Parent() { Name = "Name", City = "StackOverflow"};

Child child = new Child(parent) { NewInfo = "Something new!" };

Console.WriteLine(child.Name); //Name
Console.WriteLine(child.City); //StackOverflow
Console.WriteLine(child.NewInfo); //Something new!

The benefit from this is that you can have multiple levels of inheritance with each level managing their own properties.

EDIT: Given your most recent comment:

The motivation to this question is a situation in where I'm getting a list of objects with data, and want to show this data but with some additional fields, without touching the base class.

Perhaps the better method then is to wrap the base class:

public class Child
{
    private readonly Parent WrappedParent;

    public string NewInfo { get; set; }

    public string Name 
    { 
        get { return WrappedParent.Name; }
        set { WrappedParent.Name = value; }
    }

    public string City 
    { 
        get { return WrappedParent.City; }
        set { WrappedParent.City = value; }
    }

    public Child(Parent wrappedParent)
    {
        this.WrappedParent = wrappedParent; 
    }
}

Downside is you have to redeclare each property, and you are no longer inheriting (cannot be considered a) "Parent", but then you are definitly "not touching" the base class anymore. Could move the "Parent" properties into an IParent interface if that's better for you, but doing so again is "touching" the base class as you'll have to add the IParent interface declaration to its class definition.

这篇关于用父项的值扩展对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆