public class Student { private String name; private int age; 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; }}
时间: 2024-02-14 17:22:52 浏览: 161
这段代码定义了一个名为 Student 的类,该类有两个私有属性 name 和 age,分别表示学生的姓名和年龄。为了访问这两个属性,该类提供了两个 getter 方法 getName 和 getAge,以及两个 setter 方法 setName 和 setAge。其中,getter 方法用于获取属性值,setter 方法用于设置属性值。这种封装的方式可以保护属性的安全性,同时提供便捷的属性访问方式。
相关问题
public class Student { private String name; private int age; private int score; private static String className = "Class A"; // 无参构造方法 public Student() { } // 有参构造方法 public Student(String name, int age, int score) { this.name = name; this.age = age; this.score = score; } // set / get 方法 public void setName(String name) { this.name = name; } public String getName() { return name; } public void setAge(int age) { this.age = age; } public int getAge() { return age; } public void setScore(int score) { this.score = score; } public int getScore() { return score; } // 类方法 public static void speak() { System.out.println("说中文..."); } }编写以上代码的测试类
以下是编写测试类 `StudentTest` 的代码:
```java
public class StudentTest {
public static void main(String[] args) {
Student student1 = new Student();
student1.setName("Tom");
student1.setAge(18);
student1.setScore(90);
System.out.println("name: " + student1.getName());
System.out.println("age: " + student1.getAge());
System.out.println("score: " + student1.getScore());
System.out.println("class name: " + Student.className);
Student.speak();
System.out.println();
Student student2 = new Student("Jerry", 17, 95);
System.out.println("name: " + student2.getName());
System.out.println("age: " + student2.getAge());
System.out.println("score: " + student2.getScore());
System.out.println("class name: " + Student.className);
Student.speak();
}
}
```
在 `StudentTest` 类中,我们创建了两个 `Student` 类的实例,一个是通过无参构造方法创建的,另一个是通过有参构造方法创建的。然后,我们对每个实例的成员变量进行了赋值,并使用 `get` 方法获取了每个成员变量的值。同时,我们也输出了类变量 `className` 的值,并调用了类方法 `speak`。
希望对你有所帮助!
public class Test{ public static void main(String [] args){ Student s = new Student("zhang", 23, 74); s.display(); s.setName("lisi"); s.display(); } } class Student{ private String name; private int age; private double grade; public Student(String name, int age, double grade){ this.name = name; this.age = age; this.grade = grade; } public String getName(){ } public void setName(String name){ } public void display(){ System.out.println("Name=" + name); } }补全代码
public class Test {
public static void main(String[] args) {
Student s = new Student("zhang", 23, 74);
s.display();
s.setName("lisi");
s.display();
}
}
class Student {
private String name;
private int age;
private double grade;
public Student(String name, int age, double grade) {
this.name = name;
this.age = age;
this.grade = grade;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void display() {
System.out.println("Name=" + name + ", Age=" + age + ", Grade=" + grade);
}
}
阅读全文