2017-12-14 6 views
0

私はJavaの方がむしろ初心者ですから、まだ1週間しか学習していないので、まだ経験がほとんどありません。私は多相に数日を費やし、親クラスを子クラスに拡張できることを知っていますが、祖父クラスに親クラスのすべての属性を持たせる方法を知りたいと思います。私は少しの研究をしましたが、私が探していたものは見つかりませんでした。私が取り組んでいるのは衣服の物を作ることです。私は、「Tシャツ」、「ショートパンツ」、「サンダル」などの子供が多い「衣服」の3つの親の「Upper_wear」、「Lower_wear」、「Shoes」という祖父母を抱えています。祖父母クラスのすべての属性を取得せずに、すべての親クラスを1つの祖父クラスに入れる方法はありますか?

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

私はすべての変数で服を拡張しない理由があるIドン理由'Upper_wear'に 'Shoes'の変数である 'Shoe_laces'が含まれているかどうかを確認します。しかし、私はランクラスに行くので、すべての親クラスを1つに集めたい。 forループでは、親クラスだけでなく、Clothingのすべてのアイテムの価格を列挙したいと思います。私は私が唯一、このような私が現在持っているものとして、一度に一つの親クラスを反復処理に制限されていていることを感じる:

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」のすべての属性を持つが、保護者がすべての属性を持つことはできません。そのような靴に固有の属性は、私は他の2つの親のためにそれが靴に特有の方法を繰り返すとき空であることを望む。私が探しているものができるかどうかは分かりません。私が探しているものを理解できない場合は、混乱して申し訳ありません。これを読んで助けていただきありがとうございます。

答えて

0

あなたがしようとしているのは、多態性の古典的な応用です。ちょうどいくつかの概念を明確にする必要があります。

祖父母には、アイテムID、名前、色、価格など、すべての子に共通のすべての属性が含まれます。また、print()関数などの一般的な関数も含める必要があります。あなたのメインで必要なものです。

すべての子供(親を含む)は、上のフード/襟やジャケットの内張りなど、そのクラスの属性を紹介します。また、必要に応じてカスタマイズする必要がある機能をオーバーライド(独自の実装を提供)します。したがって、あなたのケースでは、衣類にprint()関数がありますが、各サブクラスには独自の実装があり、ジッパーや靴ひもの数などの独自のプロパティがすべて印刷されます。

最後に、あなたのメインには、あなたが望むすべてのタイプのオブジェクトへの参照を含む衣服タイプのリストがあります。親は、子タイプのオブジェクトを指すことができます。たとえば、

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

動的多型について読んでみることをお勧めします。 This linkには簡単な紹介と素晴らしい例が含まれています。

関連する問題