343-作业01
package chapter08.homeworks;
public class Homework01 {
public static void main(String[] args) {
/*1.定义一个Person类{name, age, job},初始化Person对象数组,有3个person对象,
并按照age从大到小进行排序,提示,使用冒泡排序 Homework01.java
*/
Person[] persons = new Person[3];
persons[0]=new Person("a",10,"JAVA工程师");
persons[1]=new Person("b",50,"算法工程师");
persons[2]=new Person("c",35,"PHP工程师");
for (int i = 0; i < persons.length; i++) {
System.out.println(persons[i]);
}
Person tmp=null;
for (int i = 0; i < persons.length-1; i++) {
for (int j = 0; j < persons.length-1-i; j++) {
if(persons[j].getAge()<persons[j+1].getAge()){
tmp=persons[j];
persons[j]=persons[j+1];
persons[j+1]=tmp;
}
}
}
System.out.println("输出排序后,从大到小");
for (int i = 0; i < persons.length; i++) {
System.out.println(persons[i]);
}
}
}
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 + '\'' +
'}';
}
}
344-作业02
345-作业03
package chapter08.homeworks;
public class HomeWork03 {
public static void main(String[] args) {
Professor LDY = new Professor("林黛玉", 20, "教授", 15000, 1.1);
LDY.introduce();
}
}
class Teacher{
private String name;
private int age;
private String post;
private double salary;
private double grade;
public Teacher(String name, int age, String post, double salary, double grade) {
this.name = name;
this.age = age;
this.post = post;
this.salary = salary;
this.grade = grade;
}
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;
}
public double getGrade() {
return grade;
}
public void setGrade(double grade) {
this.grade = grade;
}
public void introduce() {
System.out.println(
"name='" + name +
", age=" + age +
", post='" + post +
", salary=" + salary +
", grade=" + grade
);
}
}
package chapter08.homeworks;
public class Professor extends Teacher{
public Professor(String name, int age, String post, double salary, double grade) {
super(name, age, post, salary, grade);
}
@Override
public void introduce() {
System.out.println("这是教师的");
super.introduce();
}
}
346-作业04
package chapter08.homeworks;
public class Manager extends Employee{
private double bonus;
public Manager(String name, double daySal, int workDays, double grade) {
super(name, daySal, workDays, grade);
}
public double getBonus() {
return bonus;
}
public void setBonus(double bonus) {
this.bonus = bonus;
}
@Override
public void printSal() {
System.out.println("经理是"+getName()+" 工资是"+(getDaySal()*getWorkDays()*getGrade()+bonus));
}
}
package chapter08.homeworks;
public class Manager extends Employee{
private double bonus;
public Manager(String name, double daySal, int workDays, double grade) {
super(name, daySal, workDays, grade);
}
public double getBonus() {
return bonus;
}
public void setBonus(double bonus) {
this.bonus = bonus;
}
@Override
public void printSal() {
System.out.println("经理是"+getName()+" 工资是"+(getDaySal()*getWorkDays()*getGrade()+bonus));
}
}
347-作业05
348-作业06
super只可以继承父类的公共属性和方法
this可以访问本类的所有属性和方法,父类的公共属性和方法,本类有的使用就近原则,不去找父类的了
349-作业07
new Demo().test();
编译阶段,new,无参构造,super()输出Test,再输出Demo,
运行阶段,.test(),Rose,Jack
new Demo().test();
编译阶段,设置父类名称,无输出
运行阶段,john,Jack
350-作业08
package chapter08.homeworks;
public class SavingAccount extends BankAccount{
private int count=3;
private double rate=0.01;
public void earnMonthlyInterest(){
count=3;
super.deposit(getBalance()*rate);
}
@Override
public void deposit(double amount) {
if (count>0){
super.deposit(amount);
count-=1;
}else {
super.deposit(amount-1);
}
}
@Override
public void withdraw(double amount) {
if(count>0){
super.withdraw(amount);
count-=1;
}else {
super.withdraw(amount+1);
}
}
public SavingAccount(double initialBalance) {
super(initialBalance);
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public double getRate() {
return rate;
}
public void setRate(double rate) {
this.rate = rate;
}
}
351-作业09
352-作业10
353-作业11
向上转型,
编译类型是Person,所以只可以用Person的两个方法
运行类型是Student,动态绑定,找子类Student的方法,所以run调的是子类的方法
总结:向上转型,可以调用父类的所有方法,如果父类中有方法和子类重复,用的是子类的,子类中特有的方法不可以调用
向下转型,p编译是person,指向的是Student,现在把编译也转成Student
编译类型是student,可以调用子类的所有方法,继承父类的方法
运行类型是Student,先运行子类中的方法,如果没有就找父类的
354-作业12
355-作业13
作业13有待完善 明日再战,明日早起!