JAVA 面向对象编程作业(Chapter08)

1.定义person类,初始化对象数组,有3个person对象,按照age从大到小进行排序。

package com.HomeworkChapter08;

public class Homework01 {
    public static void main(String[] args) {
        //根据 age 使用冒泡排序法
        //思路:用数组保存三个人的信息,然后直接使用冒泡排序
        Person person[] = new Person[3];
        person[0] = new Person("jack01", 23, "教授");
        person[1] = new Person("jack02", 56, "经理");
        person[2] = new Person("jack03", 43, "工人");
        Person temp;//创建一个中间转换用的引用
        for (int i = 1; i < person.length; i++) {
            for(int j = 0; j < person.length - i; j++) {
                if (person[j].getAge() < person[j + 1].getAge()){
                    temp = person[j + 1];
                    person[j + 1] = person[j];
                    person[j] = temp;
                }
            }
        }
        for (int i = 0; i < person.length; i++) {
            System.out.println(person[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 + '\'' +
                '}';
    }
}

2.写出四种访问修饰符和各自的访问权限

访问修饰符同类同包子类不同包
public可以可以可以可以
protected可以可以可以不可以
默认可以可以不可以不可以
private可以不可以不可以不可以

3.编写教师类、及其子类,并初始化对象,后测试使用。(具体要求省略,题目简单)

package com.HomeworkChapter08;


public class Teacher {
    private String name;
    private int age;
    private String post;//对应职称
    private double sal;

    public Teacher(String name, int age, double sal) {
        this.name = name;
        this.age = age;
        this.sal = sal;
    }

    public Teacher(String name, int age, String post, double sal) {
        this.name = name;
        this.age = age;
        this.post = post;
        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 getPost() {
        return post;
    }

    public void setPost(String post) {
        this.post = post;
    }

    public double getSal() {
        return sal;
    }

    public void setSal(double sal) {
        this.sal = sal;
    }

    @Override
    public String toString() {
        return "Teacher{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", post='" + post + '\'' +
                ", sal=" + sal +
                '}';
    }
    public void introduce() {//实现输出一个教师的信息
        System.out.println("Teacher{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", post='" + post + '\'' +
                ", sal=" + sal +
                '}');
    }
}

package com.HomeworkChapter08;

public class Professor extends Teacher {
    private double level = 1.3;
    public Professor(String name, int age,  double sal) {
        super(name, age,  sal);
        super.setPost("Professor");
    }

    public double getLevel() {
        return level;
    }

    public void setLevel(double level) {
        this.level = level;
    }

    @Override
    public void introduce() {
        System.out.println("Professor{" +
                "name='" + super.getName() + '\'' +
                ", age=" + super.getAge() +
                ", post='" + super.getPost() + '\'' +
                ", sal=" + super.getSal() +
                ", salary level=" + this.level +
                '}');
    }
}

package com.HomeworkChapter08;

import java.security.PrivateKey;

public class Homework02 {
    public static void main(String[] args) {
        Professor professor = new Professor("jack", 34, 1223);
        professor.introduce();
    }
}

4.通过继承实现员工工资核算打印功能。

package com.HomeworkChapter08;

public class Homework04 {
    public static void main(String[] args) {
        Manager manager = new Manager("jack", 100, 34);
        Worker worker = new Worker("Rose", 100, 34);
        manager.showSalary();
        worker.showSalary();


    }
}
class Employee {
    //抽象出属性和方法
    //属性:姓名 单日工资 工作天数 等级;
    private String name;
    private double sal;
    private int day;
    private double grade ;//等级

    public Employee(String name, double sal, int day, double grade) {
        this.name = name;
        this.sal = sal;
        this.day = day;
        this.grade = grade;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getSal() {
        return sal;
    }

    public void setSal(double sal) {
        this.sal = sal;
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }

    public double getGrade() {
        return grade;
    }

    public void setGrade(double grade) {
        this.grade = grade;
    }

    //方法: 打印工资
    public void showSalary(){
        System.out.println("name: " + name + " 工资为:" + sal * day * grade);
    }
}
class Manager extends Employee {
    public Manager(String name, double sal, int day) {
        super(name, sal, day, 1.2);
    }

    @Override
    public void showSalary() {
        System.out.println("Manager name: " + getName() + " 工资为:" + (1000 + getSal() * getDay() * getGrade()));
    }
}
class Worker extends Employee {
    public Worker(String name, double sal, int day) {
        super(name, sal, day, 1.0);
    }
    @Override
    public void showSalary() {
        System.out.println("Worker name: " + getName() + " 工资为:" +  getSal() * getDay() * getGrade());
    }
}

5.设计员工类(父类)和子类,打印年工资。

package com.HomeworkChapter08;

public class Homework05 {
    public static void main(String[] args) {
        Worker02 worker = new Worker02("jack", 1000);
        Teacher02 teacher = new Teacher02("Rose", 3000, 120, 10);
        Scientist scientist = new Scientist("richard", 5000, 20000);
        worker.showSal();
        teacher.showSal();
        scientist.showSal();
    }
}
class Employee02 {//同包避免重名
    private String name;
    private double sal;//每月基本工资

    public Employee02(String name, double sal) {
        this.name = name;
        this.sal = sal;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getSal() {
        return sal;
    }

    public void setSal(double sal) {
        this.sal = sal;
    }
    public void showSal(){
        System.out.println("name: " + name + " 全年工资: " + 12 * sal);
    }
}
class Worker02 extends Employee02 {
    public Worker02(String name, double sal) {
        super(name, sal);
    }

    @Override
    public void showSal() {
        System.out.println("Worker name: " + getName() + " 全年工资: " + 12 * getSal());
    }
}
class  Teacher02 extends Employee02{
    private int classDay;
    private double classSal;//上课

    public Teacher02(String name, double sal, int classDay, double classSal) {
        super(name, sal);
        this.classDay = classDay;
        this.classSal = classSal;
    }

    public int getClassDay() {
        return classDay;
    }

    public void setClassDay(int classDay) {
        this.classDay = classDay;
    }

    @Override
    public void showSal() {
        System.out.println("Teacher name: " + getName() +
                " 全年工资: " + (12 * getSal() + classDay * classSal));
    }
}
class Scientist extends Employee02 {
    private double bonus;

    public Scientist(String name, double sal, double bonus) {
        super(name, sal);
        this.bonus = bonus;
    }

    public double getBonus() {
        return bonus;
    }

    public void setBonus(double bonus) {
        this.bonus = bonus;
    }

    @Override
    public void showSal() {
        System.out.println("Worker name: " + getName() + " 全年工资: " + (12 * getSal() + this.bonus));
    }
}

6.super 和 this 的访问权限

关键字属性方法
super父类的属性(public,protected以及同包的默认无访问修饰符的属性)父类的方法(满足访问修饰符的限制范围)
this子类的所有属性以及父类可以访问的属性子类的所有方法以及父类可以访问的方法

具体看笔记关于super和this的区别 P309.

7.写出程序结果

考察的是子类父类构造器的调用。

子类构造器默认调用父类无参构造器,可通过super选择构造器的使用。

8.银行存款问题扩展:体会函数重写的优点

父类中有存款取款函数,子类存取款需要修改,则直接调用然后修改参数即可。super.deposit(amount - fee)

package com.HomeworkChapter08;

public class Homework08 {
    public static void main(String[] args) {
        SavingAccount savingAccount = new SavingAccount(1000);
        savingAccount.deposit(1000);
        savingAccount.deposit(1000);
        savingAccount.deposit(1000);
        System.out.println(savingAccount.getBalance());
        savingAccount.deposit(1000);
        System.out.println(savingAccount.getBalance());
        savingAccount.earnMoneyInterest();
        System.out.println(savingAccount.getBalance());


    }
}
class BankAccount{
    private double balance;

    public BankAccount(double balance) {
        this.balance = balance;
    }
    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;
    }
}
class CheckingAccount extends BankAccount {
    private double fee = 1.0;//手续费

    public CheckingAccount(double balance, double fee) {
        super(balance);
        this.fee = fee;
    }

    public CheckingAccount(double balance) {
        super(balance);
    }

    public double getFee() {
        return fee;
    }

    public void setFee(double fee) {
        this.fee = fee;
    }

    @Override
    public void deposit(double amount) {
        super.deposit(amount - 1);
    }

    @Override
    public void withdraw(double amount) {
        super.withdraw(amount + 1);
    }
}
class SavingAccount extends BankAccount{
    private int count = 3; //免手续费的操作次数
    private  double moneyInterest = 0.03;//利息

    public SavingAccount(double balance, int count, double moneyInterest) {
        super(balance);
        this.count = count;
        this.moneyInterest = moneyInterest;
    }

    public SavingAccount(double balance) {
        super(balance);
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public double getMoneyInterest() {
        return moneyInterest;
    }

    public void setMoneyInterest(double moneyInterest) {
        this.moneyInterest = moneyInterest;
        this.count = 3;//重置次数
    }

    public void earnMoneyInterest() {
        super.deposit(getBalance() * moneyInterest);//重新设置利润
        count = 3;
    }

    @Override
    public void deposit(double amount) {
        if (this.count > 0) {
            count--;
            super.deposit(amount);//不需要手续费
        } else {
            super.deposit(amount - 1);//需要手续费
        }
    }

    @Override
    public void withdraw(double amount) {
        if (this.count > 0) {
            count--;
            super.withdraw(amount);//不需要手续费
        } else {
            super.withdraw(amount + 1);//需要手续费
        }
    }
}

9.构造器的使用。super

class Point {
    private double x;
    private double y;

    public Point(double x, double y) {
        this.x = x;
        this.y = y;
    }
}
class LabelPoint extends Point {
    private String name;

    public LabelPoint( String name, double x, double y) {
        super(x, y);
        this.name = name;
    }
}

10.掌握 equals 函数的重写(自带的和自己写的)

首先判断是否是同一个对象,若是,则直接相等;

若不是同一个对象,则判断是否是同一类,(getclass() 返回的是运行类型)

最后判断内容是否相等。记得要向下转型,这样才能访问子类的特有方法和属性

//    @Override
//    public boolean equals(Object o) {
//        if (this == o) return true;
//        if (o == null || getClass() != o.getClass()) return false;
//        Doctor doctor = (Doctor) o;
//        return age == doctor.age &&
//                gender == doctor.gender &&
//                Double.compare(doctor.sal, sal) == 0 &&
//                Objects.equals(name, doctor.name) &&
//                Objects.equals(job, doctor.job);

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Doctor)) return false;
        Doctor obj = (Doctor)o;
        return this.age == obj.age &&
                this.gender == obj.gender &&
                this.sal == obj.sal &&
                this.name.equals(obj.name) &&
                this.job.equals(obj.job);
    }

11.向上转型和向下转型

  • 向上转型:父类的引用指向了子类的对象,可以调用父类的所有成员(遵守访问权限),不能调用子类的特有成员
  • 向下转型:把指向子类对象的父类引用,转成指向子类对象的子类引用,转型后可以调用子类类型中的所有成员。

12.== 和 equals 的区别

名称概念基本数据类型引用类型
==比较运算符号可以,判断数数值是否相等可以,用于判断两个对象是否相等,判断的是对象地址
equalsObject类的方法,Java类都可使用equals不可以可以,默认是判断对象是否相等,但是子类往往需要重写,比较对象的属性是否相等。

13.老师和学生继承person类,然后再分别写特有方法以及重写play方法

     package com.HomeworkChapter08;

public class Homework13 {
    public static void main(String[] args) {
        Student student = new Student("小明", 15, '男', "00023102");
        Teacher03 teacher03 = new Teacher03("张飞", 30, '男', 5);
        student.printInfo();
        System.out.println("-----------------");
        teacher03.printInfo();
        Person01[] person = new Person01[4];//创建数组,开辟三个数组空间
        person[0] = new Student("小明", 15, '男', "00023102");
        person[1] = new Student("小红", 18, '男', "00023102");
        person[2] = new Teacher03("王刚", 34, '男', 6);
        person[3] = new Teacher03("张飞", 30, '男', 5);
        for (int i = 0; i < person.length; i++) {
            System.out.println(person[i].getName() + "  " + person[i].getAge());
        }
        //要求按照年龄从高到低进行排序
        Person01 temp;//创建中间替换的对象引用
        for (int i = 1; i < person.length; i++) {
            for (int j = 0; j < person.length - i; j++) {
                if (person[j].getAge() < person[j + 1].getAge()) {
                    temp = person[j + 1];
                    person[j + 1] = person[j];
                    person[j] = temp;
                }
            }
        }
        System.out.println("-------------------");
        for (int i = 0; i < person.length; i++) {
            System.out.println(person[i].getName() + "  " + person[i].getAge());
        }
        Person01 person01 = new Person01("小东", 13, '男');
        person01.call(student);

    }
}

class Person01 {
    private String name;
    private int age;
    private char gender;

    public Person01(String name, int age, char gender) {
        this.name = name;
        this.age = age;
        this.gender = gender;
    }

    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 char getGender() {
        return gender;
    }

    public void setGender(char gender) {
        this.gender = gender;
    }

    public String play() {
        return this.name + "爱玩";
    }

    public void printInfo() {
        System.out.println("姓名:" + this.name +
                "\n年龄:" + this.age +
                "\n性别:" + this.gender);
    }

    public void call(Person01 p) {
        Student student = (Student)p;
        student.study();
//        System.out.println(student.play());
    }
}

class Student extends Person01 {
    private String stu_id;

    public Student(String name, int age, char gender, String stu_id) {
        super(name, age, gender);
        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 void printInfo() {
        System.out.println("学生的信息:");
        super.printInfo();
        this.study();
        System.out.println(this.play());
    }
}

class Teacher03 extends Person01 {
    private int work_age;

    public Teacher03(String name, int age, char gender, int work_age) {
        super(name, age, gender);
        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 void printInfo() {
        System.out.println("老师的信息:");
        super.printInfo();
        this.teach();
        System.out.println(this.play());
    }
}

14.构造器的使用

子类的构造器会调用的父类的构造器。若子类中构造器中调用了this(),则在调用的构造器中再调用父类的构造器。

public class Test01 {
    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 类的无参构造器

15.多态的含义和体现

多态概念:方法或对象具有多种形态,是 OOP 的第三大特征,是建立在封装和继承的基础上

多态体现:

  • 方法多态:(1)重载体现多态 (2)重写体现多态
  • 对象多态:
    • 1.编译类型和运行类型可以不一致,编译在定义时就确定,不能变化;
    • 2.运行类型时可以变化的,可以通过getclass()查看运行类型;
    • 编译看 == 左边 ,运行类型看 == 右边

16.Java的动态绑定机制是什么

  • 1.当调用对象的方法时,该方法会和对象的内存地址/运行类型绑定;
  • 2.当调用对象的属性时,没有动态绑定机制,那里声明,哪里使用。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值