我的一个类中的一个变量"无法解析或不是一个字段"

Len*_*xus 2 java eclipse inheritance static object

这是我的类Person.java(简化为删除文本,我认为与问题无关):

public class Person {
    int myIdNumber;
    String myName;
    String myBirthday;
    String myType;
    Person(String forTheName, int forTheId, String forTheBirthday, String forTheType){
        this.myIdNumber = forTheId;
        this.myName = forTheName;
        this.myBirthday = forTheBirthday;
        this.myType = forTheType;
    }
}
Run Code Online (Sandbox Code Playgroud)

这里是PersonAdd.java(同样简化):

import javax.swing.JOptionPane;
public class PersonAdd {
    public static int numOfPeople = 0;
    static String instruction = "Enter the person's ";
    static String theName;
    static String theBirthday;
    static String theType;
    static void entryText(){
        numOfPeople++;
        theName = JOptionPane.showInputDialog(null, instruction + "name.", "Add People", JOptionPane.QUESTION_MESSAGE);         
        theBirthday = JOptionPane.showInputDialog(null, instruction + "birthday.", "Add People", JOptionPane.QUESTION_MESSAGE);
        theType = JOptionPane.showInputDialog(null, instruction + "type.", "Add People", JOptionPane.QUESTION_MESSAGE);
    }

    public static void main(String[] args) {
            entryText();
            Object person1 = new Person(theName, numOfPeople, theBirthday, theType);
            entryText();
            Object person2 = new Person(theName, numOfPeople, theBirthday, theType);
            entryText();
            Object person3 = new Person(theName, numOfPeople, theBirthday, theType);
            String response = person1.myName;
            JOptionPane.showMessageDialog(null, response);
        }
    }
Run Code Online (Sandbox Code Playgroud)

预期的结果是最后一个对话框显示给定的名称,但它不起作用,虽然我相信它存储正确输入的数据.关键问题在于

String response = person1.myName;
Run Code Online (Sandbox Code Playgroud)

无法解决或不是字段.如果我添加一个get方法并使用它而不是myName,也会发生这种情况.Eclipse甚至似乎无法看到person1的任何对象.

我确定这与我无法理解继承,静态/非静态或其他东西有关.(这种类和对象的东西对我来说特别棘手;我认为在"SQL模式"中并希望能够说出"select-from-where"之类的内容.)

Sot*_*lis 6

您已将变量声明为类型Object,它是每种引用类型的基本类型,即.它是你Person班级的父班.

因此,您只能访问通过该Object类型可用的字段和方法.

您需要将变量声明为类型 Person

Person person1 = new Person(...);
Run Code Online (Sandbox Code Playgroud)

或在使用之前转换变量

String response = ((Person) person1).myName;
Run Code Online (Sandbox Code Playgroud)

另外,请注意访问修饰符.