简体   繁体   中英

How to pass variables between classes?

I am very new to Java and have searched through previously asked questions which haven't helped my issue...

How do I pass the int variable age, and the String name, from the main class to the Person class?

package engineers;

import java.util.Scanner;


public class Engineers {

    public static void main(String[] args) {
        // TODO code application logic here

        Scanner scan = new Scanner(System.in); //declaration and ssignment of scanner
        System.out.println("Enter name: "); //name prompt
        String name = scan.next(); //reads name input from user

        System.out.println("Enter age: "); //age prompt
        int age = scan.nextInt(); //reads age input from user

        System.out.println("Pay check amount: "); 
        double salary = scan.nextDouble();
        Person person = new Person();


    }

    private static class Person {
        /**
         * A default constructor
         */
        public Person() {
        }
        /**
         * method for displaying the computation result
         */

       double avgSalary =  40000 * Math.sqrt(Math.exp(0.04295 * age + 0.141) );


        public void showData(int age, double pay){
            System.out.printf( "%s earns $%4.3f for age %3d.\n", name, (pay*Math.sqrt(age)*.15), age); 
        }//end method showData
    }

}

You should use Person constructor like this:

 public Person(int age, String name) {
    this.age = age;
    this.name = name;
 }

And add two fields in your Person class, before your constructor:

private int age;
private String name;

Then I am assuming you need to use this variables inside the person class, in order to do this, you can do something like this:

double avgSalary =  40000 * Math.sqrt(Math.exp(0.04295 * this.age + 0.141) );

To reference your variable:

public void showData(){
    System.out.printf( "%s earns $%4.3f for age %3d.\n", this.name, (pay*Math.sqrt(this.age)*.15), this.age); 
}//end method showData

Finnaly, you need to instantiate your Person object to use it:

Person p = new Person(name, age);

I would also recommend (since you are learning java), to understand the difference between getters/setters and constructor approach: Setter methods or constructors

You mean something like this?

int age;
String name;

class Person(int age, String name) {
        this.age = age;
        this.name = name;
}

Or (And)

void setAge(int age) {
    this.age = age;
}

void setName(String name) {
    this.name = name;
}

You would want to define a constructor with any argument another class may need for the most basic functioning. A Person istance would be initialized with a name and age (even though It would be better to store a birth date and calculate the age against the current date).

To do this, you'd need the class Person to look like this:

public class Person{
    private String name;
    private Date birthdate;

    //Constructor method
    public Person(String n, Date bd){
        name=n;
        birthdate=bd;
    }
    //Other methods
}

In the main() method, you would then create a new istance of Person , after getting anything needed, like this:

Person p = new(name, date);

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