简体   繁体   中英

How to override method return type?

I have a parent class and alot children classes:

public class ParentViewModel
{
  public int Id{set;get;}
  public string Name{set;get;}
  <..and another 10 properties..>
}

public class ChildViewModel_1:ParentViewModel
{
  public string Notes{set;get;}
}

Now i have a methods which create a Child classes:

 public static ChildViewModel_1 MakeChild_1()
 {
    var child= new ChildViewModel_1
    {
      Id = ...
      Name = ...
      ...And another parent properties...
      Note = ...
    }
    return child;
 }

They have alot of simular code with parent's class properties.
How can i make method to fill parent class field and use it to create child classes?

I tried:

 public static ParentViewModel MakeParent()
 {
    var parent = new ParentViewModel
    {
      Id = ...
      Name = ...
      ...And another properties...
    }
    return parent;
 }

 public static ChildViewModel_1 MakeChild_1()
 {
    var parent = MakeParent();
    var child= parent as ChildViewModel_1;
    child.Note = ...

    return child;
 }

But expectedly i get child = null .
I read this but looks much difficult.
Any advises?

Make child, then call some function from parent to set parent specific values. Then call function from child to make child specific values.

Parent specific initialization can be in parent's constructor.

Expanding on Dialecticus' answer, here is how you could do it:

class Parent
{
    public string Name { get; set; }
    public int ID { get; set; }
}

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

class Factory
{
    public static Parent MakeParent()
    {
        var parent = new Parent();
        Initialize(parent);

        return parent;
    }

    private static void Initialize(Parent parent)
    {
        parent.Name = "Joe";
        parent.ID = 42;
    }

    public static Child MakeChild()
    {
        var child = new Child();
        Initialize(child);

        child.Note = "memento";

        return child;
    }
}

Use constructors

Specify a construct in your base class and child class, utilize the constructors like:

public class ParentViewModel
{
    public int Id { set; get; }
    public string Name { set; get; }

    protected ParentViewModel(int Id, string Name)
    {
        this.Id = Id;
        this.Name = Name;
    }
}

public class ChildViewModel_1 : ParentViewModel
{
    public string Notes { set; get; }

    public ChildViewModel_1(int Id, string Name, string Notes)
        : base(Id, Name) //Calling base class constructor
    {
        this.Notes = Notes;
    }

    public static ChildViewModel_1 MakeChild_1()
    {
        return new ChildViewModel_1(0, "Some Name", "Some notes");
    }
}

(Not really sure why you need static methods to create objects, if your static method is part of your child class, and you only want to expose object creation through this method then make your child constructor private )

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