public class constructor {
class Person {
private String name;
private String birthday;
private String sex;
public Person( String name, String birthday, String sex) {
this.name = name;
this.birthday = birthday;
this.sex = sex;
}
}
public static void main(String[] args) {
Person obj=new Person("safwan","01--05-1999","male");
}
}
线程“主”中的异常java.lang.Error:未解决的编译问题: 无法访问类型构造函数的封闭实例。必须使用封闭的类型构造函数实例来限制分配(例如x.new A(),其中x是构造函数实例)。
在builder.main(constructor.java:18)
答案 0 :(得分:0)
您可以将您的Person
类设为静态:
public class constructor {
static class Person {
private String name;
private String birthday;
private String sex;
public Person( String name, String birthday, String sex) {
this.name = name;
this.birthday = birthday;
this.sex = sex;
}
}
public static void main(String[] args) {
Person obj=new Person("safwan","01--05-1999","male");
}
}
因此它将进行编译。