如何在此程序中调用类构造函数,以及如何打印它 [英] How do I call a class constructor in this program, and how do I print it

查看:171
本文介绍了如何在此程序中调用类构造函数,以及如何打印它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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);


这篇关于如何在此程序中调用类构造函数,以及如何打印它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