我对Java很陌生,而且只有一周的学习时间,所以我仍然非常缺乏经验。我花了几天时间讨论多态,并知道我可以将父类扩展到子类,但我想知道如何让祖父类具有父类的所有属性。我做了一些研究,但没有找到我想要的东西。我正在做的是创造衣服的对象。我有一个祖父母,这是'服装'三个父母' Upper_wear',' Lower_wear'和' Shoes'有很多孩子,比如“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'这是鞋子中的一个变量。然而,我想将所有父类聚合成一个,因为当我去运行课时。在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'和'鞋子'的所有属性,但不能让父母拥有服装的所有属性。这样,鞋子特有的属性,我希望其他两个父母在迭代鞋子特有的方法时是空白的。我不确定我所寻找的东西是否可行。如果你无法理解我在寻找什么,我很抱歉让你感到困惑。感谢您抽出宝贵时间阅读并帮助我。
答案 0 :(得分:0)
您要做的是多态性的经典应用。你只需要澄清一些概念。
您的祖父母将包含所有儿童共有的所有属性,例如商品ID,名称,颜色,价格等。它还应包含常用功能,例如print()函数,您需要的主要内容。
所有儿童(包括父母)都会在他们的课程中介绍他们的特定属性,例如鞋面罩/衣领和夹克内衬。他们还将根据他们的需要覆盖(提供他们自己的实现)所需的功能。所以,在你的情况下,虽然服装将有一个print()函数,每个子类将有自己的实现,在其中它将打印所有自己的属性,如拉链,鞋带的数量。
最后,在您的主页中,您将获得一个类型服装列表,其中包含对您想要的所有类型的对象的引用。父级可以指向子类型的对象。例如,
Clothing c = new Jacket(...);
c.print(); // This will call the print() of class Jacket, not Clothing
我建议阅读动态多态性。 This link包含一个简短的介绍和一个漂亮的例子。