2017-12-14 4 views
0

나는 자바에 익숙하지 않고 일주일에 한 번 배우기 때문에 아직 미숙하다. 다형성에 며칠을 보냈지 만 부모 클래스를 자식 클래스로 확장 할 수 있다는 것을 알고 있지만 조부모 클래스에 부모 클래스의 모든 특성을 갖는 방법을 알고 싶습니다. 나는 약간의 연구를했으나 내가 찾고있는 것을 찾지 못했습니다. 내가하고있는 일은 옷의 물건을 만드는 것입니다. 나는 'T 셔츠', '반바지', '샌들'과 같은 많은 아이들과 함께 '의류'세 부모 'Upper_wear', 'Lower_wear'및 'Shoes'라는 조부모가 있습니다.조부모 클래스의 모든 속성을 얻지 않고 한 조부모 클래스의 모든 상위 클래스를 유지하는 방법은 무엇입니까?

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'에 'Shoes'의 변수 인 'Shoe_laces'가 있는지 여부를 표시하고 싶지 않습니다. 그러나 나는 모든 학급을 하나로 모으고 싶습니다. 왜냐하면 제가 달리기 실에 갈 때입니다. for 루프에서는 부모 클래스뿐만 아니라 모든 의류 항목의 가격을 나열하고 싶습니다. 나는 그런 내가 현재 가지고있는 것처럼 한 번에 하나 개의 부모 클래스를 통해 반복으로 제한하고 있다고 생각 : 그래서

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

내가 원하는 :

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


    } 

내가 걸려 라하는 것은 더이 같은 것입니다 의류는 'Upper_wear', 'Lower_wear'및 'Shoes'의 모든 속성을 갖지만 의류의 모든 속성을 갖는 것은 아닙니다. 신발에 고유 한 속성이 있으므로 신발과 관련된 메소드를 반복 할 때 다른 두 부모에게는 공백으로 남기를 원합니다. 내가 뭘 찾고 있는지조차 모르겠다. 내가 무엇을 찾고 있는지 이해할 수 없다면 혼란 스럽습니다. 이것을 읽고 도움을 주셔서 감사합니다.

답변

0

당신이하려는 것은 다형성의 고전적인 응용 프로그램입니다. 당신은 단지 몇 가지 개념을 명확히 할 필요가 있습니다.

부모님은 항목 ID, 이름, 색상, 가격 등과 같이 모든 어린이에게 공통적 인 모든 속성을 포함합니다. 또한 print() 함수와 같은 일반적인 함수도 포함해야합니다. 당신이 당신의 메인에서 필요로하는 것입니다.

모든 어린이 (학부모 포함)는 윗층을위한 두건/고리 및 자켓을위한 안감과 같은 자신의 수업에서 자신의 구체적인 속성을 소개합니다. 또한 필요에 따라 사용자 정의해야하는 기능을 재정의 (자체 구현 제공)합니다. 따라서 의류에는 print() 함수가 있지만 각 하위 클래스에는 자체 구현이 있으므로 지퍼 수, 신발 끈 수와 같은 고유 한 속성이 모두 인쇄됩니다.

마지막으로 주에서는 원하는 모든 유형의 객체에 대한 참조를 포함하는 Clothing 유형 목록을 갖게됩니다. 부모는 자식 유형의 객체를 가리킬 수 있습니다. 예 :

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

동적 다형성을 읽는 것이 좋습니다. This link에는 간단한 소개와 좋은 예가 들어 있습니다.

관련 문제