Cannot refer to an instance field arg while explicitly invoking a constructor
来源:互联网 发布:unity3d 官方文档 编辑:程序博客网 时间:2024/06/05 16:48
public int i=1;
public MyThing(){
super(i);
}
上面这段代码也会编译不通过的:Cannot refer to an instance field arg while explicitly invoking a constructor(在明确调用一个构造器的时候不能引用类的一个实例域)
原因很简单,我们假如可以通过编译器。我问一个问题,调用构造器是干什么的?
大家都知道,创建一个对象并初始化对象中的数据域。红色字体很重要,在上面的列子中new MyThing()的作用就是为了初始化这个对象的数据域i,试问在没有通过构造器初始化之前,i的值是不确定的。我们怎么能使用一个不确定的i值来初始化对象。
必须要先明确初始化实例域,才能创建好对象,进而才能使用对象的数据域。在没有实例化之前就想使用他,不可能。
public int i=1;
public MyThing(int j){
super(j);
}
上面的代码就可以,为什么,因为编译器知道,你如果想构造这个对象,必须传过来一个确定的值来初始化。
public MyThing(){
super(i);
}
上面这段代码也会编译不通过的:Cannot refer to an instance field arg while explicitly invoking a constructor(在明确调用一个构造器的时候不能引用类的一个实例域)
原因很简单,我们假如可以通过编译器。我问一个问题,调用构造器是干什么的?
大家都知道,创建一个对象并初始化对象中的数据域。红色字体很重要,在上面的列子中new MyThing()的作用就是为了初始化这个对象的数据域i,试问在没有通过构造器初始化之前,i的值是不确定的。我们怎么能使用一个不确定的i值来初始化对象。
必须要先明确初始化实例域,才能创建好对象,进而才能使用对象的数据域。在没有实例化之前就想使用他,不可能。
public int i=1;
public MyThing(int j){
super(j);
}
上面的代码就可以,为什么,因为编译器知道,你如果想构造这个对象,必须传过来一个确定的值来初始化。
阅读全文
2 0
- Cannot refer to an instance field arg while explicitly invoking a constructor
- 转-Cannot refer to an instance field arg while explicitly invoking a constructor
- Cannot refer to a non-final variable i inside an inner class defined in a differ
- Cannot refer to a non-final variable inside an inner class defined in a different method
- get_transform is not allowed to be called from a MonoBehaviour constructor (or instance field initia
- get_transform is not allowed to be called from a MonoBehaviour constructor (or instance field initia
- Cannot refer to a non-final variable image inside an inner class 关于android 的final
- Cannot refer to a non-final variable anim inside an inner class defined in a different method
- Cannot refer to a non-final variable music inside an inner class defined in a different method
- Cannot refer to a non-final variable mylis inside an inner class defined in a different method
- "Cannot refer to a non-final variable * inside an inner class defined in a different method"错误解析
- Cannot refer to a non-final variable mylis inside an inner class defined in a different method
- Cannot refer to a non-final variable * inside an inner class defined in a different method"错误解析
- Cannot refer to a non-final variable * inside an inner class defined in a different method"错误解析
- Cannot refer to a non-final variable * inside an inner class defined in a different method错误问题
- java:Cannot refer to a non-final variable tx inside an inner class defined in a different method
- Cannot refer to a non-final variable mylis inside an inner class defined in a different method
- Cannot refer to a non-final variable mylis inside an inner class defined in a different method
- mysql -server-5.6 install master and slave on ubuntu 14.04server
- get和post的使用场景
- [Bugku]密码???[writeup]
- php里String中双引号""和单引号''的区别
- get_distance
- Cannot refer to an instance field arg while explicitly invoking a constructor
- MySQL · 性能优化 · MySQL常见SQL错误用法
- 迭代器使用
- 帧缓冲对象-实践过程问题及解决
- node.js常用命令
- 谈谈JAVA AIO
- org.xml.sax.SAXException: Scanner State 24 not Recognized
- SDIO总线协议
- CSDN