黑马程序员技术交流社区
标题:
JAVA 中的 this 和 super 有什么区别啊?
[打印本页]
作者:
乖睡觉咯
时间:
2013-8-15 17:57
标题:
JAVA 中的 this 和 super 有什么区别啊?
我想知道一下 this 和 super 的区别, 没有理解的那么清楚,有点迷糊. 哪位大神可以详细的给讲解一下
作者:
yangxin540
时间:
2013-8-15 18:10
this指当前对象,如果调用构造函数只能调用本类的现实化构造函数
super调用父类构造函数的关键字它引用当前对象的直接父类中的成员
他们调用构造函数时,都必须放在首行
private String name;
setAge(String name)
{
this.name = name;//这里的this就是代表当前对象
}
作者:
吴光新
时间:
2013-8-15 18:13
this用于区分局部变量和成员变量同名情况。
this代表它所在函数所属对象的引用。
简单说,哪个对象在调用this所在的函数,this就代表哪个对象。
super代表父类的引用
作者:
乔小北
时间:
2013-8-15 18:58
public class Box {
double weith;
double height;
double depth;
Box(double w,double h,double d){
//this总是调用该方法的一个引用,可以在当前类的类型所允许对象的任何地方将this作为一个引用
this.weith=w;
this.height=h;
this.depth=d;
}
}
class BoxWeight extends Box{
double weight;
BoxWeight(double w,double h,double d,double m){
super(w,h,d); //调用父类的构造方法,BoxWeight不再初始化这三个值
this.weight=m;
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2