项目代码
https://github.com/yinhai1114/Java_Learning_Code/tree/main/IDEA_Chapter08/src/com/yinhai/homework_
目录
1.
定义一个类Person类{name , age , job}初始化Person对象数组,有3个person对象,并按照age从大到小进行排序,提示使用冒泡排序 Homework01
package com.yinhai.homework_.homework01;
public class HomeWork01 {
public static void main(String[] args) {
Person[] person = new Person[3];
person[0] = new Person("xiaowang",30,"engineer");
person[1] = new Person("xiaohuang",18,"student");
person[2] = new Person("laowang",66,"retire");
Person person1 = person[0]; //多态数组 这个还是没用习惯
person1.printArr(person);//指一个person1出来调用,或者person[0].sortArr
//其实更好的应该再写个子类才对 叫class Features
System.out.println("\n===========================");
person1.sortArr(person);
person1.printArr(person);
}
}
class Person{
String name;
int age;
String job;
public Person(String name, int age, String job) {
this.name = name;
this.age = age;
this.job = job;
}
public void sortArr(Person[] p){
Person temp = null;
for (int i = 0; i < p.length - 1; i++) {
for (int j = 0; j < p.length - i - 1; j++) {
if(p[j].age > p[j + 1].age){
temp = p[j + 1];
p[j + 1] = p[j];
p[j] = temp;
}
}
}
}
public void printArr(Person[] p){
for (int i = 0; i < p.length; i++) {
System.out.println(p[i]);
}
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", job='" + job + '\'' +
'}';
}
}
2.
写出四种访问修饰符和各自的访问权限
public 本类 同包 子类 不同包
protected 本类 同包 子类
默认 本类 同包
private 本类
3.
(1)要求有属性"姓名name”,"年龄age" ,“职称post" ,”基本工资salary"
(2)编写业务方法,introduce () ,实现输出一个教师的信息。
(3)编写教师类的三个子类:教授类(Professor )、副教授类、讲师类。工资级别分别为:教
授为1.3、副教授为1.2、讲师类1.1.在三个子类里面都重写父类的introduce () 方法。
(4)定义井初始化一个老师对象,调用业务方法,实现对象基本信息的后台打印.
package com.yinhai.homework_.homework03;
public class Homework03 {
public static void main(String[] args) {
Teacher professor = new Professor("xiaowang",18,2000);
professor.introduce();
AssociateProfessor laowang = new AssociateProfessor("laowang", 50, 50000);
laowang.introduce();
Lecturer lecturer = new Lecturer("yinhai",18,7000);
lecturer.introduce();
Teacher teacher = new Teacher("test",00);
teacher.introduce();
}
}
public class Teacher {
private String name;
private int age;
private String post;
private double salary;
public Teacher(String name, int age) {
this.name = name;
this.age = age;
this.post = getClassName(getClass().getName());
}
public String getClassName(String className){
int lastDotIndex = className.lastIndexOf(".");
if (lastDotIndex != -1) {
className = className.substring(lastDotIndex + 1);
}
return className;
}
public void introduce(){
System.out.println("===========这是老师的信息===========");
System.out.println(this);
}
@Override
public String toString() {
return "name=" + name +
", age=" + age +
", post= "+ post
;
}
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 class Professor extends Teacher{
private double salary;
private double grade = 1.3;
public Professor(String name, int age, double salary) {
super(name, age);
this.salary = salary;
}
public void introduce(){
System.out.println("===========这是教授的信息===========");
System.out.println(this + ", salary " + salary + ", grade" + grade);
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}
public class AssociateProfessor extends Teacher{
private double salary;
private double grade = 1.2;
public AssociateProfessor(String name, int age, double salary) {
super(name, age);
this.salary = salary;
}
public void introduce(){
System.out.println("===========这是副教授的信息===========");
System.out.println(this + ", salary " + salary + ", grade" + grade);
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}
public class Lecturer extends Teacher{
private double salary;
private double grade = 1.1;
public Lecturer(String name, int age, double salary) {
super(name, age);
this.salary = salary;
}
public void introduce(){
System.out.println("===========这是讲师的信息===========");
System.out.println(this + ", salary " + salary + ", grade" + grade);
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}
public Teacher(String name, int age) { this.name = name; this.age = age; this.post = getClassName(getClass().getName()); } public String getClassName(String className){ int lastDotIndex = className.lastIndexOf("."); if (lastDotIndex != -1) { className = className.substring(lastDotIndex + 1); } return className; }
运行什么类的时候职称就出来了,为了省事使用了获取类名字的方法,详细参考
4.
通过继承实现员工工资核算打印功能
父类:员工类
子类:部门经理类、普通员工类
(1)部门经理工资= 1000+单日工资*天数*等级(1.2) 。
(2)普通员工工资=单日工资*天数*等级(1.0) ;
(3)员工属性:姓名,单日工资,工作天数
(4)员工方法(打印工资)
(5)普遍员工及部门经理都是员工子类,需要重写打印工资方法。
(5)定义并初始化普通员工对象,调用打印工资方法输入工资,定义并初始化部广经理对象,
调用打印工资方法输入工资
package com.yinhai.homework_.homework04;
public class HomeWork04 {
public static void main(String[] args) {
Manager xiaowang = new Manager("小王",100,31);
Staff xiaochen = new Staff("小陈", 400, 31);
xiaowang.setBonus(1000);
xiaowang.printSalary();
xiaochen.printSalary();
}
}
public class Employee {
private String name;
private double dailySalary;
private int workdays;
public Employee(String name, double dailySalary, int workdays) {
this.name = name;
this.dailySalary = dailySalary;
this.workdays = workdays;
}
public void printSalary(){
System.out.println(dailySalary);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getDailySalary() {
return dailySalary;
}
public void setDailySalary(double dailySalary) {
this.dailySalary = dailySalary;
}
public int getWorkdays() {
return workdays;
}
public void setWorkdays(int workdays) {
this.workdays = workdays;
}
}
public class Staff extends Employee {
private double salary;
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public Staff(String name, double dailySalary, int workdays) {
super(name, dailySalary, workdays);
this.salary = dailySalary * workdays * 1.0;
}
public void printSalary(){
System.out.println("这是" + getName() + "的工资为" + salary);
}
}
public class Manager extends Employee {
private double salary;
private double bonus;
public Manager(String name, double dailySalary, int workdays,double bonus) {
super(name, dailySalary, workdays);
this.salary = 1000 + dailySalary * workdays * 1.2;
this.bonus = bonus;
}
public void printSalary(){
System.out.println("这是" + getName() + "的工资为" + salary);
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}
5.
5.设计父类一员工类。 子类:工人类(Worker),农民类(Peasant),教师类(Teacher),科学家类
(Scientist),服务生类(Waiter)。
(1)其中工人,农民,服务生只有基本工资sal
(2)教师除基本工资外还有课酬(元/天) classDay, classSal
(3)科学家除基本工资外还有年终奖
(4)编写一个测试类,将各种类型的员工的全年工资打印出来
.
public class HomeWork05 {
public static void main(String[] args) {
Peasant peasant = new Peasant(5000);
Waiter waiter = new Waiter(6000);
Worker worker = new Worker(4000);
Teacher teacher = new Teacher(5000, 100, 150);
Scienist scienist = new Scienist(5000, 20000);
peasant.printSal();
waiter.printSal();
worker.printSal();
teacher.printSal();
scienist.printSal();
}
}
public class Empolyee {
private double sal;
public void printSal(){
System.out.println("的年工资为" + sal * 12);
}
public Empolyee(double sal) {
this.sal = sal;
}
public double getSal() {
return sal;
}
public void setSal(double sal) {
this.sal = sal;
}
}
public class Peasant extends Empolyee {
public Peasant(double sal) {
super(sal);
}
public void printSal(){
System.out.print("农民");
super.printSal();
}
}
public class Scienist extends Empolyee{
private double bonus;
public Scienist(double sal, double bonus) {
super(sal);
this.bonus = bonus;
}
public void printSal(){
System.out.print("科学家的工资为" + (getSal() * 12 + bonus));
}
}
public class Teacher extends Empolyee{
private int classDay;
private double classSal;
public Teacher(double sal, int classDay, double classSal) {
super(sal);
this.classDay = classDay;
this.classSal = classSal;
}
public void printSal(){
System.out.print("老师的工资为" + (getSal() * 12 + classSal * classDay) + "\n");
}
}
public class Waiter extends Empolyee{
public Waiter(double sal) {
super(sal);
}
public void printSal(){
System.out.print("农民");
super.printSal();
}
}
public class Worker extends Empolyee{
public Worker(double sal) {
super(sal);
}
public void printSal(){
System.out.print("农民");
super.printSal();
}
}
可以把年工资的月份数设成父类变量,这里就不做演示了
6.
6.假定Grand、Father 和Son在同一个包,问:父类和子类中通过this和super都可以调用哪些属性和方法,
class Grand{
String name= "AA":
private int age= 100;
public void g1(){}
}
class Father extends Grand{
String id="001":
private double score;
public void f1(){
//super可以访问哪些成员(属性和方法) ?
//this可以访问哪些成员?
}
}
class Son extends Father{
String name="BB" ;
pubic void 9100
private void show(){
//super可以访问哪些成员(属性和方法)?
//this可以访问哪些成员?
}
}
class Father //内可以访问
super.name;
super.g1();
this.id;this.f1();this.name;this.g1()
class Son内可以调用的
super.id;super.f1();super.name;super.g1();
this.name;this.g1(); // 这两个是Son类的,父类的两个无法查找到
this.show();this.id;this.f1()
7.
package com.yinhai.homework_;
class Test {
String name = "Rose";
Test() {
System.out.println("Test");
}
Test(String name) {//堆里的对象name = 传入的值
this.name = name;
}
}//父类
class Demo extends Test {//子类
String name = "Jack";
Demo() {//在创建对象时执行无参构造器初始化
super();//父类的无参构造
System.out.println("Demo");
}
Demo(String s) {//创建对象时该构造器初始化
super(s);//父类的String构造器
}
public void test() {//根据查找关系使用该方法 super为父类name为Rose
//this为本类开始查找为Jack
System.out.println(super.name);
System.out.println(this.name);
}
public static void main(String[] args) {
new Demo().test();
new Demo("john").test();
}
}
//1.out Test 2.out Demo 3.out Rose 4.out Jack
//1.out john 2.out Jack
8.
要求:
1)在上面类的基础上扩展新类CheckingAccount”对每次存款和取款都收取1美元的手续费
(2)扩展前个练习的BankAccount类,新类SavingsAccount每个月都有利息产(earnMonthlyInterest方法被调用),并且有每月三次免手续费的存款或取款。在earnMonthlyInterest方法中重置交易计数
(3)体会重写的好处
public class HomeWork08 {
public static void main(String[] args) {
CheckingAccount checkingAccount = new CheckingAccount(1000);
checkingAccount.deposit(10);//存10块钱 收取1手续费实际存9元
checkingAccount.withdraw(10);//取10元 收取1手续费 实际取11元
System.out.println(checkingAccount.getBalance());
SavingAccount savingAccount = new SavingAccount(1000);
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.deposit(100);//免手续费
System.out.println(savingAccount.getBalance());
}
}
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(double balance) {
this.balance = balance;
}
}
public class CheckingAccount extends BankAccount{
public CheckingAccount(double initialBalance) {
super(initialBalance);
}
public void deposit(double amount){
super.deposit(amount - 1);
}
public void withdraw(double amount){
super.withdraw(amount + 1);
}
}
public class SavingAccount extends BankAccount {
private int count = 3;
private double rate = 0.01;
public SavingAccount(double initialBalance) {
super(initialBalance);
}
public void earnMonthlyInterest() {
count = 3;
super.deposit((getBalance()) * rate);
//调用存款方法,传入getBanlce * rate
}
@Override
public void deposit(double amount) {
if (count > 0) {
count--;
super.deposit(amount);
} else {
super.deposit(amount);
}
}
@Override
public void withdraw(double amount) {
if (count > 0) {
count--;
super.withdraw(amount);
} else {
super.withdraw(amount);
}
}
}
9.
设计一一个Point类,其x和y坐标可以通过构造器提供。提供一个子类LabeledPoint, 其构造器接受一个标签值和x,y坐标,比如:new LabeledPoint( "BlackThursday" ,1929,230.07),写出对应的构造器即可Homework09java
public class LabeledPoint extends Point{
private String labels;
public LabeledPoint(double xcoordinate, double ycoordinate, String labels) {
super(xcoordinate, ycoordinate);
this.labels = labels;
}
}
10.
编写Doctor类{name, age, job, gender, sal}相应的getter0和setter()方法,5个参数的构造器,重写父类的equals(方法: publicboolean equals(Object obj),并判断测试类中创建的两个对象是否相等.相等就是判断属性是否相同
public class HomeWork10 {
public static void main(String[] args) {
Doctor doctor1 = new Doctor("xiaowang", 20, "doctor", "nan", 2000);
Doctor doctor2 = new Doctor("xiaowang", 20, "doctor", "nan", 2000);
Doctor doctor3 = new Doctor("xiaowang", 21, "doctor", "nan", 2000);
Doctor doctor4 = doctor1;
Teacher teacher = new Teacher("xiaowang", 20, "doctor", "nan", 2000);
System.out.println(doctor1.equals(doctor2));
System.out.println(doctor1.equals(doctor3));
System.out.println(doctor1.equals(doctor4));
System.out.println(doctor1.equals(teacher));
}
}
public class Doctor {
private String name;
private int age;
private String job;
private String gender;
private double sal;
public Doctor(String name, int age, String job, String gender, double sal) {
this.name = name;
this.age = age;
this.job = job;
this.gender = gender;
this.sal = sal;
}
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if(!(obj instanceof Doctor)){//如果不是子类直接返回false 避免出现不同类的指空异常
return false;
}
Doctor doctor = (Doctor) obj;//是同一个类或者子类,向下转型调用特有的属性
boolean temp = this.name.equals(doctor.name) && this.age == doctor.age &&
this.job.equals(doctor.job) && this.gender.equals(doctor.gender) &&
this.sal == doctor.sal;
return temp;
}
public class Teacher {
private String name;
private int age;
private String job;
private String gender;
private double sal;
public Teacher(String name, int age, String job, String gender, double sal) {
this.name = name;
this.age = age;
this.job = job;
this.gender = gender;
this.sal = sal;
}
}
}
11.
现有Person类,里面有方法run、eat, Student类继承 了Person类,并重写了run方法,自定义study方法试写出对象向上转型和向下转型的代码,并写出各自都可以调用哪些方法,并写出方法输出什么?
public class HomeWork11 {
public static void main(String[] args) {
Person student = new Student();//向上转型 //编译类型是Person 运行类型是Student
//所以能调用的应该是Person类内的所有方法
//但运行时的方法看运行类型,因为run被重写了,所以在编译阶段能写的如下
student.run();
student.eat();
//而运行时,run方法是Student内的,eat则是Person内的 输出 student run 和 person eat
Student student1 = (Student)student;//向下转型,这时候编译类型是Student
//此时编译阶段能调用子类特有的方法,即study。以及父类的所有方法,因为是继承关系,所以都有
System.out.println("================================");
student1.run();
student1.study();
student1.eat();
//运行时,根据查找关系,run study是Student类内的,而eat是Person类内的
//输出 student run student study person eat
}
}
12.
名称 概念 用于基本数据类型 用于引用类型
== 比较运算符 判断值是否相等 判断地址是否相同即看是否是同一个对象
equals Object内的方法 不行 默认判断两个对象是否是同一个对象,
但是子类往往重写该方法,
比较对象的属性值是否相等
13.综合性很强 可以多看看
public class HomeWork13 {
public static void main(String[] args) {
Student student = new Student("xiaowang", "nan", 18, "011114");
Teacher teacher = new Teacher("Mr.chen", "nan", 30, 10);
student.printInfo();
System.out.println("-------------------------");
teacher.printInfo();
System.out.println("========================================================");
//7.多态数组
Person[] people = new Person[4];
people[0] = new Student("xiao", "nv", 14, "011514");
people[1] = new Student("wang", "nan", 1, "000000");
people[2] = new Teacher("Mr.wang", "nan", 70, 30);
people[3] = new Teacher("Ms.chen", "nv", 50, 20);
//创建一个对象为homework13 调用排序方法
HomeWork13 homework13 = new HomeWork13();
homework13.sortArr(people);
System.out.println("=====================排序后的数组=========================");
for (int i = 0; i < people.length; i++) {
System.out.println(people[i]);
System.out.println();
}
System.out.println("========================================================");
//调用子类特有方法uniqueMethod
homework13.useUniqueMethod(people[0]);
homework13.useUniqueMethod(people[3]);
}
public void sortArr(Person[] people){//排序方法
Person temp = null;
for (int i = 0; i < people.length - 1; i++) {
for (int j = 0; j < people.length - i - 1; j++) {
if(people[j].getAge() < people[j + 1].getAge()){
temp = people[j];
people[j] = people[j + 1];
people[j + 1] = temp;
}
}
}
}
public void useUniqueMethod(Person p){//调用方法
if(p instanceof Student){
Student p1 = (Student)p;
p1.study();
}
if(p instanceof Teacher){
Teacher p1 = (Teacher)p;
p1.teach();
}
}
}
public class Person {
private String name;
private String sex;
private int age;
public Person(String name, String 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 String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String play(){
return name;
}
@Override
public String toString() {//需要有一个打印信息的方法
return "姓名:" + name + "\n" + "年龄:" + age + "\n" + "性别:" + sex + "\n" ;
}
public void printInfo(){
System.out.println(toString());
}
}
public class Student extends Person {
private String stu_id;
public Student(String name, String 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;
}
public void study() {
System.out.println("我会好好学习");
}
@Override
public String play() {
return super.play() + "爱玩足球";
}
@Override
public String toString() {
return "学生的信息:\n" + super.toString() + "学号:"+ stu_id ;
}
public void printInfo(){
System.out.println(toString());
study();
System.out.println(play());
}
}
public class Teacher extends Person{
private int work_age;
public Teacher(String name, String sex, int age, int work_age) {
super(name, sex, age);
this.work_age = work_age;
}
public int getWork_age() {
return work_age;
}
public void setWork_age(int work_age) {
this.work_age = work_age;
}
public void teach(){
System.out.println("我会认真教书");
}
@Override
public String play() {
return super.play() + "爱玩象棋";
}
@Override
public String toString() {
return "老师的信息:\n" + super.toString() + "工龄:"+ work_age ;
}
public void printInfo(){
System.out.println(toString());
teach();
System.out.println(play());
}
}
14.
输出
我是A类
hahah 我是B类的有参构造
我是c类的有参构造
我是c类的无参构造
流程,创建对象后因为默认无参,所以跑public C{}碰到this,调用该类的有参构造public C(Sting name),碰到super,跳到public B(String name),但在调用B类时语句块内默认有个super()跳到public A()输出完依次回退
15.什么是多态,多态具体体现有哪些(举例说明)
多态:方法或者对象具有多种形态,是OOP的第三大特征,建立在继承和封装的基础之上
体现:方法的多态 1)方法重载 2)方法的重写
2.对象的多态 1)编译类型和运行类型可以不一致 2)对象的运行类型是可变的 可以通过getClass()获取运行类型 3)编译类型看等号左边 运行类型看等号右边
16.JAVA动态绑定机制是什么
当调用对象的方法时,该方法会和对象的内存地址绑定,即和类型运行类型绑定
当调用对象的属性时,没有动态绑定机制,哪里声明,哪里使用