来瞧瞧我写的,够细的吧!
- public class Test8
- {
- public static void main(String[] args)
- {
- //通过 new Student("张三", 22, 95) 的方式创建对象。
- Student s = new Student("张三",22,95);
-
- //set 设置各个属性信息。
- s.setName("name张三");
- s.setAge(2);
- s.setScore(9);
- //使用 get 获取属性信息。
- System.out.println(s.getName());
- System.out.println(s.getAge());
- //设置并获取全部的学生信息。
- s.setStudent("张三1", 21, 91);
- System.out.println(s.getStudent());
- }
- }
- //建立学生类
- class Student
- {
- private String name;
- private int age;
- private int score;
- //构造方法
- public Student(String name,int age,int score)
- {
- this.name = name;
- this.age = age;
- this.score = score;
- }
- //set 各个成员变量。
- public void setName(String name)
- {
- this.name = name;
- }
- public void setAge(int age)
- {
- this.age = age;
- }
- public void setScore(int score)
- {
- this.score = score;
- }
- public void setStudent(String name,int age,int score)
- {
- this.name = name;
- this.age = age;
- this.score = score;
- }
-
- //get 各个成员变量。
- public String getName()
- {
- return name;
- }
- public int getAge()
- {
- return age;
- }
- public int getScore()
- {
- return score;
- }
- public String getStudent()
- {
- return name+","+age+","+score;
- }
- }
复制代码 |