2

私は Java のクラスを受講していますが、この質問は私が完了する予定の演習の 1 つに関連しています。抽象スーパークラスの 2 つのサブクラスから作成されたオブジェクトの配列の内容を出力しようとしています。オブジェクトを作成して配列に格納することはできますが、配列の内容を出力すると、スーパークラスの「年齢」属性と「体重」属性の最後のインスタンスしか取得できません。ご覧のとおり、これらはプライベート属性です。オブジェクトの作成時にこれらの属性の値にアクセスする方法はありますか? 私はかなりの量の読書をしましたが、それができるかどうかについて混乱しています。できる場合はどうすればよいですか? 私のコード:

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() メソッドを使用すると、各オブジェクトに入力された値が取得されず、属性の最後の値のみが取得されます。どんな助けでも大歓迎です。乾杯。

4

2 に答える 2

1

年齢と体重に静的変数を使用しないでください。

private static int age;
private static double weight;

これらの変数はインスタンス変数ではなくクラス変数であるため、静的変数の値はこのタイプのすべてのオブジェクトで同じです。これらの人は、このクラスの各インスタンス(または子クラスのインスタンス)に対して一意になるインスタンスまたは非静的フィールドである必要があります。

次に、子クラスで、これらのシャドウ変数を削除します。これは、親クラスの同様の名前のフィールドをシャドウするためです。

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 を使用してください。

于 2013-11-08T02:09:44.030 に答える
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");
        }
    }
于 2013-11-08T02:41:39.433 に答える