面向对象编程练习题

package oop_Homework;public class Homework01 {public static void main(String[] args) {Person[] persons = new Person[3];//初始化Person对象数组,有三个person对象persons[0] = new Person("smith",10,"java工程师");persons[1] = new Person("tom",50,"大数据工程师");persons[2] = new Person("mary",30,"PHP工程师");//输出对象数组for (int i = 0; i < persons.length; i++) {System.out.println(persons[i]);//默认对象的。tostring(),方法已经重写 tostring 已经重写输出对象的属性}System.out.println("========冒泡排序后========");Person tmp = null;//临时变量,用于交换数据for (int i = 0; i < persons.length - 1; i++) {for (int j = 0; j < persons.length - i; j++) {//按照 age 从大到小 进行排序,如果 前面人的 age 小于后面人的年龄 就交换if (persons[i].getName().length() > persons[i + 1].getName().length()){tmp = persons[i + 1];persons[i + 1] = persons[i];persons[i] = tmp;}}}for (int i = 0; i < persons.length; i++) {System.out.println(persons[i]);//默认对象的。tostring(),方法已经重写 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;}@Overridepublic String toString() {return "Person{" +"name='" + name + '\'' +", age=" + age +", job='" + job + '\'' +'}';}
}
同类 同包 不同包子类 不同包非子类
public 可以 可以 可以 可以
protect 可以 可以 可以 不可以
默认 可以 可以 不可以 不可以
private 可以 不可以 不可以 不可以
public公共 同包 同类 子类 不同包 都可使用
private受保护 同包 同类 子类 可以使用 不同包不可使用
默认修饰符 同包 同类 可以使用 子类不可使用 不同包不可使用
private私有 同类可以使用 同包 同类 不同包 不可使用

package oop_Homework.Homework03;public class Homework03 {public static void main(String[] args) {Professor professor = new Professor("老高",50,"高级职称",12000,1.3);a_Professor aProfessor = new a_Professor("老中", 49, "中级职称", 11000, 1.2);Lectuer lectuer = new Lectuer("老讲", 48, "初级职称", 10000, 1.1);professor.introduce();aProfessor.introduce();lectuer.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 oop_Homework.Homework04;public class Emlpoyee {private String name;private double salary;private double days;private double grade;public Emlpoyee(String name, double salary, double days, double grade) {this.name = name;this.salary = salary;this.days = days;this.grade = grade;}public String getName() {return name;}public void setName(String name) {this.name = name;}public double getSalary() {return salary;}public void setSalary(double salary) {this.salary = salary;}public double getDays() {return days;}public void setDays(double days) {this.days = days;}public double getGrade() {return grade;}public void setGrade(double grade) {this.grade = grade;}public void sumsal(){System.out.println(name + " 工资=" + salary*days*grade);}
}
package oop_Homework.Homework04;public class worker extends Emlpoyee{public worker(String name, double salary, double days, double grade) {super(name, salary, days, grade);}@Overridepublic void sumsal() {super.sumsal();}
}
package oop_Homework.Homework04;public class manager extends Emlpoyee{private double bonus;public manager(String name, double salary, double days, double grade, double bonus) {super(name, salary, days, grade);this.bonus = bonus;}public double getBonus() {return bonus;}public void setBonus(double bonus) {this.bonus = bonus;}@Overridepublic void sumsal() {System.out.println("经理" + getName() + "工资是=" + (bonus + (getSalary()*getDays()*getGrade())));}
}
package oop_Homework.Homework04;public class Homework04 {public static void main(String[] args) {worker worker01 = new worker("老张", 200, 26, 1);manager manager01 = new manager("老李", 300,26, 1.2,1000);worker01.sumsal();manager01.sumsal();}
}

package Homework05;public class Employee {private String name;private String job;private double sal;public Employee(String name, String job, double sal) {this.name = name;this.job = job;this.sal = sal;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getJob() {return job;}public void setJob(String job) {this.job = job;}public double getSal() {return sal;}public void setSal(double sal) {this.sal = sal;}public void yearsal(){System.out.println("全年的工资是:" + (12*getSal()));}
}
package Homework05;public class Worker extends Employee{public Worker(String name, String job, double sal) {super(name, job, sal);}@Overridepublic void yearsal() {System.out.println(getName() + getJob());super.yearsal();}
}
package Homework05;public class Teacher extends Employee{private double classSal;private double classDay;public Teacher(String name, String job, double sal, double classSal, double classDay) {super(name, job, sal);this.classSal = classSal;this.classDay = classDay;}public double getClassSal() {return classSal;}public void setClassSal(double classSal) {this.classSal = classSal;}public double getClassDay() {return classDay;}public void setClassDay(double classDay) {this.classDay = classDay;}@Overridepublic void yearsal() {System.out.println(getName() + getJob() + "全年的工资是:" + (classDay*classSal + 12 * getSal()) );}
}
package Homework05;public class Scientist extends Employee{private double bonus;public Scientist(String name, String job, double sal, double bonus) {super(name, job, sal);this.bonus = bonus;}public double getBonus() {return bonus;}public void setBonus(double bonus) {this.bonus = bonus;}@Overridepublic void yearsal() {System.out.println(getName() + getJob() + "全年工资是:" + (getBonus() + 12 * getSal()));}
}
package Homework05;import com.sun.deploy.util.Waiter;public class Homework05 {public static void main(String[] args) {Peasant peasant = new Peasant("johe", "农民", 4500);peasant.yearsal();Worker worker = new Worker("sam", "工人", 4000);worker.yearsal();Teacher teacher = new Teacher("chuanpu", "教师", 4000, 25, 100);teacher.yearsal();Scientist scientist = new Scientist("tagna", "科学家", 5000, 10000);scientist.yearsal();}
}

super可以调用父类的属性和方法,但是得有访问权限
this指从本类开始查找,本来没有开始查找父类的


package Homework07;public class Test {String name = "rose";Test(){System.out.println("Test");}Test(String name){this.name = name;}
}
class Demo extends Test{String name = "Jack";Demo(){super();System.out.println("Demo");}Demo(String s){super(s);}public void test(){System.out.println(super.name);System.out.println(this.name);}public static void main(String[] args) {new Demo().test();new Demo("john").test();}
}
Test
Demo
rose
Jack
john
Jack

package Homework08;public class BankAccount {private double balance;public BankAccount(double initialBalance){this.balance = initialBalance;}public void deposit(double amount){balance += amount;}public void withdraw(double amount){balance -= amount;}public double getBalance() {return balance;}public void setBalance(int balance) {this.balance = balance;}
}
package Homework08;public class CheckingAccount extends BankAccount{public CheckingAccount(double initialBalance) {super(initialBalance);}@Overridepublic void deposit(double amount) {super.deposit(amount - 1);}@Overridepublic void withdraw(double amount) {super.withdraw(amount + 1);}
}
package Homework08;public class SavingAccount extends BankAccount{private int count = 3;private double rate = 0.01;public void earnMonthlyInterest(){//每个月初,我们将统计上个月的利率,同时将count = 3count = 3;super.deposit(getBalance() * rate);}@Overridepublic void deposit(double amount) {//判断是否还可以免手续费if (count > 0){super.deposit(amount);}else {super.deposit(amount - 1);//1 块转入银行}count--;//减去一次}@Overridepublic void withdraw(double amount) {if (count > 0){super.withdraw(amount);}else {super.deposit(amount + 1);}count --;}public SavingAccount(double initialBalance) {super(initialBalance);this.count = count;}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;}
}
package Homework08;public class Homework08 {public static void main(String[] args) {
// CheckingAccount checkingAccount = new CheckingAccount(1000);
// checkingAccount.deposit(10);
// checkingAccount.withdraw(9);
// System.out.println(checkingAccount.getBalance());SavingAccount savingAccount = new SavingAccount(1000.0);savingAccount.deposit(100);savingAccount.deposit(100);savingAccount.deposit(100);System.out.println(savingAccount.getBalance());savingAccount.deposit(100);System.out.println(savingAccount.getBalance());savingAccount.earnMonthlyInterest();System.out.println(savingAccount.getBalance());savingAccount.withdraw(100);savingAccount.withdraw(100);savingAccount.withdraw(100);System.out.println(savingAccount.getBalance());savingAccount.deposit(100);System.out.println(savingAccount.getBalance());}
}

package Homework09;public class Point {private double x;private double y;public Point(double x, double y) {this.x = x;this.y = y;}public double getX() {return x;}public void setX(double x) {this.x = x;}public double getY() {return y;}public void setY(double y) {this.y = y;}
}
package Homework09;public class LabeledPoint extends Point{private String label;public LabeledPoint(double x, double y, String label) {super(x, y);this.label = label;}
}
package Homework09;public class Homework09 {public static void main(String[] args) {LabeledPoint blackpeace = new LabeledPoint(128, 213.9, "blackpeace");}
}

package Homework10;public class Doctor {private String name;private int age;private String job;private char gender;private double sal;public Doctor(String name, int age, String job, char gender, double sal) {this.name = name;this.age = age;this.job = job;this.gender = gender;this.sal = sal;}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;}public char getGender() {return gender;}public void setGender(char gender) {this.gender = gender;}public double getSal() {return sal;}public void setSal(double sal) {this.sal = sal;}public boolean equals(Object obj){if (this == obj){return true;}if (!(obj instanceof Doctor)){return false;}//向下转型,因为obj的运行类型是Doctor或者是其子类型Doctor doctor = (Doctor)obj;return this.name.equals(doctor.name) && this.age == doctor.age && this.gender == doctor.gender && this.job.equals(doctor.job) && this.sal == doctor.sal;}
}
package Homework10;public class Homework10 {public static void main(String[] args) {Doctor doctor1 = new Doctor("jack", 20, "牙科医生", '男', 20000);Doctor doctor2 = new Doctor("jack", 20, "牙科医生", '男', 20000);System.out.println(doctor1.equals(doctor2));}
}

package Homework11;public class Person {public void run(){System.out.println("person run");}public void eat(){System.out.println("person eat");}
}
package Homework11;public class Student extends Person{@Overridepublic void run() {System.out.println("Student run");}public void study(){System.out.println("Student study...");}
}
/*答案:
* 向上转型:父类的引用指向子类对象
* Person p = new Student();
* p.run(); //student run;
* p.eat(); //person eat;
*
* 向下转型:把指向子类对象的父类引用,转成指向子类对象的子类引用
* Student s = (Student)p;
* s.run();//student run;
* s.study();//student study...
* s.eat();//person eat
* */


package Homework12;public class Person {private String name;private char sex;private int age;public Person(String name, char sex, int age) {this.name = name;this.sex = sex;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public char getSex() {return sex;}public void setSex(char sex) {this.sex = sex;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}//编写一个 play 方法,把共有的输出内容写道父类public String play(){return name + "爱玩";}//返回一个基本信息/** 姓名:张飞* 年龄:30* 性别:男* */public String basicInfo(){return "姓名: " + name + "\n年龄: " + age + "性别: " + sex;}@Overridepublic String toString() {return "Person{" +"name='" + name + '\'' +", sex=" + sex +", age=" + age +'}';}
}
package Homework12;public class Student extends Person{private String stu_id;public Student(String name, char sex, int age, String stu_id) {super(name,sex,age);this.stu_id = stu_id;}public String getStu_id() {return stu_id;}public void setStu_id(String stu_id) {this.stu_id = stu_id;}//我承诺会好好学习 javapublic void study(){System.out.println(getName() + "我承诺会好好学习 java");}//学生爱玩@Overridepublic String play() {return super.play() + "足球";}public void printInfo() {System.out.println("学生的信息:");System.out.println(basicInfo());System.out.println("学号: " + stu_id);study();System.out.println(play());}@Overridepublic String toString() {return "Student{" +"stu_id='" + stu_id + '\'' +'}' + super.toString();}
}
package Homework12;public class Teacher extends Person{private int work_age;public Teacher(String name, char sex, int age, int work_age) {super(name,sex,age);this.work_age = work_age;}public void setWork_age(int work_age) {this.work_age = work_age;}//承诺会好好教书public void teach(){System.out.println(getName() + "我承诺会好好教书");}//老师爱玩@Overridepublic String play() {return super.play() + "象棋";}public void printInfo(){System.out.println("老师的信息: ");System.out.println(super.basicInfo());System.out.println("工龄: " + work_age);teach();System.out.println(play());}@Overridepublic String toString() {return "Teacher{" +"work_age=" + work_age +'}' + super.toString();}
}
package Homework12;public class Homework12 {public static void main(String[] args) {Student student = new Student("小明", '男', 15, "00023102");student.printInfo();Teacher teacher = new Teacher("小王", '男', 34, 7);System.out.println("------------------------------------------");teacher.printInfo();//定义多态数组,里面保存2个学生和2个老师,要求按年龄从高到低排序Person[] persons = new Person[4];persons[0] = new Student("jack", '男', 10, "10086");persons[1] = new Student("mary", '男', 10, "10011");persons[2] = new Teacher("jeason", '男', 36, 10);persons[3] = new Teacher("scott", '男', 56, 29);//创建对象Homework12 homework12 = new Homework12();homework12.bubbleSort(persons);//输出排序后的数组System.out.println("-----排序后的数组-----");for (int i = 0; i < persons.length; i++) {System.out.println(persons[i]);}System.out.println("====================");for (int i = 0; i < persons.length; i++) {homework12.test(persons[i]);}}public void test(Person p){if (p instanceof Student){((Student)p).study();} else if (p instanceof Teacher) {((Teacher)p).teach();}else {System.out.println("do nothing......");}}public void bubbleSort(Person[] persons){Person temp = null;for (int i = 0; i < persons.length; i++) {for (int j = 0; j < persons.length - 1 - i; j++) {if (persons[j].getAge() < persons[j + 1].getAge()){temp = persons[j];persons[j] = persons[j + 1];persons[j + 1] = temp;}}}}
}
package Homework14;public class Homework14 {public static void main(String[] args) {C c = new C();}
}
class A{public A(){System.out.println("我是A类");}
}
class B extends A{public B(){System.out.println("我是B类的无参构造");}public B(String name){System.out.println(name + "我是B类的有参构造");}
}
class C extends B{public C(){this("Hello");System.out.println("我是c类的无参构造");}public C(String name){super("hahah");System.out.println("我是C类的有参构造");}
}我是A类
hahah我是B类的有参构造
我是C类的有参构造
我是c类的无参构造
![]()
多态:方法或对象具有多种形态,是oop的第三大特征,是建立在封装和继承基础之上的
多态的具体体现
1.方法多态
(1)重载体现多态(2)重写体现多态
2.对象多态
(1)对象的编译类型和运行类型可以不一致,编译类型在定义时就确定,不能不能变化
(2)对象的运行类型是可以变化的,可任意通过getclass()来查看运行类型
(3)编译类型看定时 = 号的左边,运行类型看 = 号的右边
![]()
1.当调用对象的方法时,该方法会和对象的内存地址/运行类型绑定。
2.当调用对象的属性时,没有动态绑定机制,哪里声明,哪里使用。
