Java-在字段中获取值?

Java-在字段中获取值?,java,Java,我不知道该怎么说,但我想得到字段中某个值的值。以下是一些代码不要问我为什么有长度和高度: //person class public person(double w, double h, double l){ this.width = w; this.height = h; this.length = l; } //age class (extends person) public age(double w, double h,

我不知道该怎么说,但我想得到字段中某个值的值。以下是一些代码不要问我为什么有长度和高度:

//person class
public person(double w, double h, double l){
        this.width = w;
        this.height = h;
        this.length = l;
    }

//age class (extends person)
    public age(double w, double h, double l, int a) {
        super(w, h, l);
        this.age = a;
    }

//Creating the objects and putting them in an array (in the main class):
public person steve = new age(36.64, 185.64, 44.4, 26);
public person paul = new age(45.64, 178.64, 53.4, 47);

person[] people = new person[2];
people[0] = steve;
people[1] = paul;

现在我需要得到阵列中人员的年龄。您将如何做到这一点?

希望这有助于您理解:

class Main {
  public static void main(String[] args) {
    Person pixelGrid = new Person();
    pixelGrid.setName("Pixel Grid");
    pixelGrid.setAge(18);
    System.out.println(pixelGrid);
  }
}

class Person {
   protected String sName;
   protected int sAge;

   public void setName(String sName) {
      this.sName = sName;
   }

   public String getName() {
      return sName;
   }

   public void setAge(int sAge) {
      this.sAge = sAge;
   }

   public int getAge() {
      return sAge;
   }

   public String toString() {
      String s = "The Persons Name is: " + getName() + "\n"; 
      s += "Their Age is: " + getAge() + "\n";
      return s;
   }
}
输出:

The Person's Name is: Pixel Grid
Their Age is: 18

尝试直接从数组中检索年龄:

对主类的一些次要代码更改:

public static void main(String[] args) {
    // TODO Auto-generated method stub


        age steve = new age(36.64, 185.64, 44.4, 26);
        age paul = new age(45.64, 178.64, 53.4, 47);
        age[] people = new age[2];
        people[0] = steve;
        people[1] = paul;

        System.out.println(people[0].age);
        System.out.println(people[1].age);
}

为什么要创建具有年龄构造函数的人?您的年龄级别在哪里?似乎它延伸了人,它有接受者,接受者?发布itFYI的代码:类名以大写字母开头,即Person和Age。你现在想做什么?那么Age扩展Person?这没有多大意义。必须说明的是,建议使用getter和setter来检索实例变量-根据下面的SHSH678建议