在父类中创建chlid类对象

时间:2022-07-24 19:34:32

I have two classes a parent class and child. what I really want to find away so ,I can create an object of child class in parent class. I tired but compiler throw exception here is my code.

我有两个班级,父班级和孩子班。我真的很想找到的,我可以在父类中创建一个子类的对象。我累了,但编译器抛出异常是我的代码。

   class b  { 
        private c obj;
        public b()
        {
            obj=new c();
        }
        public void show()
        {

            obj.show();
        }
        }
        class c : b{ 
        public void show()
        {
        Console.WriteLine("working ");
        }
        }
  b object=new b();
  b.show();

Is there a way to create child class object in Parent class.

有没有办法在Parent类中创建子类对象。

2 个解决方案

#1


1  

you can try this:

你可以试试这个:

class b  { 
    private c obj;
    public b()
    {

    }
    public void show(c o)
    {
        obj = o;
        obj.show();
    }
    }
    class c : b{ 
    public void show()
    {
    Console.WriteLine("working ");
    }
    }

    class Program
    {
        static void Main(string[] args)
        {
            c o=new c();
            b bo = new b();
            bo.show(o);
            o.show();
            Console.ReadKey();

        }
    }

#2


1  

You can create a child object from the parent class, but not in the constructor. If you do that, the child object (which is also a parent) will create another child, which will create another child, and so on. You have made an infinite loop.

您可以从父类创建子对象,但不能在构造函数中创建。如果这样做,子对象(也是父对象)将创建另一个子对象,这将创建另一个子对象,依此类推。你做了一个无限循环。

You can create a child object from a method in the parent class. Example:

您可以从父类中的方法创建子对象。例:

public class Parent {

  private Child _child;

  public void CreateChild() {
    _child = new Child();
  }

  public void Show() {
    _child.Show();
  }

}

public class Child : Parent {

  public void Show() {
    Console.WriteLine("Working");
  }

}

Parent p = new Parent();
p.CreateChild();
p.Show();

#1


1  

you can try this:

你可以试试这个:

class b  { 
    private c obj;
    public b()
    {

    }
    public void show(c o)
    {
        obj = o;
        obj.show();
    }
    }
    class c : b{ 
    public void show()
    {
    Console.WriteLine("working ");
    }
    }

    class Program
    {
        static void Main(string[] args)
        {
            c o=new c();
            b bo = new b();
            bo.show(o);
            o.show();
            Console.ReadKey();

        }
    }

#2


1  

You can create a child object from the parent class, but not in the constructor. If you do that, the child object (which is also a parent) will create another child, which will create another child, and so on. You have made an infinite loop.

您可以从父类创建子对象,但不能在构造函数中创建。如果这样做,子对象(也是父对象)将创建另一个子对象,这将创建另一个子对象,依此类推。你做了一个无限循环。

You can create a child object from a method in the parent class. Example:

您可以从父类中的方法创建子对象。例:

public class Parent {

  private Child _child;

  public void CreateChild() {
    _child = new Child();
  }

  public void Show() {
    _child.Show();
  }

}

public class Child : Parent {

  public void Show() {
    Console.WriteLine("Working");
  }

}

Parent p = new Parent();
p.CreateChild();
p.Show();