我如何将变量从一个类移动到另一个类

时间:2015-12-19 17:26:02

标签: java string

对于我们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);
    }
}

2 个答案:

答案 0 :(得分:0)

要传递给新类的参数很多,但有一些技巧可以做到这一点。您可以创建具有所有这些属性的Face类。最佳做法是use a Builder pattern创建Face对象。然后可以将Face对象作为参数传递给Draw类,或者Face类可以只有一个允许它自己绘制的函数。

答案 1 :(得分:0)

简单地这样做:这里我只是向您展示如何封装数据并将其传递给其他类以供进一步使用。希望这有助于您了解如何编写代码

创建人员课程

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