定义一个person类{name job age},初始化person对象组,有3个person对象,并按照age 从大到小进行排序(冒泡排序)
public class Main {
public static void main(String[] args) {
Person person1 = new Person("张三",18,"工人");
Person person2 = new Person("李四",15,"老板");
Person person3 = new Person("王五",30,"经理");
Person[] perosns= {person1,person2,person3};
//冒泡排序开始 外层比较n-1次
Person personTemp = null; // 只需要一个空指针就行了
for (int i = 0; i < perosns.length-1; i++) {
//注意是从大到小排序
for (int j = 0; j <perosns.length-1-i ; j++) { //暂时先这样写着,后面再思考
if(perosns[j].getAge()<perosns[j+1].getAge()){
//交换 小的数据往后面移动
personTemp=perosns[j];
perosns[j]=perosns[j+1];
perosns[j+1]=personTemp;
}
}
}
System.out.println(perosns[0].getAge()+" "+perosns[1].getAge()+" "+perosns[2].getAge());
}
}
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 int getAge() {
return age;
}
public String getJob() {
return job;
}
}
写出四种访问修饰符已经他们的权限(公->受-默->私)
// 感觉这个题其实出的不太好呀
public class Main {
public static void main(String[] args) {
Teacher teacher = new Professor("韩顺平", 18, "教授");
teacher.introduce();
}
}
public class Teacher {
private String name;
private int age;
private String post;
public Teacher(String name, int age, String post) {
this.name = name;
this.age = age;
this.post = post;
}
void introduce(){
System.out.println("名字:"+name+" 年龄:"+age+" 职称:"+post);
}
}
public class Professor extends Teacher {
private double salary=1.3;
public Professor(String name, int age, String post) {
super(name, age, post);
}
public void introduce(){
super.introduce();
System.out.print(" 薪资"+this.salary);
}
}