黑马程序员技术交流社区
标题:
关于this关键字和super关键字
[打印本页]
作者:
王楚鑫
时间:
2013-7-8 13:09
标题:
关于this关键字和super关键字
个人总结,欢迎补充~
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
)
指定使用继承而来的某成员,而非使用此类改写的成员
import java.lang.*;
class a15
{
public static void main(String[] para)
{
B obj1 = new B();
System.out.println("======= B 类之 super =======");
obj1.showBSuper(); //B 定义之方法中 super ==> A
System.out.println("======= B 类之 this =======");
obj1.showBThis(); //B 定义之方法中 this ==> B
C obj2 = new C();
System.out.println("======= C 类之 super =======");
obj2.showCSuper(); // C 定义之方法中 super ==> B
System.out.println("======= C 类之 this =======");
obj2.showCThis(); // C 定义之方法中 this ==> C
}
}
class A1
{
public int Var1 = 1000;
public int Var2 = 2000;
public int Var3 = 3000;
public void Fun1()
{
System.out.println("AAAA ==== Fun1() in A class");
}
public void Fun2()
{
System.out.println("AAAA ==== Fun2() in A class");
}
}
class B extends A1
{
public float Var2 = 22.22F; //Hide 继承来的 Var2
public float Var3 = 33.33F; //Hide 继承来的 Var3
public float Var4 = super.Var2 + 400; //新增,仅为示范而作,尽量将初始化写于构造子之内;
public float Var5 = super.Var2 + 500; //新增,super ==> A
public void Fun2()
{
System.out.println("BBBB ==== Fun2() in B class");
}
public void showBThis()
{
System.out.println("Var1 = " + Var1
+ " Var2 = " + Var2
+ " Var3 = " + Var3);
Fun1();
Fun2();
}
public void showBSuper()
{
System.out.println("super.Var1 = " + super.Var1
+ " super.Var2 = " + super.Var2
+ " super.Var3 = " + super.Var3);
super.Fun1();
super.Fun2();
}
}
class C extends B
{
public String Var2 = "AAAA"; //Hide 继承来的 Var2
public String Var4 = "BBBB"; //Hide 继承来的 Var4
public void Fun1()
{
System.out.println("CCCC ==== Fun1() in C class");
}
public void showCThis()
{
System.out.println("Var1 = " + Var1
+ " Var2 = " + Var2
+ " Var3 = " + Var3
+ " Var4 = " + Var4
+ " Var5 = " + Var5);
Fun1();
Fun2();
}
public void showCSuper()
{
System.out.println("super.Var1 = " + super.Var1
+ " super.Var2 = " + super.Var2
+ " super.Var3 = " + super.Var3
+ " super.Var4 = " + super.Var4
+ " super.Var5 = " + super.Var5);
super.Fun1();
super.Fun2();
}
}
复制代码
作者:
神之梦
时间:
2013-7-8 15:54
为小鑫鑫顶一个
作者:
王楚鑫
时间:
2013-7-8 20:53
神之梦 发表于 2013-7-8 15:54
为小鑫鑫顶一个
:victory:谢谢哟~
作者:
神之梦
时间:
2013-7-8 21:04
王楚鑫 发表于 2013-7-8 20:53
谢谢哟~
:lol
作者:
小狼
时间:
2013-7-12 16:41
学习一下
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2