class Demo_Student { //创建一个类
public static void main(String[] args) { //主方法区
Student a = new Student("赵六",18,89); //创建对象并赋值
a.Study(); //调用方法
}
}
class Student { //创建一个基本类,Student类
private String name; //私有化成员变量name名字,并初始化
private int age; //私有化成员变量age年龄,并初始化
private int score; //私有化成员变量score 成绩,并初始化
public Student(String name,int age,int score){ //创建一个有参的构造方法
this.name = name; //给成员变量赋值
this.age = age;
this.score = score;
}
public void setName(String name) { //设置name
this.name = name;
}
public String getName() { //获取name
return name;
}
public void setAge(int age) { //设置年龄age
this.age = age;
}
public int getAge() { //获取年龄age
return age;
}
public void setScore(int score){ //设置成绩score
this.score = score;
}
public int getScore() { //获取成绩
return score;
}
public void Study() { //创建方法
System.out.println("姓名:"+name+" "+"年龄:"+age+"岁"+" "+"成绩:"+score+"分"); //在控制台打印姓名,年龄,成绩.
}
}
|
|