java实验:定义一个People类,为People类派生出一个子类Employee类

//4、定义一个People类,它包含属性有姓名name和性别sex,
//为People类派生出一个子类Employee类,为其增加两个属性年龄age和工资等级grade,工资等级分A、B、C三个等级,
//在子类中增加用于打印出员工的姓名、性别、年龄和工资等级的方法print,编写一个应用程序,测试定义的类。
public class Csj12 {
     
	String name;
	String sex;

	public String getName() {
     
		return name;
	}

	public void setName(String name) {
     
		this.name = name;
	}

	public String getSex() {
     
		return sex;
	}

	public void setSex(String sex) {
     
		this.sex = sex;
	}

	public Csj12() {
     
	}

	public Csj12(String name, String sex) {
     
		super();
		this.name = name;
		this.sex = sex;
	}

	public String toString() {
     
		return "姓名:" + name + "  性别:" + sex;
	}
}

public class Employee extends Csj12 {
     
	int age;
	String grade;

	// 对于年龄的设置的要求
	public void setAge(int age) {
     
		if (age > 0 && age < 100)
			this.age = age;
		else
			this.age = 0;
	}

	public Employee() {
     
	}

	public Employee(String name, String sex, int age, int salary) {
     // 重载
		super(name, sex);// 调用父类构造方法
		this.setAge(age);
		this.pdgrade(salary);
	}

	public String pdgrade(int salary) {
     
		if (salary >= 10000) {
     
			return grade = "A";
		} else if (5000 <= salary) {
     
			return grade = "B";
		} else {
     
			return grade = "C";
		}
	}

	public String toString() {
     // 重写
		return super.toString() + "  年龄:" + age + "  工资等级:" + grade;
	}

	public void print() {
     
		System.out.println(this.toString());// 执行子类的tostring()
	}

}

class Employee_test {
     
	public static void main(String[] args) {
     
		Employee e1 = new Employee("陈陈陈", "女", 18, 18000);
		e1.print();
		Employee e2 = new Employee("程程程", "女", 22, 6000);
		e2.print();
		Employee e3 = new Employee("丁丁丁", "男", 120, 2000);
		e3.print();
	}
}

你可能感兴趣的:(java,java)