java定义一个Person类, 类中属性name(String),age(int),sex(boolean),要求属性私有,提供set/get 方法print(),打印Perosn类的各属性

package test;

public class TestPerson {
	public static void main(String[] args) {
		Person[] ps = {
						new Student("zzz",20,true,95.0),
						new Student("abc",19,false,96.0),
						new Worker("xyz",25,true,5000.0),
						new Worker("xxx",26,false,4000.0),
						new Teacher("cpx",20,true,"Java"),				
						new Teacher("zs",25,false,"UI")
					  };
		System.out.println("工人平均工资为:"+getWorkerSalary(ps));
		System.out.println("教Java的老师如下:");
		Teacher[] ts = getTeachers(ps);
		for (int i = 0; i < ts.length; i++) {
			ts[i].print();;
		}
		Student[] ss = getStudents(ps);
		for (int i = 0; i < ss.length; i++) {
			ss[i].print();
		}
	}
	public static double getWorkerSalary(Person[] ps) {
		double sum = 0;
		int count = 0;
		for(int i=0;i<ps.length;i++) {
			if(ps[i] instanceof Worker) {
				Worker w = (Worker)ps[i];
				sum+=w.getSalary();
				count++;
			}
		}
		return sum/count;
	}
	public static Teacher[] getTeachers(Person[] ps) {
		int count = 0;
		for(int i=0;i<ps.length;i++) {
			if(ps[i] instanceof Teacher) {
				Teacher t = (Teacher)ps[i];
				if(t.getCourse()=="Java") {
					count++;
				}
			}
		}
		Teacher[] ss = new Teacher[count];
		int index = 0;
		for(int i=0;i<ps.length;i++) {
			if(ps[i] instanceof Teacher) {
				Teacher t = (Teacher)ps[i];
				if(t.getCourse()=="Java") {
					ss[index]=t;
					index++;
				}
			}
		}
		return ss;
	}
	public static Student[] getStudents(Person[] ps) {
		int count = 0;
		for(int i=0;i<ps.length;i++) {
			if(ps[i] instanceof Student) {
				Student s = (Student)ps[i];
				if(s.getScore()>90.0 && s.getAge()<20) {
					count++;
				}
			}
		}
		Student[] ss = new Student[count];
		int index = 0;
		for(int i=0;i<ps.length;i++) {
			if(ps[i] instanceof Student) {
				Student s = (Student)ps[i];
				if(s.getScore()>90.0 && s.getAge()<20) {
					ss[index]=s;
					index++;
				}
			}
		}
		System.out.println("三好学生如下:");
		return ss;
	}
}
class Person{
	private String name;
	private int age;
	private boolean sex;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public boolean isSex() {
		return sex;
	}
	public void setSex(boolean sex) {
		this.sex = sex;
	}
	public Person(String name, int age, boolean sex) {
		super();
		this.name = name;
		this.age = age;
		this.sex = sex;
	}
	public Person() {
		super();
	}
	public void print() {
		System.out.print(name+"\t"+age+"\t"+sex+"\t");
	}
}
class Student extends Person{
	private double score;

	public double getScore() {
		return score;
	}

	public void setScore(double score) {
		this.score = score;
	}

	public Student() {
		super();
	}

	public Student(String name, int age, boolean sex,double score) {
		super(name, age, sex);
		this.score = score;
	}
	public void print() {
		super.print();
		System.out.println(score);
	}
}
class Worker extends Person{
	private double salary;

	public double getSalary() {
		return salary;
	}

	public void setSalary(double salary) {
		this.salary = salary;
	}

	public Worker() {
		super();
	}

	public Worker(String name, int age, boolean sex,double salary) {
		super(name, age, sex);
		this.salary = salary;
	}
}
class Teacher extends Person{
	private String course;

	public String getCourse() {
		return course;
	}

	public void setCourse(String course) {
		this.course = course;
	}

	public Teacher() {
		super();
	}

	public Teacher(String name, int age, boolean sex,String course) {
		super(name, age, sex);
		this.course = course;
	}
	public void print() {
		super.print();
		System.out.println(course);
	}
}

你可能感兴趣的:(笔记)