代码之家  ›  专栏  ›  技术社区  ›  Ashley Bahora

如何在一个祖父母类中包含所有父类,而不获取祖父母类的所有属性?

  •  0
  • Ashley Bahora  · 技术社区  · 7 年前

    我对Java相当陌生,只需要一周的学习时间,所以我仍然非常缺乏经验。我花了几天时间研究多态性,知道可以将父类扩展到子类,但我想知道如何使祖父母类具有父类的所有属性。我做了一些研究,但没有找到我想要的。我正在做的是创造衣服的物体。我有一个祖父母是“衣服”,三个父母是“上装”、“下装”和“鞋子”,还有很多孩子,比如“t恤”、“短裤”和“凉鞋”。目前,我的家长代码是:

    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;
        }
    }
    

    对于儿童守则,我有:

    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”,这是“Shoes”中的一个变量。然而,我想把所有的父类集中到一个类中,因为当我进入run类时。在for循环中,我想列出每件衣服的价格,而不仅仅是一个父类的价格。我觉得我一次只能迭代一个父类,比如我目前拥有的:

    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”的每一个属性,而不是父母拥有衣服的每一个属性。因此,对于特定于鞋子的属性,我希望在迭代特定于鞋子的方法时,其他两个父对象为空。我不确定我要找的东西是否有可能做到。如果你不明白我在找什么,很抱歉让你困惑。谢谢你花时间阅读并帮助我。

    1 回复  |  直到 7 年前
        1
  •  0
  •   Brishna Batool    7 年前

    您试图做的是多态性的一个经典应用。你只需要澄清几个概念。

    您的祖父母将包含所有子项通用的所有属性,如项目ID、名称、颜色、价格等。它还应包含通用函数,如print()函数,这是您在main中需要的。

    所有的孩子(包括家长)都会在课堂上介绍他们的特定属性,例如鞋面的风帽/领子,夹克的内衬。他们还将覆盖(提供自己的实现)需要根据需要定制的功能。因此,在您的例子中,虽然衣服将有一个print()函数,但每个子类都有其自己的实现,在其中它将打印自己的所有属性,例如拉链、鞋带的数量。

    最后,在main中,您将有一个类型衣服列表,其中将包含对您想要的所有类型的对象的引用。父对象可以指向子类型的对象。例如

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

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