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

📄 person.java

📁 经典java教材《java完美经典》一书中源码的完全收集
💻 JAVA
字号:
package j02080201.Pack1;
import java.lang.*;

public class person extends animal
{             //person 是 animal 的子类,而且在同一个 package 里
	public void testfun()
	{   // 测试使用自 animal 继承而来的成员
		//IQ = 80; //错误, private 成员只在 animal 内可见
		setIQ(80);  // 存取animal 的 public 成员
		
		System.out.println("in person LabArea = " + LabArea);// 在同一个 package,可存取 animal 内 package 等级的成员		
		
		DNA = DNA.replace('@','%') ; //存取 animal 定义的 protected 成员
		System.out.println("in person DNA = " + DNA); //存取 animal 定义的 protected 成员
			/* DNA 与 this.DNA 同,this 为 person 类类型的一个对象,请参考第五节 this 关键词
		     若于此 person 类内构造一个 person 类对象,其成员可存取的状况与此 this 对象完全相同*/
		
		System.out.println("in person height = " + height); //存取 animal 的 public 成员   //height 与 this.height 同
		System.out.println("in person weight = " + weight); //存取 animal 的 public 成员  //weight 与 this.weight 同
	}

	public static void main(String para[])
  {               //测试使用 animal 类对象的成员
		animal Obj2 = new animal();
		Obj2.height = 200;
		Obj2.weight = 110;
		
		//System.out.println("Obj2.IQ = " + Obj2.IQ);  //错误, private 成员只在 animal 内可见
		Obj2.setIQ(60); //改用 public 接口函数
		
		System.out.println("Obj2.LabArea = " + Obj2.LabArea);		
		
		System.out.println("Obj2.DNA = " + Obj2.DNA);  
		  // person 和 animal 在同一个 package,可使用 protected 成员

		System.out.println("Obj2.height = " + Obj2.height);
		System.out.println("Obj2.weight = " + Obj2.weight);
  }	
}

⌨️ 快捷键说明

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