建立学生类和测试类。学生类中有成员变量:姓名,年龄;成员方法:学习,考试。测试类中实现创建学生类对象并赋值调用方法。java完整程序
时间: 2024-09-30 17:12:27 浏览: 52
在Java中,我们可以创建一个名为`Student`的类作为学生的模型,包含姓名和年龄属性,以及学习和考试的方法。然后创建一个`TestClass`类来进行测试操作。下面是一个简单的例子:
```java
// 学生类 (Student.java)
public class Student {
private String name;
private int age;
// 构造函数
public Student(String name, int age) {
this.name = name;
this.age = age;
}
// 成员方法 - 学习
public void study() {
System.out.println(name + " is studying.");
}
// 成员方法 - 考试
public void exam() {
System.out.println(name + " is taking an exam.");
}
}
// 测试类 (TestClass.java)
public class TestClass {
public static void main(String[] args) {
// 创建学生对象
Student student1 = new Student("张三", 18);
// 调用学习方法
student1.study();
// 调用考试方法
student1.exam();
}
}
```
在这个例子中,`TestClass`的`main`方法首先创建了一个`Student`对象,并设置了姓名和年龄。接着通过这个对象分别调用了`study`和`exam`方法。
阅读全文
相关推荐
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)