【Java开源代码栏目提醒】:网学会员Java开源代码为您提供j02080502.java参考,解决您在j02080502.java学习中工作中的难题,参考学习。
import java.lang.*;
class j02080502
{
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();
//=========== 子类的 this 会是子类类型 ====================
System.out.println("========= test obj4 =========");
childClass obj4 = new childClass(4,43,'E',false);
obj4.showABC();
}
}
class myClass
{
public final myClass myThis = this; //引用此对象实例本身,此处 this 就是对象实例拥有的 this,不是局部变量
//static public final myClass aThis = this; //错误,this 还不存在无法取得其值
public int a;
public float b;
public static char c; //定义为 static 只为测试 this 也可用于指称 static 成员
public myClass getThis()
{
return this;
}
public myClass()
{
a = 1;
b = 60;
c = 'D';
System.out.println("this = " + this);
System.out.println("this 的类类型 = " + this.getClass().getName());
System.out.println("myThis = " + myThis);
System.out.println("myThis 的类类型 = " + myThis.getClass().getName());
}
public myClass(int a,float b,char c)
{
this();
this.a = a;
this.b = b;
this.c = c; // static 的成员一样可用 this 来指称
}
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;
else
this.b = b;
}
public void showABC()
{
System.out.println("a = " + a + " b = " + b + " c = " + c );
}
}
class childClass extends myClass
{
public childClass()
{
//super(); //编译器会自动调用,所以可不写
}
public childClass(int a,float b,char c)
{
super(a,b,c);
}
public childClass(int a,float b,char c,boolean revise)
{
super(a,b,c,revise);
}
}
上一篇:
j02080501.java
下一篇:
“台独”的渊源与实质(中)——兼论台湾问题的基本估计与中国统一的可能前景