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("说中文..."); } }编写以上代码的测试类
时间: 2024-02-14 11:10:28 浏览: 77
String类的一些常用方法的测试
以下是编写测试类 `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`。
希望对你有所帮助!
阅读全文