对于我的任务,我应该:“为您的父类和子类编写代码,并编写驱动程序类以演示应用程序的方法和变量。在您的驱动程序中,包括一个数组来存储对象的所有子类。使用循环来打印每个对象的相关值和/或使用其方法。”

好吧,这是我到目前为止所做的:

狗(抽象类):

    package objectsofaffection;

    public abstract class Dog extends Test {

        static String name;
        static String age;
        static String coatColor;

        public abstract void name();
        public abstract void age();
        public abstract void coatColor();
}


DogBetty:

package objectsofaffection;

public class DogBetty extends Dog {

    public DogBetty() {
        name();
        age();
        coatColor();
    }


    @Override
    public void name() {
        System.out.println("Dog Name: Betty");
        }
    @Override
    public void age() {
        System.out.println("Age: 11 years");
        }
    @Override
    public void coatColor() {
        System.out.println("Coat Color: Brown");

        }
    }


DogCarl:

package objectsofaffection;

public class DogCarl extends Dog {

    public DogCarl() {
        name();
        age();
        coatColor();
    }


    @Override
    public void name() {
        System.out.println("Dog Name: Carl");
        }

    public void age() {
        System.out.println("Age: 2 months");
        }

    public void coatColor() {
        System.out.println("Coat Color: Black");

        }
    }


DogJoe:

package objectsofaffection;

public class DogJoe extends Dog {

    public DogJoe() {
        name();
        age();
        coatColor();
    }


    @Override
    public void name() {
        System.out.println("Dog Name: Joe");
        }

    public void age() {
        System.out.println("Age: 6 years");
        }

    public void coatColor() {
        System.out.println("Coat Color: Spotted Black and White");

        }
    }


DogMollie:

package objectsofaffection;

public class DogMollie extends Dog {

    public DogMollie() {
        name();
        age();
        coatColor();
    }


    @Override
    public void name() {
        System.out.println("Dog Name: Mollie");
        }

    public void age() {
        System.out.println("Age: 3 years");
        }

    public void coatColor() {
        System.out.println("Coat Color: Black, Brown, White mix.");

        }
    }


测试(驱动程序类):

package objectsofaffection;

public class Test {

    @SuppressWarnings("unused")
    public static void main(String[] args) {

        DogBetty betty = new DogBetty();
            System.out.println(); //space

        DogJoe joe = new DogJoe();
            System.out.println(); //space

        DogCarl carl = new DogCarl();
            System.out.println(); //space

        DogMollie mollie = new DogMollie();
            System.out.println(); //space

    }
}


那么,我怎么可能使用数组呢?请帮忙。

最佳答案

首先,您没有为每只狗创建子类,抽象类代表类型,子类代表子类型。因此,如果您的Dog是父类,则您的子类可以是品种,并且对于每条狗,您都将其作为对象。

Dog-> germanShepard extends Dog-> germanShepard dogMollie = new germanShepard();

然后,您可以使用germanShepard类型的ArrayList:ArrayList<germanShepard> dogs = new ArrayList<germanShepard>();

dogs.add(dogMollie); ArrayList是动态数据结构,因此可以根据您的需要进行扩展

但是如果要使用数组,

germanShepard[] dogs;

dogs =new germanShepard[10];

dogs[0] = new germanShepard(name,age,coatColor);

09-11 19:37