package 项目.面向对象homework;
public class HomeWork01 {
public static void main(String[] args) {
Person[] p1 = new Person[5];
p1[0] = new Person("小明",10,"建筑师");
p1[1] = new Person("校长",24,"厨师");
p1[2] = new Person("小红",12,"学生");
p1[3] = new Person("小花",56,"学生");
p1[4] = new Person("小会",33,"学生");
System.out.println("排序前");
for (int i = 0; i < p1.length; i++) {
System.out.println(p1[i].toString());
}
Person temp = null;
for (int i = 0; i < p1.length - 1; i++) {
for (int j = 0; j < p1.length - 1 - i; j++) {
if(p1[i].getAge() < p1[i+1].getAge()){
temp = p1[i];
p1[i] = p1[i+1];
p1[i+1] = temp;//应该交换对象,不能只交换对象的getAge
}
}
}
System.out.println("\n排序后");
for (int i = 0; i < p1.length; i++) {
System.out.println(p1[i].toString());
}
}
}
class Person{
private String name;
private int age;
private String job;
public Person(String name, int age, String job) {
this.name = name;
this.age = age;
this.job = job;
}
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 String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", job='" + job + '\'' +
'}';
}
}
package 项目.面向对象homework.HW03;
public class HomwWork03 {
public static void main(String[] args) {
Teacher teacher = new Teacher("小明老师",34,"普通教师",8000);
System.out.println(teacher.introduce());
Teacher t2 = new Professor("小张教授",56,"教授",10000,1.3);
System.out.println(((Professor)t2).introduce());
Teacher t3 = new NextProfesser("小红副教授",44,"副教授",9000,1.2);
System.out.println(((NextProfesser)t3).introduce());
Teacher t4 = new JiangShi("小讲师",40,"讲师",8500,1.1);
System.out.println(((JiangShi)t4).introduce());
}
}
class Teacher{
private String name;
private int age;
private String post;//职称
private double salary;
public Teacher(String name, int age, String post, double salary) {
this.name = name;
this.age = age;
this.post = post;
this.salary = salary;
}
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 String getPost() {
return post;
}
public void setPost(String post) {
this.post = post;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
@Override
public String toString() {
return "Teacher{" +
"name='" + name + '\'' +
", age=" + age +
", post='" + post + '\'' +
", salary=" + salary +
'}';
}
public String introduce(){
return this.toString();
}
}
package 项目.面向对象homework.HW03;
public class Professor extends Teacher{
private double salaryGrade;
public Professor(String name, int age, String post, double salary, double salaryGrade) {
super(name, age, post, salary);
this.salaryGrade = salaryGrade;
}
public double getSalaryGrade() {
return salaryGrade;
}
public void setSalaryGrade(double salaryGrade) {
this.salaryGrade = salaryGrade;
}
@Override
public String introduce() {
return super.introduce() + "工资级别:"+ salaryGrade;
}
}
package 项目.面向对象homework.HW03;
public class NextProfesser extends Teacher{
private double salaryGrade;
public NextProfesser(String name, int age, String post, double salary, double salaryGrade) {
super(name, age, post, salary);
this.salaryGrade = salaryGrade;
}
public double getSalaryGrade() {
return salaryGrade;
}
public void setSalaryGrade(double salaryGrade) {
this.salaryGrade = salaryGrade;
}
@Override
public String introduce() {
return super.introduce() + "工资级别:"+ salaryGrade;
}
}
package 项目.面向对象homework.HW03;
public class JiangShi extends Teacher{
private double salaryGrade;
public JiangShi(String name, int age, String post, double salary, double salaryGrade) {
super(name, age, post, salary);
this.salaryGrade = salaryGrade;
}
public double getSalaryGrade() {
return salaryGrade;
}
public void setSalaryGrade(double salaryGrade) {
this.salaryGrade = salaryGrade;
}
@Override
public String introduce() {
return super.introduce() + "工资级别:"+ salaryGrade;
}
}