⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 j02080502.java

📁 经典java教材《java完美经典》一书中源码的完全收集
💻 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);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -