面向对象编程中级部分习题

1.作业1

定义一个Person类{name, age, job},初始化Person对象数组,有3个person对象,并按照age从大到小进行排序,提示:使用冒泡排序.

package com.yt.homwork.homework01;

public class HomeWork01 {
    public static void main(String[] args) {
        Person[] people = new Person[3];
        people[0] = new Person("张三",20,"程序员1");
        people[1] = new Person("李四",18,"程序员2");
        people[2] = new Person("王五",26,"程序员3");


        System.out.println("排序前~");
        for (int i=0; i<people.length; i++) {
//            System.out.println(people[i].toString());
            System.out.println(people[i]);//默认会调用重写之后的toString()方法
        }

        Person temp = null;
        boolean flag = false;
        for (int i=0; i<people.length-1; i++){//外层循环
//            System.out.println(people[i].toString());
            for (int j=0; j< people.length - 1 - i; j++){//内层循环
                //按年龄从大到小排序
                if (people[j].getAge() < people[j+1].getAge()){
                    flag = true;
                    temp = people[j];
                    people[j] = people[j+1];
                    people[j+1] = temp;
                }
            }
            // 注意:标志位的判断要在外层循环中
            if (!flag){
                break;
            } else {
                flag = false;
            }
        }

        System.out.println("排序后~");
        for (int i=0; i<people.length; i++) {
            System.out.println(people[i].toString());
        }
    }

}
public 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.作业2(访问修饰符)

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

  1. private
  2. protected
  3. 默认权限
  4. public
本类同包子类不同包
private×××
protected××
默认权限×
public

3.作业3

3.编写老师类Homework03.java
1)要求有属性“姓名name”,“年龄age”,“职称post”,“基本工资salary”;

2)编写业务方法,introduce(),实现输出一个教师的信息。

3)编写教师类的三个子类:教授类(Professor)、副教授类、讲师类。

工资级别分别为:教授为1.3、副教授为1.2、讲师类1.1。

在三个子类里面都重写父类的introduce()方法。

4)定义并初始化一个老师对象,调用业务方法,实现对象基本信息的后台打印。

package com.yt.homwork.homework02;

public class Homework03 {
    public static void main(String[] args) {
        Teacher teacher = new Teacher("张三", 50, "高级", 120000,1.3);
        System.out.println(teacher.introduce());

    }
}
class Teacher {
    private String name;
    private int age;
    private String post;
    private double salary;
    private double salary_level;

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

    public String introduce() {
        return "name=" + this.name + " age=" + this.age
                + " post=" + this.post + " salary=" + salary
                + " salary_level=" + salary_level;
    }

    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 getSalary_level() {
        return salary_level;
    }

    public void setSalary_level(double salary_level) {
        this.salary_level = salary_level;
    }
}
class Professor extends Teacher {

    public Professor(String name, int age, String post, double salary, double salary_level) {
        super(name, age, post, salary, salary_level);
    }

    @Override
    public String introduce() {
        return super.introduce();
    }

}

class AssProfessor extends Teacher {
    private String salary_level;

    public AssProfessor(String name, int age, String post, double salary, double salary_level) {
        super(name, age, post, salary, salary_level);
    }

    @Override
    public String introduce() {
        return super.introduce();
    }
}

class Lecturer extends Teacher {
    public Lecturer(String name, int age, String post, double salary, double salary_level) {
        super(name, age, post, salary, salary_level);
    }

    @Override
    public String introduce() {
        return super.introduce();
    }
}

4.作业4

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

父类:员工类(Employee)
子类:部门经理类(Manager)、普通员工类(Worker)

(1)部门经理工资=1000+单日工资*天数*等级(1.2)。=>奖金+基本工资

(2)普通员工工资=单日工资*天数*等级(1.0) ; =>基本工资
(3)员工属性:姓名,单日工资,工作天数
(4)员工方法(打印工资)
(5)普遍员工及部门经理都是员工子类,需要重写打印工资方法。
(5)定义并初始化普通员工对象,调用打印工资方法输出工资,定义并初始化部门经理对象,调用打印工资方法输出工资。

public class PrintSalaryDemo {
    public static void main(String[] args) {
        Worker jack = new Worker("jack", 100.0, 10, 1.0);
        jack.printSalary();

        Manager tom = new Manager("tom", 200.0, 10, 1.2);
        tom.setBonus(1000.0);
        tom.printSalary();

    }
}
package com.yt.homwork.homework04;

public class Employee {
    private String name;
    private Double salary;
    private int day;
    private double level;

    public Employee(String name, Double salary, int day, double level) {
        this.name = name;
        this.salary = salary;
        this.day = day;
        this.level = level;
    }

    //编写打印工资的方法
    public void printSalary(){
        System.out.println("员工" + name + "的工资= " + (this.salary * this.day*this.level));
    }

    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 int getDay() {
        return day;
    }

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

    public double getLevel() {
        return level;
    }

    public void setLevel(double level) {
        this.level = level;
    }
}
public class Manager extends Employee {
    private double bonus;

    //因为奖金在一开始是不能确定的,构造器中就不用初始化
    //后面通过set方法来设置
    public Manager(String name, Double salary, int day, double level) {
        super(name, salary, day, level);
//        this.bonus = bonus;
    }

    public double getBonus() {
        return bonus;
    }

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

    @Override
    public void printSalary() {
        System.out.println("经理" + getName() + "的工资=" + (bonus+getSalary()*getDay()*getLevel()));
    }
}
//分析出普通员工没有特有的属性
public class Worker extends Employee{
    public Worker(String name, Double salary, int day, double level) {
        super(name, salary, day, level);
    }

    @Override
    public void printSalary() {
        //System.out.println("普通员工" + getName()+ "的工资=" + (getSalary()*getDay()*1.0));
        System.out.print("普通");//自己的输出信息
        super.printSalary();//调用父类的方法
    }
}

5.作业5(重写)

package com.yt.homwork.homework08;

public class BankAccount {
    private double banlance;//余额

    public BankAccount(double initialBanlance) {
        this.banlance = initialBanlance;
    }

    //存款
    public void deposit(double amount){
        banlance += amount;
    }

    //取款
    public void withdraw(double amount){
        banlance -= amount;
    }

    public double getBanlance() {
        return banlance;
    }

    public void setBanlance(int banlance) {
        this.banlance = banlance;
    }
}

要求:
(1)在上面类的基础上扩展新类CheckingAccount对每次存款和取款都收取1美元的手续费;
(2)扩展前一个练习的BankAccount类,新类SavingsAccount每个月都有利息产生;
(earnMonthlylnterest方法被调用).并且有每月三次免手续费的存款或取款。在earnMonthlylnterest方法中重置交易计数;
(3)体会重写的好处;

package com.yt.homwork.homework08;

public class CheckingAccount extends BankAccount {
    private int serviceCharge= 1;//表示手续费,默认为1元


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

    //体会重写方法
    //只需要增加部分逻辑就能扩展父类方法
    @Override
    public void deposit(double amount) {
        super.deposit(amount - getServiceCharge());
    }

    @Override
    public void withdraw(double amount) {
        super.withdraw(amount + getServiceCharge());
    }

    public int getServiceCharge() {
        return serviceCharge;
    }

    public void setServiceCharge(int serviceCharge) {
        this.serviceCharge = serviceCharge;
    }
}
package com.yt.homwork.homework08;

/*
扩展前一个练习的BankAccount类,
新类SavingsAccount每个月都有利息产生(earnMonthlyInterest方法被调用),
并且有每月三次免手续费的存款或取款。在earnMonthlyInterest方法中重置交易计数
 */
public class SavingAccount extends BankAccount{
    private int acount=3;
    private double rate = 0.01;//利率

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

    public void earnMonthlyInterest(){
        //每个月初统计上个月的利息,将acount重新置为3
        acount = 3;
        super.deposit(getBanlance()*rate);
    }

    //体会重写的方法
    //只需要增加条件逻辑就可以对父类的方法进行扩展
    @Override
    public void deposit(double amount) {
        if (acount > 0) {//小于3次,免手续费
            super.deposit(amount);
        } else {//大于3次,收手续费,手续费存入银行
            super.deposit(amount - 1);
        }
        acount--;
    }

    @Override
    public void withdraw(double amount) {
        if (amount > 0){
            super.withdraw(amount);
        } else {
            super.withdraw(amount+1);
        }
        acount--;
    }

    public int getAcount() {
        return acount;
    }

    public void setAcount(int acount) {
        this.acount = acount;
    }

    public double getRate() {
        return rate;
    }

    public void setRate(double rate) {
        this.rate = rate;
    }
}

6.作业6(重写equals)

编写Doctor类{name, age, job, gender, sal}相应的getter()和setter()方法,5个参数的构造器,重写父类(Object)的equals()方法:public boolean equals(Object obj),

并判断测试类中创建的两个对象是否相等。相等就是判断属性是否相同.

package com.yt.homwork.homework10;

