2014-01-06 47 views
0

因此,我拿了一些我的科普教授的示例類,並試圖編譯它們以瞭解更多關於它的知識,但是我得到了「找不到符號」的錯誤。所有文件都在同一個文件夾中。任何人都可以告訴我如何解決這個問題或發生了什麼?給我「抽象符號未找到」的子類抽象類的錯誤

這裏是抽象類:

package abstractclass; 

/** 
    The Element class is introduced to illustrate the use of 
    abstract classes. In this revised version of the PersonSet 
    class example, the Person class will inherit from this 
    abstract class. The purpose of the abstract class Element 
    is to introduce the "common class protocol" for all of its 
    subclasses. That common class protocol is introduced using 
    abstract as well as fully implemented methods. The abstract 
    methods are: 
        readIn 
        display 
        equals 
        clone 
    In addition, the abstract class Element fully implements the 
    getClassName method. 
*/ 

    public abstract class Element 
    { 

    // Access method 

    /** 
     The getClassName method returns the name of the calling object's 
    class. 
    @return the name of the calling object's class 
    */ 

     public String getClassName() 
     { 
     // Local data ... 
     String resultStr; 
         // Result of applying toString method to 
          // the calling object 
     int whereAt; // Where the @ symbol is in resultStr 

     // Logic ... 
     resultStr = this.toString(); 
     whereAt = resultStr.indexOf('@'); 
     resultStr = resultStr.substring(0,whereAt); 
     whereAt = resultStr.indexOf('.'); 
     return resultStr.substring(whereAt + 1); 
     } 

     // Abstract methods readIn, display, equals and clone. 
     // A direct subclass must implement these abstract methods 
    // unless the direct subclass is itself declared "abstract" 

     public abstract void readIn(); 

     public abstract void display(); 

     public abstract boolean equals(Element dobj); 

     public abstract Element clone(); 
    } 

這裏是子類人:

 package abstractclass; 

import java.util.Scanner; 
import java.util.*; 

    public class Person extends Element 
    { 
     private String name;   
     private String eMail;  

     public Person() 
     { 
     name = ""; 
     eMail = ""; 
     } 

     public Person (String aName) 
     { 
     name = new String(aName.toUpperCase()); 
     } 

     public Person (Person originalPerson) 
     { 
     name = new String(originalPerson.name); 
     eMail = new String(originalPerson.eMail); 
     } 

     public String getName() 
     { 
     return new String(name); 
     } 

     public String getEMail() 
     { 
     return new String(eMail); 
     } 

     public void setName(String aName) 
     { 
     name = new String(aName.toUpperCase()); 
     } 

     public void setEMail(String anEMail) 
     { 
     eMail = new String(anEMail); 
     } 

     public void readIn() 
     { 
     Scanner keyboard = new Scanner(System.in); 
     System.out.print("Name: "); 
     name = keyboard.nextLine().toUpperCase(); 
     System.out.print("E-Mail: "); 
     eMail = keyboard.nextLine(); 
     } 

     public void display() 
     { 
     System.out.println("Name: " + name); 
     System.out.println("E-mail: " + eMail); 
     } 

     public boolean equals(Element otherPerson) 
     { 
     return name.equals(((Person) otherPerson).name); 
     } 

     public Element clone() 
     { 
     Person theClone = new Person(); 
     theClone.name = new String(name); 
     theClone.eMail = new String(eMail); 
     return theClone; 
     } 

這裏是它給我的錯誤,當我嘗試編譯Person類:

Person.java:13: error: cannot find symbol 
    public class Person extends Element 
           ^
    symbol: class Element 
Person.java:147: error: cannot find symbol 
     public boolean equals(Element otherPerson) 
          ^
    symbol: class Element 
    location: class Person 
Person.java:158: error: cannot find symbol 
     public Element clone() 
      ^
    symbol: class Element 
    location: class Person 
3 errors 
+0

剛一說明:從'Object'類,而不是覆蓋'equals'方法實現你自己的。 –

+0

你是如何編譯的? –

回答

0

確保這兩個類都在編譯器所預期的名爲abstractclass的文件夾中。編譯器期望在查找相關類時文件夾名稱與包名稱匹配。

+0

這個伎倆!謝謝一堆!我很困惑,但爲什麼他們必須在名爲abstractclass的文件夾中? – user3150298

+0

對於單個類使用錯誤命名的包結構,您可以「離開」,但是當您需要在同一個包中找到第二個相關類時,編譯器將會失敗 - 希望這更清晰! – Reimeus

0

您需要在抽象類文件夾,把兩個文件,因爲你已經把

package abstractclass; 

兩個類