CoreJava/exp2/StudentTest.java
2024-05-09 09:10:51 +08:00

60 lines
1.1 KiB
Java

package exp2;
//学生类
class Student {
private String name;
private String age;
//无参构造
public Student() {
this.name = "张三";
this.age = "38";
}
//带参构造
public Student(String name, String age) {
this.name = name;
this.age = age;
}
void show(){
System.out.println(this.name);
System.out.println(this.age);
}
//设置姓名
static void setName(Student s, String name) {
s.name = name;
}
//设置年龄
static void setAge(Student s, String age) {
s.age = age;
}
//进行学习行为
static void study(Student s) {
System.out.println(s.name+"学习");
}
//进行考试行为
static void exam(Student s) {
System.out.println(s.name+"考试");
}
}
//测试类
public class StudentTest {
public static void main(String[] args) {
//创建学生s1
Student s1 = new Student();
Student s2 = new Student("李四", "36");
Student.study(s1);
Student.exam(s2);
}
}