如何在一个grandparentclass中拥有所有的parentclass而不获得grandparentclass的所有属性?

How to have all parent classes in one grandparent class without gaining all attributes of grandparent class?

我对 Java 比较陌生,只学了一个星期,所以我还很缺乏经验。我在多态性上花了几天时间,知道我可以将 parent class 扩展为 child class,但我想知道如何拥有一个 grandparent class 具有 parent class 的所有属性。我做了一些研究,但没有找到我要找的东西。我正在做的是制作 objects 的衣服。我有一个 grandparent 是 'Clothing' 三个 parents 'Upper_wear', 'Lower_wear', and 'Shoes' 还有很多 children 这样的如 't-shirts'、'shorts' 和 'sandals'。目前我在 parents 代码中的内容是:

public class Upper_wear
{
    private String fabric;
    private int numb_zippers;
    private String draw_string;
    private int numb_pockets;
    private int size;
    private String color;
    private double length_sleeves;
    private int length_shirt;
    private String collar;
    private String hood;
    private int code;
    private double price;
    private String name;
    Upper_wear(String fabric,int numb_zippers,String draw_string,int numb_pockets,int size,String color, double length_sleeves, int length_shirt, String collar, String hood, int code, double price, String name){
        this.fabric = fabric;
        this.numb_zippers = numb_zippers;
        this.draw_string = draw_string;
        this.numb_pockets = numb_pockets;
        this.size = size;
        this.color = color;
        this.length_sleeves = length_sleeves;
        this.length_shirt = length_shirt;
        this.collar = collar;
        this.hood = hood;
        this.code = code;
        this.price = price;
        this.name = name;

    }
    public String get_fabric(){
        return fabric;
    }
    public int get_numb_zippers(){
        return numb_zippers;
    }
    public String get_draw_string(){
        return draw_string;
    }
    public int get_numb_pockets(){
        return numb_pockets;
    }
    public int get_size(){
        return size;
    }
    public String get_color(){
        return color;
    }
    public double get_length_sleeves(){
        return length_sleeves;
    }
    public int get_length_shirt(){
        return length_shirt;
    }
    public String get_collar(){
        return collar;
    }
    public String get_hood(){
        return hood;
    }
    public int get_code(){
        return code;
    }
    public double get_price(){
        return price;
    }
    public String get_name(){
        return name;
    }
}

对于 children 的代码,我有:

public class Jacket extends Upper_wear
{
    Jacket(String fabric,int numb_zippers,String draw_string,int numb_pockets,int size,String color, double length_sleeves, int length_shirt, String collar, String hood, int code, double price, String name){
        super(fabric, numb_zippers, draw_string, numb_pockets, size, color, length_sleeves, length_shirt, collar, hood, code, price, name);
    }
}

我之所以不只是用所有变量来扩展服装,是因为我不想说明 'Upper_wear' 是否有 'Shoe_laces',这是 [=30] 中的一个变量=].然而,我想把所有 parent class 都收集起来,因为当我去 运行 class 时。在 for 循环中,我想列出每件衣服的价格,而不仅仅是 parent class 的价格。我觉得我一次只能迭代一个 parent class,例如我目前拥有的:

public class Run
{
    public static void main (String[]args){
        Shoes Tennis_shoes_01 = new Shoes("Canvas", 0, "yes", 10, "red and white", 0,0.5,2.5, 00001, 750.99,"Tenny shoey");
        Upper_wear T_shirt_01 = new Upper_wear("Cotton", 0, "no", 0, 14, "yellow", 14.5, 15, "v-neck", "no", 00002, 990.50, "Yel-ow");)

        Shoes[]In_Stock = {Tennis_shoes_01};
        Upper_wear[]In_Stock_upper = {};
        Lower_wear[]In_Stock_lower = {};


        System.out.println("Price");
        System.out.println("-------");
        for(Shoes x : In_Stock){
            System.out.println(x.get_name() + ": " +x.get_price());
        }
        for(Upper_wear x : In_Stock_upper){
            System.out.println(x.get_name() + ": " + x.get_price());
        }


    }

我想要的是更像这样的东西:

public class Want_run
{
    public static void main(String[]args){
        Clothing Tennis_shoes_01 = new Shoes("Canvas", 0, "yes", 10, "red and white", 0,0.5,2.5, 00001, 750.99,"Tenny shoey");
        //Not sure if this is possible to have a class that's different than the constructor but I am looking for it to come from clothing class with properties of Shoes.
        Clothing T_shirt_01 = new Upper_wear("Cotton", 0, "no", 0, 14, "yellow", 14.5, 15, "v-neck", "no", 00002, 990.50, "Yel-ow");
        //So I want all properties to be in clothing but the ones that the childeren don't have I want to be just blank.ex. Upper_wear is blank on the shoe_laces. 
        Clothing[]In_Stock = {Tennis_shoes_01, T_shirt_01};
        //I really want everything to be just in one list to iterate through but I can't currently do that with multiple parents of my knowledge.
        for(Clothing x : In_Stock){
            System.out.println(x.get_name() + ": " + x.get_price());
        }
        //this way I have only one for loop for every item,and for parents that don't have 'price' I am hoping would just not print.
    }
}

所以我希望服装具有 'Upper_wear'、'Lower_wear' 和 'Shoes' 的所有属性,而不是 parent 具有服装的所有属性。这样特定于鞋子的属性,我希望在循环遍历特定于鞋子的方法时,其他两个 parent 为空。我不确定我正在寻找的东西是否有可能做到。如果您不明白我在寻找什么,很抱歉让您感到困惑。感谢您花时间阅读本文并帮助我。

您要做的是 class多态性的 ic 应用。你只需要澄清几个概念。

你的大 parent 将包含所有 children 共有的所有属性,例如项目 ID、名称、颜色、价格等。它还应该包含共同的函数,例如 print() 函数,这是您在 main 中需要的函数。

所有child人(包括parent人)都会在class人里面介绍自己的具体属性,比如hood/collar是鞋面,内里是夹克.他们还会覆盖(提供自己的实现)他们需要根据需要定制的功能。因此,在您的情况下,虽然 Clothing 有一个 print() 函数,但每个子 class 都有自己的实现,其中它将打印自己的所有属性,例如拉链数、鞋带数。

最后,在您的 main 中,您将有一个 Clothing 类型的列表,其中将包含对 object 所需所有类型的引用。 parent 可以指向 child 类型的 object。例如,

Clothing c = new Jacket(...);
c.print();    // This will call the print() of class Jacket, not Clothing

我建议阅读动态多态性。 This link 包含快速介绍和漂亮的示例。