个人总结,欢迎补充~
this:它存放的值就是此对象实例的“引用”(地址),也就是“new 此类构造函数”返回
的这个“引用值”。也就是用来引用此对象实例本身。
this(参数) :调用此类的构造函数,此种用法中的this关键字可代表此类
从用途来看,主要有3种情况:
(1)取得此对象实例本身的“引用值”
(2)在函数内指称“成员变量”,以区别于同名的“局部变量”
(3)用于构造函数内,以调用此类内其他的构造函数
练习代码- import java.lang.*;
- class a13
- {
- public static void main(String[] para)
- {
- System.out.println("========= test obj1 =========");
- myClass obj1 = new myClass();
- System.out.println("obj1.getThis() = " + obj1.getThis());
- obj1.showABC();
- System.out.println("========= test obj2 =========");
- myClass obj2 = new myClass(2,78,'C');
- obj2.showABC();
- System.out.println("========= test obj3 =========");
- myClass obj3 = new myClass(3,26,'E',true);
- obj3.showABC();
- }
- }
- class myClass
- {
- public final myClass myThis = this; //引用此对象实例本身
-
-
- public int a;
- public float b;
- public char c;
-
- public myClass getThis()
- {
- return this;
- }
-
- public myClass()
- {
- a = 1;
- b = 60;
- c = 'D';
- System.out.println("this = " + this);
- System.out.println("myThis = " + myThis);
-
- }
-
- public myClass(int a,float b,char c)
- {
- this(); //在这个构造函数调用上一个构造函数内容
- this.a = a;
- this.b = b;
- this.c = c;
- }
-
- public myClass(int a,float b,char c,boolean revise)
- {
- this();
- this.a = a;
- this.c = c;
- if(revise)
- this.b = this.b * 70/100 + 30; //注意此处b的值是60
- else
- this.b = b;
- }
- public void showABC()
- {
- System.out.println("a = " + a + " b = " + b + " c = " + c );
- }
- }
复制代码 super:引用该对象实例内的父类对象
super(参数) :调用父类的构造函数,此种用法中的super关键字可代表此类
从用途来看,主要有2种情况:
(1)用于构造函数内,以指定要调用父类的某个构造函数
练习代码- import java.lang.*;
- class a14
- {
- public static void main(String[] para)
- {
- Cc obj1 = new Cc();
- System.out.println("obj1.Var1 = " + obj1.Var1);
- System.out.println("obj1.Var2 = " + obj1.Var2);
- System.out.println("obj1.Var3 = " + obj1.Var3);
- System.out.println("========================");
- Cc obj2 = new Cc(10000);
- System.out.println("obj2.Var1 = " + obj2.Var1);
- System.out.println("obj2.Var2 = " + obj2.Var2);
- System.out.println("obj2.Var3 = " + obj2.Var3);
- System.out.println("========================");
- Cc obj3 = new Cc(555,12);
- System.out.println("obj3.Var1 = " + obj3.Var1);
- System.out.println("obj3.Var2 = " + obj3.Var2);
- System.out.println("obj3.Var3 = " + obj3.Var3);
- }
- }
- class AaA
- {
- int Var1;
-
- public AaA(int var1)
- {
- //自动调用 super(),也就是 Object()
- System.out.println("start in A(int var1) Var1 = " + Var1);
- //可取 Var1 之值,证实在此之前 Var1 已自动初始化(为0)
- Var1 = var1;
- System.out.println("执行 public A(int var1)");
- }
-
- public AaA()
- {
- //自动调用 super(),也就是 Object()
- System.out.println("start in A() Var1 = " + Var1);
- //可取 Var1 之值,证实在此之前 Var1 已自动初始化(为0)
- Var1 = 10;
- System.out.println("执行 public A()");
- }
- }
- class Bb extends AaA
- {
- int Var2;
-
- public Bb()
- {
- //自动调用 super(),A 必须要有 A() 预设构造子
- Var2 = 10;
- System.out.println("执行 public B()");
- }
-
- public Bb(int var1)
- {
- super(var1); //若不指定调用,则会自动调用父类的 A()
- Var2 = Var1 * 2; // 到此行 Var1 之值已初始化
- System.out.println("执行 public B(int var1)");
- }
-
- }
- class Cc extends Bb
- {
- int Var3;
-
- public Cc()
- {
- //自动调用 super(),B 必须要有 B() 预设构造子
- Var3 = 10;
- System.out.println("执行 public C()");
- }
-
- public Cc(int var1)
- {
- super(var1); //若不指定调用,则会自动调用父类的 B()
- Var3 = Var2 * 2; // 到此行 Var2 之值已初始化
- System.out.println("执行 public C(int var1)");
- }
-
- public Cc(int var3,int multi)
- {
- //不指定调用,自动调用 super(),即 B 类的 B()
- Var3 = var3*multi;
- System.out.println("执行 public C(double var3,int multi)");
- }
- }
复制代码 (2) 指定使用继承而来的某成员,而非使用此类改写的成员 |