/* 练习:
(1)声明一个员工类
-包含属性:编号、姓名、薪资、性别、要求属性私有化,并提供get/set
-提供无参构造器和有参构造器
-提供getInfo()
(2)在测试类的main中分别用无参构造和有参构造创建员工类对象,调用getInfo*/
声明类
public class Employee {
private int id;
private String name;
private double salary;
private char gender;
//提供get/set方法
public void setId(int i){
id=i;
}
public int getId(){
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public char getGender() {
return gender;
}
public void setGender(char gender) {
this.gender = gender;
}
//提供无参构造器和有参构造器
public Employee(int i, String n, double s, char g) {
id=i;
name=n;
salary=s;
gender=g;
}
public Employee(){};
public String getInfo(){
return name+id+salary+gender;
}
}
测试类
public class TestEmployee {
public static void main(String[] args) {
Employee employee = new Employee();
employee.setId(1);
System.out.println(employee.getId());
Employee employee1 = new Employee(2,"tt",300,'男');
System.out.println(employee1.getInfo());
}
}
根据内存图理解实例变量和类变量的不同
实例变量
?类变量
|