Java中set和get
public?class?HelloWorld?{
???
???
???String?name;?
???String?sex;
???int?score;
???int?age;
?????
???public?String?getName()?{
return?name;
}
???public?void?setName(String?name)?{
this.name?=?name;
}
???public?String?getSex()?{
return?sex;
}
???public?void?setSex(String?sex)?{
this.sex?=?sex;
}
???public?int?getScore()?{
return?score;
}
???public?void?setScore(int?score)?{
this.score?=?score;
}
???public??int?getAge()?{
return?age;
}
???public??void?setAge(int?age)?{
this.age?=?age;
}
public?static?void?main(String[]?args)?{
HelloWorld?hello?=?new?HelloWorld();
hello.setName("imooc");
???????hello.setSex("Male");
???????hello.setAge(10);
???????hello.setScore(98);
???????
???????
???????hello.getName();
???????hello.getSex();
???????hello.getAge();
???????hello.getScore();
???????
}
}請問一下我寫的代碼為什么不能輸出?
2017-01-02
因為你return返回的是一個值,并不是輸出一個值,所以你還要用輸出語句,即System.out.print();來輸出屬性的值。System.out.print(hello.getName()+hello.getSex()+hello.getAge()+hello.getScore());
2017-02-09
你的變量沒有用private修飾,不需要用get() , set()方法??!你沒有對面變量進行封裝,完全可以直接操作啊!
2017-01-02
小伙 沒有輸出方法。
2017-01-02
System.out.println()都沒用,當然顯示不了