import java.util.Objects;

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;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        //过关方法
        if (!(o instanceof Doctor)){
            return false;
        }
        //向下转型
        Doctor doctor = (Doctor) o;
        return this.name.equals(doctor.name) && this.age == doctor.age
                && this.job.equals(doctor.job) && this.gender == doctor.gender
                && this.sal==doctor.sal;

    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age, job, gender, 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;
    }
}

7.== 和equals区别

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

8.作业8(封装、继承、多态)

1664276961040

package com.yt.homwork.homework13;

public class Homework13 {
    public static void main(String[] args) {
        Teacher teacher = new Teacher("张飞", '男', 30, 5);
       teacher.info();
        System.out.println("==============");

        Student student = new Student("小明", '男', 15, "00023102");
        student.info();

        System.out.println("================");

        Person[] person = new Person[4];
        person[0] = new Student("zhangsan",'m',16, "001");
        person[1] = new Student("lisi",'w',18, "002");
        person[2] = new Teacher("wanglaoshi ",'m',30, 5);
        person[3] = new Teacher("李教授 ",'w',60, 20);

        System.out.println("排序前~");
        for (int i=0; i<person.length; i++) {
//            System.out.println(people[i].toString());
            System.out.println(person[i]);//默认会调用重写之后的toString()方法
        }
        sort(person);
        System.out.println("排序后~");
        for (int i=0; i<person.length; i++) {
            System.out.println(person[i].toString());
        }

        System.out.println("======");
        //遍历多态数组
        for (int i=0; i< person.length; i++) {
            fuc(person[i]);
        }

    }

    public static void sort(Person[] people){
        Person temp = null;
        boolean flag = false;
        for (int i=0; i<people.length-1; i++){//外层循环
//            System.out.println(people[i].toString());
            for (int j=0; j< people.length - 1 - i; j++){//内层循环
                //按年龄从大到小排序
                if (people[j].getAge() < people[j+1].getAge()){
                    flag = true;
                    temp = people[j];
                    people[j] = people[j+1];
                    people[j+1] = temp;
                }
            }
            // 注意:标志位的判断要在外层循环中
            if (!flag){
                break;
            } else {
                flag = false;
            }
        }
    }

    // 定义方法,形参为Person类型,功能:调用学生的study或教师的teach
    //需要用到向下转型,和类型判断
    public static void fuc(Person person){
        if (person instanceof Student){
            Student student = (Student) person;
            student.study();
        } else if (person instanceof Teacher) {
            Teacher teacher = (Teacher) person;
            teacher.teach();
        } else {
            System.out.println("nothing");
        }
    }

}
package com.yt.homwork.homework13;

/*
(3)抽取一个父类Person类,将共同属性和方法放到Person类
 */
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 play() {
        return name;
    }

    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;
    }

    @Override
    public String toString() {
        return  "name=" + name + '\n' +
                "sex=" + sex +
                "\nage=" + age;
    }

}
package com.yt.homwork.homework13;

/*
写一个Teacher类,
Teacher类有名称(name),性别(sex),年龄(age),工龄(work_age),
做合理封装,通过构造器在创建对象时将4个属性赋值。
 */
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 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 super.toString() +
                "\nwork_age=" + work_age;
    }

    public void info(){
        System.out.println("老师的信息:");
        System.out.println(toString());
        teach();
        System.out.println(play());
    }
}
package com.yt.homwork.homework13;

/*
做一个Student类,
Student类有名称(name),性别(sex),年龄(age),学号(stu_id),
做合理封装,通过构造器在创建对象时将4个属性赋值。
 */
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;
    }

    public void study() {
        System.out.println("我承诺,我会好好学习");
    }

    @Override
    public String play() {
        return super.play() + "爱玩足球";
    }

    @Override
    public String toString() {
        return super.toString() +
                "\nstu_id=" + stu_id ;
    }

    public void info(){
        System.out.println("学生的信息:");
        System.out.println(toString());
        study();
        System.out.println(play());
    }
}

9.什么是多态?

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

多态具体体现

1.方法多态

(1)重载体现多态

(2)重写体现多态

2.对象多态
(1)对象的编译类型和运行类型可以不一致,编译类型在定义时,就确定,不能变化

(2)对象的运行类型是可以变化的,可以通过getClasss()来查看运行类型;

编译类型看定义时=号的左边,运行类型看=号右边

10.java的动态绑定机制是什么?

  1. 当调用对象的方法时,该方法会和对象的内存地址/运行类型绑定;

  2. 当调用对象的属性时,没有动态绑定机制,哪里声明,那里使用。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值