2015-12-19 32 views
0

對於我們java類中的最終項目,我們可以做任何事情。我正在做一個臉部創作者,我想知道我將如何製作它,所以來自Questions類的字符串可以移動到繪畫類。這是這樣我可以做,所以我可以做如果string ...然後繪製...如何將變量從類移動到類

public class FinalProjectQuestions 
{ 
    public static void main(String []args) 
    { 
     Scanner in = new Scanner(System.in); 

     System.out.println("We will design the stick face of your dreams"); 

     System.out.print("Choose gender M for Male, or F for Female: "); 

     String gender = in.next(); 

     String hair = ""; 
     String haircolor = ""; 
     String eyecolor = ""; 
     String eyesize = ""; 
     String mouthsize = ""; 
     String m = "m"; 
     String m2 = "M"; 
     String femalehair = ""; 
     String femalehaircolor = ""; 
     String femaleeyecolor = ""; 
     String femaleeyesize = ""; 
     String femalemouthsize = ""; 
     String femalemakeup = ""; 
     if (gender.equals(m) || gender == "M") 
     { 
      System.out.print("Hair Length, Short, Mediuem, or Long: "); 
      hair = in.next(); 
      System.out.print("Hair Color, Red, Brown, Blond, Blue, Black, or Purple: "); 
      haircolor = in.next(); 
      System.out.print("Eye Color, Blue, Red, Brown, or Black: "); 
      eyecolor = in.next(); 
      System.out.print("Eye size, Big, Small, or Mediuem: "); 
      eyesize = in.next(); 
      System.out.print("Mouth Size, Big, Small, or Mediem: "); 
      mouthsize = in.next(); 
     } 

     else 
     { 
      System.out.print("Hair Length, Short, Mediuem, or Long: "); 
      femalehair = in.next(); 
      System.out.print("Hair Color, Red, Brown, Blond, Blue, Black, or Purple: "); 
      femalehaircolor = in.next(); 
      System.out.print("Eye Color, Blue, Red, Brown, or Black: "); 
      femaleeyecolor = in.next(); 
      System.out.print("Eye size, Big, Small, or Mediuem: "); 
      femaleeyesize = in.next(); 
      System.out.print("Mouth Size, Big, Small, or Mediem: "); 
      femalemouthsize = in.next(); 
      System.out.print("MakeUp, None, Little, Average, or Tons: "); 
      femalemakeup = in.next(); 
     } 
    } 
} 


public class FinalProjectMale 
{ 
    public void paintComponent(Graphics g) 
    { 
     Graphics2D g2 = (Graphics2D) g; 

     Ellipse2D.Double faceshape = new Ellipse2D.Double(100,100,100,100); 

     g2.draw(faceshape); 
    } 
} 
+0

哪裏是你的畫類? – SMA

+2

實際上,最好的方法是創建一個通用的Person類,並將所有變量放入其中,然後爲所有變量創建set方法。在此之後,您可以創建兩個課程「男」和「女」,並從「人」類延伸。 – Laerte

+0

不要在'gender ==「處用'=='做字符串比較M」'您可以在那個if語句中使用'gender.equalsIgnoreCase(「m」)''。 –

回答

0

這是一個很大的參數傳遞給一個新的類,但也有一些技巧來做到這一點。你可以創建一個具有所有這些屬性的Face類。最佳做法是use a Builder pattern創建Face對象。 Face對象然後可以作爲參數傳遞給Draw類,或者Face類可以有一個允許它自己繪製的函數。

0

簡單地這樣做:在這裏我只是告訴你如何封裝數據並將它傳遞給其他類以便進一步使用。希望這有助於你知道如何編寫代碼

創建一個Person類

public class Person { 
    private String hair; 
    private String hairColor; 
    private String eyeColor; 
    private int eyeSize; 
    private int mouthSize; 
    private Gender gender = Gender.MALE; // default is male 
    private String makeup; 

    public String getHair() { 
     return hair; 
    } 

    public void setHair(String hair) { 
     this.hair = hair; 
    } 

    public String getHairColor() { 
     return hairColor; 
    } 

    public void setHairColor(String hairColor) { 
     this.hairColor = hairColor; 
    } 

    public String getEyeColor() { 
     return eyeColor; 
    } 

    public void setEyeColor(String eyeColor) { 
     this.eyeColor = eyeColor; 
    } 

    public int getEyeSize() { 
     return eyeSize; 
    } 

    public void setEyeSize(int eyeSize) { 
     this.eyeSize = eyeSize; 
    } 

    public int getMouthSize() { 
     return mouthSize; 
    } 

    public void setMouthSize(int mouthSize) { 
     this.mouthSize = mouthSize; 
    } 

    public Gender getGender() { 
     return gender; 
    } 

    public void setGender(Gender gender) { 
     this.gender = gender; 
    } 

    public String getMakeup() { 
     return makeup; 
    } 

    public void setMakeup(String makeup) { 
     this.makeup = makeup; 
    } 

    public enum Gender { 
     MALE, FEMALE 
    } 
} 

等班

class OtherClass { 

    private Person person; 

    public OtherClass(Person person) { 
     this.person = person; 
     if (person.getGender() == Person.Gender.FEMALE) { 

     } 
    } 
} 

與用法:

public static void main(String[] args) { 
    Person male = new Person(); 
    male.setHair("HairColor"); 
    ... 
    Person female = new Person(); 
    female.setHair("HairColor"); 
    ... 

    // and pass it to the other class 
    OtherClass otherClass = new OtherClass(male); 
} 
相關問題