我正在上Java课,这个问题与我要完成的一个练习有关。我试图打印出从抽象超类的2个子类创建的对象数组的内容。我能够创建对象并将它们存储在一个数组中,但是当我打印出数组的内容时,我只能获得超类的“age”和“weight”属性的最后一个实例。如您所见,它们是私有属性。有没有办法在创建对象时访问这些属性的值?我做了一些阅读,我很困惑,我是否可以做到,如果可以,那怎么样? 我的代码:
public abstract class Parent {
private static int age;
private static double weight;
public Animal(int age, double weight) {
this.age = age;
this.weight = weight;
}
public static int getAge() {
return age;
}
public static double getWeight() {
return weight;
}
}
public class Child1 extends Parent {
private String name, owner, petInfo;
protected int age;
protected double weight;
public Child1(int age, double weight, String name, String owner) {
super(age, weight);
this.name = name;
this.owner = owner;
}
public String toString() {
petInfo = "Pet's name: " + this.getName() + "\nPet's age: " + getAge() + " years\nPet's weight: " + getWeight() + " kilos\nOwner's name: " + this.getOwner();
return petInfo;
}
}
public class Child2 extends Parent {
public String wildInfo;
public Child2(int age, double weight) {
super(age, weight);
}
public String toString() {
wildInfo = "The wild animal's age: " + getAge() + "\nThe wild animal's weight: " + getWeight();
return wildInfo;
}
}
public class Console {
public static void main(String[] args) {
Parent ref[] = new Parent[5];
for(i = 0; i < 5; i++) {
//user input here
Child1 pet = new Child1(age, weight, name, owner);
ref[i] = pet;
//more user input
Child2 wild = new Child2(age, weight);
ref[i] = wild;
}
//print contents of array
for(Parent item : ref)
System.out.println("\n" +item.toString()+ "\n");
我的理解是我只能通过方法访问超类的属性。当我在toString()中使用getAge()和getWeight()方法时,我没有得到为每个对象输入的值,只有属性的最后一个值。 任何帮助将不胜感激。欢呼声。
答案 0 :(得分:1)
不要对年龄和体重使用静态变量:
private static int age;
private static double weight;
静态变量的值对于此类型的所有对象都是相同的,因为这些变量是 class 变量,而不是实例变量。这些人应该是实例或非静态字段,这将使它们对于此类的每个实例(或子类的实例)都是唯一的。
然后在你的Child类中,去掉这些阴影变量,因为它们会遮蔽Parent类中类似命名的字段:
public class Child1 extends Parent {
private String name, owner, petInfo;
protected int age; // ***** get rid of, since it shadows
protected double weight; // ***** get rid of, since it shadows
相反,无论你在哪里使用它们,都要使用Child类中的getter和setter。
答案 1 :(得分:0)
上述程序中存在错误。
将父类中的静态方法更正为非静态方法。
示例代码:
import java.util.Scanner;
abstract class Parent {
private int age;
private double weight;
public Parent(int age, double weight) {
this.age = age;
this.weight = weight;
}
public int getAge() {
return age;
}
public double getWeight() {
return weight;
}
}
class Child1 extends Parent {
private String name, owner, petInfo;
public Child1(int age, double weight, String name, String owner) {
super(age, weight);
this.name = name;
this.owner = owner;
}
public String toString() {
petInfo = "Pet's name: " + this.getName() + "\nPet's age: " + getAge() + " years\nPet's weight: " + getWeight() + " kilos\nOwner's name: " + this.getOwner();
return petInfo;
}
public String getName() {
return name;
}
public String getOwner() {
return owner;
}
}
class Child2 extends Parent {
public String wildInfo;
public Child2(int age, double weight) {
super(age, weight);
}
public String toString() {
wildInfo = "The wild animal's age: " + getAge() + "\nThe wild animal's weight: " + getWeight();
return wildInfo;
}
}
public class Console {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Parent ref[] = new Parent[2];
//int weight=10;
//int age=5;
//String name="parrot";
//String owner="rajesh";
for(int i = 0; i < 2; i++) {
System.out.println("Enter the name");
String name=in.next();
System.out.println("Enter the age ");
int age=in.nextInt();
System.out.println("Enter the weight");
double weight=in.nextDouble();
System.out.println("Enter the owner");
String owner=in.next();
//user input here
Child1 pet = new Child1(age, weight, name, owner);
ref[i] = pet;
//more user input
if (i==1)
{
break;
}
System.out.println("Enter the name");
name=in.next();
System.out.println("Enter the age ");
age=in.nextInt();
System.out.println("Enter the weight");
weight=in.nextDouble();
System.out.println("Enter the owener");
owner=in.next();
Child2 wild = new Child2(age, weight);
ref[++i] = wild;
}
//print contents of array
for(Parent item : ref)
System.out.println("\n" +item.toString()+ "\n");
}
}