K
K
Koshkasobaka2020-02-14 20:31:54
Java
Koshkasobaka, 2020-02-14 20:31:54

Java. How to pass an object to a constructor?

Hello, I need to create an Employe class that, among others, has a Post field that contains the fields String title, String department, int salary. I don't understand how to pass this to the constructor, please help. And do I need to create a Post object in Main? If you can explain in a simpler way (I'm dumb)

public class Employee {
private String fullName;
private post post;
private int age;
private String phoneNomber;
private String email;

Employee(String fullName, HOW TO POST HERE? int age, String phoneNomber, String email) {
this.fullName = fullName;
AND HERE?
this.age = age;
this.phoneNomber = phoneNomber;
this.email = email; }

public String getFullname() { return fullName; }
public void setFullName() { this.fullName = fullName; }
public Post getPost() { return post; }
public void setPost() { this.post = post; }
public int getAge() { return age; }
public String getPhoneNomber() { return phoneNomber; }
public void setNomberPhone() { this.phoneNomber = phoneNomber; }
public String getEmail() { return email; }
public void setEmail() { this.email = email; }

public class Post {
private String title;
private String departure;
private int salary;

public Post(String title, String departament, int salary) {
this. title = title;
this.departament = departement;
this.salary = salary; }

public String getTitle() { return title; }
public void setTitle() { this.title = title; }
public String getDepartament() { return departament; }
public void setDepartament() { this.departament = departament; }
public int getSalary() { return salary; }
public void setSalary() { this.salary = salary; }

public void printInfo() {
System.out.print("position: " + title + ", department: " + departament + ", salary: " + salary);
}
}

public class Main {

public static void main(String[] args) {
Employee employee1 = new Employee("Stepanov Ilya Olegovich", 36, 89220098765L, "[email protected]");
}
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
constaprol, 2020-02-14
@Koshkasobaka

I would do this: I would take out the entire Post class in Post.java, well, I would add it to the Employee constructor

public class Employee {
    private String fullName;
    private Post post;
    private int age;
    private String phoneNomber;
    private String email;

    Employee(String fullName, Post post, int age, String phoneNomber, String email) {
        this.fullName = fullName;
        this.post = post;
        this.age = age;
        this.phoneNomber = phoneNomber;
        this.email = email;
    }

and the object was created like this
Employee employee1 = new Employee("Степанов Илья Олегович", new Post("title","department",10),20,"123123","aa");

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question