通过new Student("张三", 22, 95)知道姓名、年龄、分数是通过构造函数进行传递的。
所以:
- public class Test01 {
- public static void main(String[] args)
- {
- Student stu = new Student("张三", 22, 95);
- stu.setAge(25);
- stu.show();
- }
- }
- class Student {
- private String name;
- private int age;
- private int score;
-
- Student(String name,int age,int 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 int getScore() {
- return score;
- }
- public void setScore(int score) {
- this.score = score;
- }
- public void show(){
- System.out.println("Name: " + name + ", age: "+ age + ", score:" + score);
- }
- }
复制代码
|