Java 入門指南 V2.00 - 單元 11 範例




ClassDemo06.java


package classdemo; // 宣告套件

public class ClassDemo06 {
   private int a;

   public ClassDemo06(int a) {
       setA(a);
   }

   public void setA(int a) {
      this.a = a;
   }

   public int getA() {
      return a;
   }

   public int doSomething(int a) {
      return this.a + a;
   }

   public static void main(String[] args) { 
      ClassDemo06 d = new ClassDemo06(13);

      System.out.println();
      System.out.println(d.getA());
      System.out.println(d.doSomething(9));
      System.out.println();
   }
}
 
/* 檔名: ClassDemo06.java
   作者: Kaiching Chang
   時間: September, 2014  */

ClassDemo07.java


// 引入套件中的類別名稱
import classdemo.ClassDemo06;

public class ClassDemo07 {
   public static void main(String[] args) { 
      // 建立套件裡的類別建立物件
      ClassDemo06 d = new ClassDemo06(11);

      System.out.println();
      System.out.println(d.getA());
      System.out.println(d.doSomething(8));
      System.out.println();
   }
}
 
/* 檔名: ClassDemo07.java
   作者: Kaiching Chang
   時間: September, 2014  */

the end

1 則留言:

石方 提到...

請問出現 bad class file:D:\workspace\ClassDemo06.class
class file contains wrong class:classdemo.ClassDemo06
Please remove or make sure it appears in the correct subdirectory of classpath.