2 changed files with 51 additions and 42 deletions
@ -1,44 +1,9 @@ |
|||||
// Student.java |
# 温度转换程序(Java版) |
||||
public class Student { |
## 功能 |
||||
// 成员属性 |
实现摄氏温度与华氏温度的相互转换,输入格式:温度值 + 空格 + 单位(C/F),例如 `36.6 C`。 |
||||
private int studentId; |
|
||||
private String name; |
|
||||
private double score; |
|
||||
|
|
||||
// 构造方法(用于创建对象时给属性赋值) |
## 编译与运行命令 |
||||
public Student(int studentId, String name, double score) { |
1. 编译:`javac TemperatureConverter.java` |
||||
this.studentId = studentId; |
2. 运行:`java TemperatureConverter` |
||||
this.name = name; |
|
||||
this.score = score; |
|
||||
} |
|
||||
|
|
||||
// study() 方法 |
## 示例输出 |
||||
public void study() { |
|
||||
System.out.println(name + "(学号:" + studentId + ")正在学习,当前成绩:" + score); |
|
||||
} |
|
||||
|
|
||||
// 可选:getter/setter 方法(用于访问/修改私有属性) |
|
||||
public int getStudentId() { |
|
||||
return studentId; |
|
||||
} |
|
||||
|
|
||||
public String getName() { |
|
||||
return name; |
|
||||
} |
|
||||
|
|
||||
public double getScore() { |
|
||||
return score; |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
public class Main { |
|
||||
public static void main(String[] args) { |
|
||||
// 实例化两个 Student 对象 |
|
||||
Student stu1 = new Student(2026001, "张三", 90.5); |
|
||||
Student stu2 = new Student(2026002, "李四", 88.0); |
|
||||
|
|
||||
// 调用 study() 方法 |
|
||||
stu1.study(); |
|
||||
stu2.study(); |
|
||||
} |
|
||||
} |
|
||||
@ -0,0 +1,44 @@ |
|||||
|
// Student.java |
||||
|
public class Student { |
||||
|
// 成员属性 |
||||
|
private int studentId; |
||||
|
private String name; |
||||
|
private double score; |
||||
|
|
||||
|
// 构造方法(用于创建对象时给属性赋值) |
||||
|
public Student(int studentId, String name, double score) { |
||||
|
this.studentId = studentId; |
||||
|
this.name = name; |
||||
|
this.score = score; |
||||
|
} |
||||
|
|
||||
|
// study() 方法 |
||||
|
public void study() { |
||||
|
System.out.println(name + "(学号:" + studentId + ")正在学习,当前成绩:" + score); |
||||
|
} |
||||
|
|
||||
|
// 可选:getter/setter 方法(用于访问/修改私有属性) |
||||
|
public int getStudentId() { |
||||
|
return studentId; |
||||
|
} |
||||
|
|
||||
|
public String getName() { |
||||
|
return name; |
||||
|
} |
||||
|
|
||||
|
public double getScore() { |
||||
|
return score; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public class Main { |
||||
|
public static void main(String[] args) { |
||||
|
// 实例化两个 Student 对象 |
||||
|
Student stu1 = new Student(2026001, "张三", 90.5); |
||||
|
Student stu2 = new Student(2026002, "李四", 88.0); |
||||
|
|
||||
|
// 调用 study() 方法 |
||||
|
stu1.study(); |
||||
|
stu2.study(); |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue