使用C#中为基类创建的方法填充派生对象中的继承成员

时间:2015-09-04 18:22:26

标签: c# object inheritance member base

嘿伙计们,我想要达到这样的目标

class Program
{
    static void Main(string[] args)
    {

        Responsible responsible = new Responsible()
        {
            //I want here to populate with PopulatePerson the base members
            Phone = "93827382",
            Company = "Google"
        };
    }

    public Person PopulatePerson(string pName, string pLastName)
    {
        Person person = new Person();
        person.Name = pName;
        person.LastName = pLastName;
        return person;
    }
}
public class Person
{
    public string Name { get; set; }
    public string LastName { get; set; }
}



public class Responsible : Person
{

    public string Phone { get; set; }
    public string Company { get; set; }
}

案例在数据库查询和内容方面更复杂,但基本上这就是我需要的内容

我可以使用一个名为Person in Responsible的成员并且做Person = PopulatePerson(“Dan”,“Johns”)但是因为我继承了我发现它有点多余

2 个答案:

答案 0 :(得分:1)

这样的事情怎么样?我为Person创建了一个通用(静态)工厂方法,可以在从Person继承的所有类型中重用。

class Program
{
    static void Main(string[] args)
    {

        //Responsible responsible = new Responsible()
        //{
        //    //I want here to populate with PopulatePerson the base members
        //    Phone = "93827382",
        //    Company = "Google"
        //};

        var responsible = Responsible.Populate("Glenn", "Fake", "93827382", "Google");
        //responsible
    }

    // NO LONGER NEEDED
    // ============================
    //public Person PopulatePerson(string pName, string pLastName)
    //{
    //    Person person = new Person();
    //    person.Name = pName;
    //    person.LastName = pLastName;
    //    return person;
    //}
}
public class Person
{
    public string Name { get; set; }
    public string LastName { get; set; }

    public static TPerson Populate<TPerson>(string name, string lastname) where TPerson : Person, new()
    {
        TPerson person = new TPerson();
        person.Name = name;
        person.LastName = lastname;
        return person;
    }
}



public class Responsible : Person
{
    public static Responsible Populate(string name, string lastname, string phone, string company)
    {
        var p = Responsible.Populate<Responsible>(name, lastname);
        p.Phone = phone;
        p.Company = company;
        return p;
    }

    public string Phone { get; set; }
    public string Company { get; set; }
}

答案 1 :(得分:0)

不确定您的真实情况是否合理,但您可以更改PopulatePerson以接受Person对象作为输入参数,而不是在内部创建新对象。然后,您可以将新的Responsible对象传递给它,然后设置Phone and Company。

编辑:像这样

class Program
{
    static void Main(string[] args)
    {

        Responsible responsible = new Responsible();
        PopulatePerson(responsible, "first", "last");
        responsible.Phone = "93827382";
        responsible.Company = "Google";

    }

    public static void PopulatePerson(Person person, string pName, string pLastName)
    {
        person.Name = pName;
        person.LastName = pLastName;
    }
}
public class Person
{
    public string Name { get; set; }
    public string LastName { get; set; }
}



public class Responsible : Person
{

    public string Phone { get; set; }
    public string Company { get; set; }
}