質問

I am new to this concept, i need a bit understanding the use of getter and setter methods in following situation.

I have three classes as below.

1) ModelClass.java, where i have getter and setter methods.

public class ModelClass {

private String name;
private int age;
private String address;

public ModelClass(){

}

public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public int getAge() {
    return age;
}
public void setAge(int age) {
    this.age = age;
}
public String getAddress() {
    return address;
}
public void setAddress(String address) {
    this.address = address;
}

}

2) StaffInformation.java where i have set the particular staff information through setter method.

  public class StaffInformation {

public StaffInformation(){

    ModelClass mc = new ModelClass();
    mc.setName("Khan");
    mc.setAge(30);
    mc.setAddress("NY");

}

 }

3) Main.java, Here i want to print out StaffInformation through getter methods which i have in ModelClass.java. Something like this.

    public class Main {

public static void main(String[] args) {
            StaffInformation staff = new StaffInformation();
        ModelClass mc = new ModelClass(); //here is problem
    System.out.println(mc.getAge());
    System.out.println(mc.getName());
    System.out.println(mc.getAddress());

}

  }

How get off this...Thanks

役に立ちましたか?

解決

problem with your code you are creating multiple Model objects try this code

   public StaffInformation(){
           ModelClass mc = new ModelClass();
            mc.setName("Khan");
            mc.setAge(30);
            mc.setAddress("NY");
            Main.displayData(mc);

        }

Here is your main class

  class Main{
    public static displayData(ModelClass mc)
    {
        System.out.println(mc.getAge());
        System.out.println(mc.getName());
        System.out.println(mc.getAddress());

    }
    public static void main(String[] args) {
    StaffInformation sc=new StaffInformation();
    }
    }

他のヒント

Based on the code you show, the only place where you set name, age, or address is in the constructor to StaffInformation. But you also never instantiate StaffInformation, so all the fields in Model are left as their default values -- 0, null, and null, respectively.

StaffInformation is creating the ModelClass but not making it visible. Change StaffInformation to

public class StaffInformation {

    public StaffInformation(){
    }

    public ModelClass createModel() {
        ModelClass mc = new ModelClass();
        mc.setName("Khan");
        mc.setAge(30);
        mc.setAddress("NY");
        return mc;
    }

}

and change your main to use the ModelClass created:

class Main{
    public static void displayData(ModelClass mc)
    {
        System.out.println(mc.getAge());
        System.out.println(mc.getName());
        System.out.println(mc.getAddress());

    }

    public static void main(String[] args) {
        StaffInformation sc= new StaffInformation();
        ModelClass mc = sc.createModel();
        Main.displayData(mc);
    }
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top