diff --git a/w2 b/w2 new file mode 100644 index 0000000..f238593 --- /dev/null +++ b/w2 @@ -0,0 +1,31 @@ +// 定义Student类 +class Student { + // 定义属性 + int studentId; + String name; + double score; + + // 定义study方法 + void study() { + System.out.println("学生" + name + "(学号:" + studentId + ")正在学习,当前成绩为:" + score); + } +} + +// 主类 +public class StudentTest { + public static void main(String[] args) { + // 实例化第一个Student对象 + Student student1 = new Student(); + student1.studentId = 101; + student1.name = "张三"; + student1.score = 92.5; + student1.study(); // 调用study方法 + + // 实例化第二个Student对象 + Student student2 = new Student(); + student2.studentId = 102; + student2.name = "李四"; + student2.score = 88.0; + student2.study(); // 调用study方法 + } +} \ No newline at end of file