当前位置:网站首页>Fake constructor???

Fake constructor???

2022-06-27 08:30:00 C_ x_ three hundred and thirty

public class MyClass {
    long var;
    public void MyClass(long param) { var = param; }//(1)
    public static void main(String[] args) {
        MyClass a, b;
        a =new MyClass();//(2)
        b =new MyClass(5);//(3)
    }
}
 

What happens when you try to compile the following program ?()
Compilation errors will occur in (1), Because constructors cannot specify return values

Compilation errors will occur in (2), Because this class has no default constructor

Compilation errors will be in (3) It happened at , Because this class has no constructor , The constructor accepts an int Parameters of type

The program will compile and execute correctly

  The answer is the third sentence

Be careful : This public void MyClass(long p) Not a construction method !!! Just a return value of type void General method of , One i Remember the format of the constructor  

public( Or other access modifiers ) Class name ( Parameters are optional ){}

原网站

版权声明
本文为[C_ x_ three hundred and thirty]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/178/202206270823528910.html