菜鸟学习java,求大神解释下面的代码各段是什么意思,尽量详细,谢谢

2024-12-28 19:21:10
推荐回答(2个)
回答1:

implements Serializable可序列化,可以把这个类压缩存储起来
Employee 意思是雇员
String name; int age; int salary;字符串姓名,整数年龄,整数工资,是雇员的属性
public Employee(String name, int age, int salary) {
this.name = name;
this.age = age;
this.salary = salary;
}是构造函数
public void print() {
System.out.println("Record for: "+name);
System.out.println("Name: "+name);
System.out.println("Age: "+age);
System.out.println("Salary: "+salary);
}是输出雇员信息

回答2:

//导入io包
import java.io.*;

//类定义,继承于接口Serializabel(可序列化接口)
public class Employee implements Serializable {
// 成员变量name
String name;
// 成员变量age
int age;
// 成员变量salary
int salary;

// 构造函数,初始化3个成员变量
public Employee(String name, int age, int salary) {
this.name = name;
this.age = age;
this.salary = salary;
}

// 打印方法,按格式输出3个成员变量
public void print() {
System.out.println("Record for: " + name);
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Salary: " + salary);
}
}