Building the assignment of a constructor class to a base class

public class Test1
{
    public int Q1 { get; set; }
    public string Q2 { get; set; }

}

public class Test2 : Test1
{
    public Test2(Test1 Value)
    {
        Q1 = Value.Q1;
        Q2 = Value.Q2;
        //Does this way of writing is getting very long and difficult

    }
}

public class Test2 : Test1
{
    public Test2(Test1 Value)
    {
        base = Value;
        //In this way a short and easy ways
    }
}



   public Test1 ExampleTest()
    {
        return new Test1();
    }

    public void Example()
    {
        Test2 t = new Test2();

        t = ExampleTest();
    }

I am a direct assignment for class inheritance How do I get it?

+4
source share
1 answer

Both are wrong -

In the first case, you initialize the members of the base class in the constructor of the derived class and are bad practice. The problem here is that you have to use inheritance here and let the base class initialize the inherited elements through the constructor of the base class.

, . , . , .

+1

All Articles