public class AnyThing {
public AnyThing(){
this(5);
System.out.println("无参数构造方法");
}
public AnyThing(String name){
System.out.println(name);
}
public AnyThing(int age){
System.out.println("年龄是"+age);
}
public static void main(String[] args){
AnyThing a = new AnyThing();
a.AnyThing("1111111");
}
}
提示:The method AnyThing(String) is undefined for the type AnyThing。
但是 AnyThing(String name) 方法已经定义了啊,main()方法中不能调用构造函数吗?
哦,我明白了,我的程序里面的a是通过无参数的构造方法实例出来的对象,构造方法只能通过new来进行实例化。构造方法是不能通过“.”来调用的,其他的成员方法是可以通过“.”来调用的,是吧?
追答对,公有的方法可以,私有的只能在类中自己调用