如何在此程序中调用类构造函数,以及如何打印它

问题描述:

class Program
    {
        static void Main(string[] args)
        {
            B b = new B(20);
            Console.WriteLine(b.x);
          
        }
    }
    public class A
    {
        public int y;
        public A(int x1)
        {
            y = x1;
        }
        
    }
    public class B
    {
        A a = new A(10);
        public int x;
        public B(int x2)
        {
            x = x2;
        }
    }
}





我尝试了什么:



我想调用A类构造函数我该如何调用



What I have tried:

well i want to call the A class constructor how should i call

你已经在ClassB代码中执行此操作并使用您在Main方法中创建的ClassB实例执行此操作。这是C#101的东西:

You're already doing it in the ClassB code and doing it with the ClassB instance you created in the Main method. This is C# 101 stuff:
A myInstance = new A(10);