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

📄 dog.java

📁 经典java教材《java完美经典》一书中源码的完全收集
💻 JAVA
字号:
package j02080201.Pack2;
import java.lang.*;
import j02080201.Pack1.*;  //或 import j02080201.Pack1.animal;  //才可以使用 animal 类
public class dog extends animal
{           //person 是 animal 的子类,但不在同一个 package 
  public void testfun()
	{   // 测试使用自 animal 继承而来的成员
		//IQ = 40; //错误, private 成员只在 animal 内可见
		setIQ(40);  // animal 的 public 成员
		
		//System.out.println("in dog LabArea = " + LabArea);// 错误,不同 package 故无法存取 animal 内 package 等级的成员
		setLabArea("火星"); // 改用 animal 的 public 成员
				
		DNA = DNA.replace('<','~') ;//存取 animal 定义的 protected 成员
		System.out.println("in dog DNA = " + DNA); // 存取 animal 定义的 protected 成员 
		 /* DNA 与 this.DNA 同,this 为 dog 类类型的一个对象,请参考第五节 this 关键词
		    若于此 Dog 类内构造一个 Dog 类对象,其成员可存取的状况与此 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 Obj4 = new animal();
		Obj4.height = 180;
		Obj4.weight = 80;		
		
		//System.out.println("Obj4.IQ = " + Obj4.IQ);  //错误, private 成员只在 animal 内可见
		Obj4.setIQ(60); //改用 public 方法
		
		//System.out.println("Obj4.LabArea = " + Obj4.LabArea); //错误,不在同一个 package
		Obj4.setLabArea("宇宙第三牧埸");
				
		//System.out.println("Obj4.DNA = " + Obj4.DNA);  
		    // 错误,不在同一个package,不能使用 animal 类对象的 protected 成员
		Obj4.setDNA('F','W'); //改用 public 方法
		
		System.out.println("Obj4.height = " + Obj4.height);
		System.out.println("Obj4.weight = " + Obj4.weight);		
		
		//===================================================
		/*
		dog myDog = new dog();  // 建立的是继承 animal 的 dog 类对象
		myDog.height = 50;
		myDog.weight = 16;
		
		//System.out.println("myDog.IQ = " + myDog.IQ);  //错误, private 成员只在 animal 内可见
		myDog.setIQ(15); //改用 public 接口函数
		
		//System.out.println("in dog LabArea = " + LabArea);// 无法存取 animal 内 package 等级的成员
		setLabArea("多哥星球"); // 改用 animal 的 public 成员	
		
		System.out.println("myDog.DNA = " + myDog.DNA);  //因 Dog 是子类而可在此类内使用
		                                                  //myDog.DNA 若用在其它类内就会有错误
		System.out.println("myDog.height = " + myDog.height);
		System.out.println("myDog.weight = " + myDog.weight);
		*/		
  }
}

⌨️ 快捷键说明

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