黑马程序员技术交流社区

标题: 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
  1. public class Box {
  2.      double weith;
  3.      double height;
  4.      double depth;
  5.      Box(double w,double h,double d){
  6.              //this总是调用该方法的一个引用,可以在当前类的类型所允许对象的任何地方将this作为一个引用
  7.              this.weith=w;
  8.              this.height=h;
  9.              this.depth=d;
  10.              
  11.      }
  12. }
  13. class BoxWeight extends Box{
  14.         double weight;
  15.         BoxWeight(double w,double h,double d,double m){
  16.                 super(w,h,d);  //调用父类的构造方法,BoxWeight不再初始化这三个值
  17.                 this.weight=m;
  18.                
  19.         }
  20. }
复制代码





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2