本帖最后由 张龙跃 于 2013-4-30 03:53 编辑
定义一个学生类, 需要有姓名, 年龄, 考试成绩三个成员属性. 属性(成员变量)需要私有并提供get, set方法, 可以通过构造函数进行初始化.
class xuesheng
{
private String name;
private int age;
private double score;
//定义私有变量接收传入的参数
xuesheng(String name,int age,double score)
{
this.name=name;
this.age=age;
this.score=score;
}
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 double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
private void speak()
{
System.out.println(name+age+score);
}
}
class haha
{
public static void main(String[] args)
{
xuesheng.x = new xuesheng("zhangsan",22,98);
}
}
我把.改成空格后还是出错啊
|