慕粉1143028379
2017-01-02 18:45
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();
}
}请问一下我写的代码为什么不能输出?
因为你return返回的是一个值,并不是输出一个值,所以你还要用输出语句,即System.out.print();来输出属性的值。System.out.print(hello.getName()+hello.getSex()+hello.getAge()+hello.getScore());
你的变量没有用private修饰,不需要用get() , set()方法啊!你没有对面变量进行封装,完全可以直接操作啊!
小伙 没有输出方法。
System.out.println()都没用,当然显示不了
Java入门第二季
531293 学习 · 6327 问题
相似问题