我想创建一个像" android.view.View"这样的类,它将显示"' android.view.View&#39中没有可用的默认构造函数;"错误,如果在课堂上没有调用super(...)来扩展我的课程,抱歉我的英语不好,有没有人可以帮助我?感谢
- 修改
@Eran,谢谢,你的评论给了我一个提示,所以罪魁祸首是龙目岛注释" @Data",即使我用参数创建一个构造函数也没有错误,任何想法如何实现这一点Lombok的?答案 0 :(得分:1)
您可以将其设为私有,以便其他类无法使用它。
public class MyClass{
private String text;
private int number;
private MyClass(){
this.text="hi";
}
public MyClass(int number){
this();
this.number = number
}
...
}
这样,子类应该使用非默认构造函数:
public MyChildClass1 extends MyClass{
//NO COMPILE: No default constructor available in MyClass
}
public MyChildClass2 extends MyClass{
public MyChildClass2(){
//NO COMPILE: No default constructor available in MyClass
}
}
public MyChildClass3 extends MyClass{
public MyChildClass3(){
super(); //NO COMPILE: MyClass() has private access
}
}
public MyChildClass4 extends MyClass{
public MyChildClass4(){
super(2); //It Works! (it will initialize number=2 and text="hi")
}
}
答案 1 :(得分:0)
这是您的主要班级模式。
public class MyParentClass {
MyParentClass(int value) {
}
}
如果任何类想要在不调用super(value)
的情况下扩展它,将获得“parent_class_name中没有可用的默认构造函数”
如下所示:
public class ChildClass extends MyParentClass {
}
答案 2 :(得分:0)
我只提供私有构造函数:
20160711,"M","N1","F","S","A","good data with.....some special character and space
space ..
....","M","072","00126"
20160711,"M","N1","F","S","A","R","M","072","00126"
20160711,"M","N1","F","S","A","R","M","072","00126"