简体   繁体   中英

How would I move variables from class to class

For a final Project in our java class we can make anything. I am making a face creator, and I was wondering how would I make it so the Strings from the Questions class can move to the draw class. That's way I can make it so I can do if String is ... then draw...

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

That's a lot of parameters to pass to a new class, but there are some tricks to do this. You could create a Face class that had all these attributes. A best practice would be to use a Builder pattern to create the Face object. The Face object could then be passed as a parameter to the Draw class, or the Face class could just have a function that allows it to draw itself.

Simply do like this: Here I just show you how to encapsulate data and pass it to the other classes for farther use. Hope this help you to know how to write your code

Create a person class

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

And Other class

class OtherClass {

    private Person person;

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

        }
    }
}

and usage:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM