子类给我抽象类的“找不到符号”错误

时间:2014-01-06 22:39:33

标签: java inheritance abstract

所以我带了一些comp sci教授的示例类,并尝试编译它们以了解更多信息,但是我得到了“找不到符号”错误。所有文件都在同一个文件夹中。任何人都可以告诉我如何解决这个或正在发生的事情?

这是抽象类:

    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();
   }

这是子类Person:

        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.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

2 个答案:

答案 0 :(得分:0)

确保两个类都在编译器所期望的名为abstractclass的文件夹中。在查找依赖类时,编译器期望文件夹名称与包名称匹配。

答案 1 :(得分:0)

您需要将这两个文件放在abstractclass文件夹中,因为您已经放了

package abstractclass;

这两个类