Java笔记015-面向对象编程(中级部分)章节练习

目录

面向对象编程(中级部分)章节练习

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

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

3、编写老师类

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

5、设计父类一员工类。子类:工人类(Worker),农民类(Peasant),教师类(Teacher),科学家类(Scientist),服务生类(Waiter)。

6、假定Grand 、Father和 Son在同一个包,问:父类和子类中通过this和super都可以调用哪些属性和方法

8、扩展如下的BankAccount类

9、设计一个Point类,其x和y坐标可以通过构造器提供。

10、编写Doctor类 {name, age, job, gender, sal}

11、现有Person类,里面有方法run、eat,Student类继承了Person类,并重写了run方法,自定义了study方法,试写出对象向上转型和向下转型的代码,并写出各自都可以调用哪些方法,并写出方法输出什么?

12、说出==和equals的区别(简答)

13、打印效果如下

案例题目描述:

14、程序阅读题 在main方法中执行:C c = new C(); 输出什么内容

15、什么是多态,多态具体体现有哪些?(可举例说明)

16、java的动态绑定机制是什么?


面向对象编程(中级部分)章节练习

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

package com.homework;

//定义一个Person类{name, age, job},初始化Person对象数组,
//有3个person对象,并按照age从大到小进行排序,提示,使用冒泡排序
public class Homework01 {
    public static void main(String[] args) {
        Person[] people = new Person[3];
        people[0] = new Person("jack", 55, "网络工程师");
        people[1] = new Person("tom", 30, "java工程师");
        people[2] = new Person("mary", 45, "算法工程师");

        for (int i = 0; i < people.length; i++) {
            System.out.println(people[i]);
        }

        Person tmp = null;
        for (int i = 0; i < people.length - 1; i++) {
            for (int j = 0; j < people.length - 1 - i; j++) {
                if (people[j].getAge() < people[j + 1].getAge()) {
                    tmp = people[j];
                    people[j] = people[j + 1];
                    people[j + 1] = tmp;
                }
            }
        }
        System.out.println("排序后的效果");
        for (int i = 0; i < people.length; i++) {
            System.out.println(people[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 + '\'' +
                '}';
    }

    public void AgeSort() {

    }
}

运行结果

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

4种访问修饰符的访问范围
访问级别访问控制修饰符同类同包子类不同包
公开public
受保护protected×
默认没有修饰符××
私有private×××

3、编写老师类

(1)要求有属性"姓名name","年龄age","职称post","基本工资salary"

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

(3)编写教师类的三个子类:教授类(Professor)、副教授类、讲师类。
工资级别分别为:教授为1.3、副教授为1.2、讲师类1.1。
在三个子类里面都重写父类的introduce ()方法。

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

package com.homework;

//(1)要求有属性"姓名name","年龄age","职称post","基本工资salary"
//(2)编写业务方法,introduce(),实现输出一个教师的信息。
//(3)编写教师类的三个子类:教授类(Professor)、副教授类、讲师类。
//   工资级别分别为:教授为1.3、副教授为1.2、讲师类1.1。
//   在三个子类里面都重写父类的introduce ()方法。
//(4)定义并初始化一个老师对象,调用业务方法,实现对象基本信息的后台打印
public class Homework03 {
    public static void main(String[] args) {
        Professor professor = new Professor("tom", 26, "教授", 7300.00, 1.3);
        Associate_Professor associate_professor = new Associate_Professor("jack", 46, "副教授", 6400.00, 1.2);
        Lecturer lecturer = new Lecturer("甲柒", 18, "讲师", 4800.00, 1.1);

        System.out.println(professor.toString());
        System.out.println(associate_professor.toString());
        System.out.println(lecturer.toString());
    }
}

class Introduce {
    private String name;
    private int age;
    private String post;
    private double salary;
    private double salary_grade;

    public Introduce(String name, int age, String post, double salary, double salary_grade) {
        this.name = name;
        this.age = age;
        this.post = post;
        this.salary = salary;
        this.salary_grade = salary_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 getSalary_grade() {
        return salary_grade;
    }

    public void setSalary_grade(double salary_grade) {
        this.salary_grade = salary_grade;
    }

    @Override
    public String toString() {
        return "Introduce{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", post='" + post + '\'' +
                ", salary=" + salary +
                ", salary_grade=" + salary_grade +
                '}';
    }
}

class Professor extends Introduce {
    public Professor(String name, int age, String post, double salary, double salary_grade) {
        super(name, age, post, salary, salary_grade);
    }

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

class Associate_Professor extends Introduce {
    public Associate_Professor(String name, int age, String post, double salary, double salary_grade) {
        super(name, age, post, salary, salary_grade);
    }

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

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

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

运行结果

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

父类:员工类

子类:部门经理类、普通员工类

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

(2)普通员工工资=单日工资*天数*等级(1.0) ;

(3)员工属性:姓名,单日工资,工作天数

(4)员工方法((打印工资)

(5)普遍员工及部门经理都是员工子类,需要重写打印工资方法。

(5)定义并初始化普通员工对象,调用打印工资方法输出工资,定义并初始化部门经理对象,调用打印工资方法输出工资

package com.homework;

public class Homework04 {
    public static void main(String[] args) {
        department_manager manager = new department_manager("甲柒", 300, 120, 1.2);
        manager.print_salary();
        ordinary_employee employee = new ordinary_employee("海绵宝宝", 100, 30, 1.0);
        employee.print_salary();
    }
}

class employee {
    private String name;
    private double salary_day;
    private int work_days;
    private double grade;

    public employee(String name, double salary_day, int work_days, double grade) {
        this.name = name;
        this.salary_day = salary_day;
        this.work_days = work_days;
        this.grade = grade;
    }

    public String getName() {
        return name;
    }

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

    public double getSalary_day() {
        return salary_day;
    }

    public void setSalary_day(double salary_day) {
        this.salary_day = salary_day;
    }

    public int getWork_days() {
        return work_days;
    }

    public void setWork_days(int work_days) {
        this.work_days = work_days;
    }

    public double getGrade() {
        return grade;
    }

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

    public void print_salary() {
        System.out.println(name + " 工资=" + (salary_day * work_days * grade));
    }
}

class department_manager extends employee {
    private double bonus;

    public department_manager(String name, double salary_day, int work_days, double grade) {
        super(name, salary_day, work_days, grade);
    }

    public double getBonus() {
        return bonus;
    }

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

    @Override
    public void print_salary() {
        System.out.println("经理 " + getName() + " 工资=" + (bonus + getSalary_day() * getWork_days() * getGrade()));
    }
}

class ordinary_employee extends employee {
    public ordinary_employee(String name, double salary_day, int work_days, double grade) {
        super(name, salary_day, work_days, grade);
    }

    @Override
    public void print_salary() {
        System.out.print("普通员工 ");
        super.print_salary();
    }
}

运行结果

5、设计父类一员工类。子类:工人类(Worker),农民类(Peasant),教师类(Teacher),科学家类(Scientist),服务生类(Waiter)。

(1)其中工人,农民服务生只有基本工资

(2)教师除基本工资外,还有课酬(元/天)

(3)科学家除基本工资外,还有年终奖

(4)编写一个测试类,将各种类型的员工的全年工资打印出来

package com.homework;

public class Homework05 {
    public static void main(String[] args) {
        Worker worker = new Worker("jack", 4000);
        worker.print_salary();

        Peasant peasant = new Peasant("smith", 4500);
        peasant.print_salary();

        Teacher teacher = new Teacher("tom", 3000);
        teacher.setClass_days(210);
        teacher.setClass_salary(400);
        teacher.print_salary();

        Scientist scientist = new Scientist("爱因斯坦", 30000);
        scientist.setBonus(60000);
        scientist.print_salary();

        Waiter waiter = new Waiter("jerry", 4500);
        waiter.print_salary();
    }
}

class Staff {
    private String name;
    private double salary;
    private int salary_month = 12;

    public Staff(String name, double salary) {
        this.name = name;
        this.salary = salary;
    }

    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 getSalary_month() {
        return salary_month;
    }

    public void setSalary_month(int salary_month) {
        this.salary_month = salary_month;
    }

    public void print_salary() {
        System.out.println(name + " 年工资=" + (salary * salary_month));
    }
}

class Worker extends Staff {
    public Worker(String name, double salary) {
        super(name, salary);
    }

    @Override
    public void print_salary() {
        System.out.print("工人 ");
        super.print_salary();
    }
}

class Peasant extends Staff {
    public Peasant(String name, double salary) {
        super(name, salary);
    }

    @Override
    public void print_salary() {
        System.out.print("农民 ");
        super.print_salary();
    }
}

class Teacher extends Staff {
    private int class_days;
    private double class_salary;

    public Teacher(String name, double salary) {
        super(name, salary);
    }

    public int getClass_days() {
        return class_days;
    }

    public void setClass_days(int class_days) {
        this.class_days = class_days;
    }

    public double getClass_salary() {
        return class_salary;
    }

    public void setClass_salary(double class_salary) {
        this.class_salary = class_salary;
    }

    @Override
    public void print_salary() {
        System.out.print("教师 ");
        System.out.println(getName() + " 年工资=" + (getSalary() * getSalary_month() + class_salary * class_days));
    }
}

class Scientist extends Staff {
    private double bonus;

    public Scientist(String name, double salary) {
        super(name, salary);
    }

    public double getBonus() {
        return bonus;
    }

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

    @Override
    public void print_salary() {
        System.out.print("科学家 ");
        System.out.println(getName() + " 年工资=" + (bonus + getSalary() * getSalary_month()));
    }
}

class Waiter extends Staff {
    public Waiter(String name, double salary) {
        super(name, salary);
    }

    @Override
    public void print_salary() {
        System.out.print("服务生 ");
        super.print_salary();
    }
}

运行结果

6、假定Grand 、Father和 Son在同一个包,问:父类和子类中通过this和super都可以调用哪些属性和方法

package com.homework;

public class Homework06 {
}

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可以访问哪些成员(属性和方法)?
        super.name;
        super.g1();
        //this可以访问哪些成员?
        this.id;
        this.score;
        this.f1();
        this.name;
        this.g1();
    }
}

class Son extends Father { //子类
    String name = "BB";

    @Override
    public void g1() {

    }

    private void show() {
        //super可以访问哪些成员(属性和方法)?
        super.id;
        super.f1();
        super.name;
        super.g1();
        //this可以访问哪些成员?
        this.name;
        this.g1();
        this.show();
        this.id;
        this.f1();
    }
}

7、写出程序结果

package com.homework;

public class Homework07 {
}

class Test {//父类
    String name = "Rose";

    Test() {
        System.out.println("Test");//(1)
    }

    Test(String name) {//name john
        this.name = name;//这里把父类的name 修改john

    }
}

class Demo extends Test {//子类
    String name = "Jack";

    Demo() {
        super();
        System.out.println("Demo");//(2)
    }

    Demo(String s) {
        super(s);
    }

    public static void main(String[] args) {
        //分析
        //1.new Demo()
        new Demo().test();//匿名对象
        new Demo("john").test();//匿名
    }

    public void test() {
        System.out.println(super.name);//(3) Rose (5) john
        System.out.println(this.name);//(4) jack (6) jack
    }
}

运行结果

8、扩展如下的BankAccount类

要求:

(1)在上面类的基础上扩展新类CheckingAccount对每次存款和取款都收取1美元的手续费

(2)扩展前一个练习的BankAccount类,新类SavingsAccount每个月都有利息产生
(earnMonthlyInterest方法被调用),并且有每月三次免手续费的存款或取款。在earnMonthlyInterest方法中重置交易计数

(3)体会重写的好处

package com.homework;

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());

        //测试SavingsAccount
        SavingsAccount savingsAccount = new SavingsAccount(1000);
        savingsAccount.deposit(100);
        savingsAccount.deposit(100);
        savingsAccount.deposit(100);
        System.out.println(savingsAccount.getBalance());//1300
        savingsAccount.deposit(100);
        System.out.println(savingsAccount.getBalance());//1399

        //月初,定时器自动调用一下 earnMonthlyInterest
        savingsAccount.earnMonthlyInterest();//统计利息
        System.out.println(savingsAccount.getBalance());//1399 + 13.99 = 1412.99
        savingsAccount.withdraw(100);//免手续费
        System.out.println(savingsAccount.getBalance());//1412.99 - 100 = 1312.99
        savingsAccount.withdraw(100);//免手续费
        savingsAccount.withdraw(100);//免手续费
        System.out.println(savingsAccount.getBalance());//1312.99 - 200 = 1112.99
        savingsAccount.deposit(100);//扣手续费
        System.out.println(savingsAccount.getBalance());//1112.99 + 100 - 1 =1211.99
    }
}

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;
    }
//set和getBalance方法....

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }
}

class CheckingAccount extends BankAccount {
    public CheckingAccount(double initialBalance) {
        super(initialBalance);
    }

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

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

class SavingsAccount extends BankAccount {
    private int count = 3;
    private double rate = 0.01;//利率

    public SavingsAccount(double initialBalance) {
        super(initialBalance);
    }

    public void earnMonthlyInterest() {//每个月初,我们统计上个月的利息,同时将count=3
        count = 3;
        super.deposit(getBalance() * rate);
    }

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

    @Override
    public void deposit(double amount) {//存款
        //判断是否还可以免手续费
        if (count > 0) {
            super.deposit(amount);
        } else {
            super.deposit(amount - 1);//1块转入银行
        }
        count--;//减去1次
    }

    @Override
    public void withdraw(double amount) {//取款
        //判断是否还可以免手续费
        if (count > 0) {
            super.withdraw(amount);
        } else {
            super.withdraw(amount + 1);//1块转入银行
        }
        count--;//减去1次
    }
}

运行结果

9、设计一个Point类,其x和y坐标可以通过构造器提供。

提供一个子类LabeledPoint,其构造器接受一个标签值和x,y坐标,比如:new LabeledPoint("Black”,1929,230.07),写出对应的构造器即可

package com.homework;

public class Homework09 {
    public static void main(String[] args) {
        LabeledPoint labeledPoint = new LabeledPoint("black", 1929, 230.07);
    }
}

class Point {
    private double x;
    private double y;

    public Point(double x, double y) {
        this.x = x;
        this.y = y;
    }
}

class LabeledPoint extends Point {
    private String label;

    public LabeledPoint(String label, double x, double y) {
        super(x, y);
        this.label = label;
    }

}

10、编写Doctor类 {name, age, job, gender, sal}

相应的getter()和setter()方法,5个参数的构造器,
重写父类的equals()方法:publicboolean equals(Object obj),
并判断测试类中创建的两个对象是否相等。
相等就是判断属性是否相同

package com.homework;

public class Homework10 {
    public static void main(String[] args) {
        Doctor doctor1 = new Doctor("甲柒", 18, "牙科主任", '男', 36000);
        Doctor doctor2 = new Doctor("甲柒", 18, "牙科主任", '男', 36000);
        System.out.println(doctor1.equals(doctor2));//true"牙科主任", '男', 36000);
        
        Doctor doctor3 = new Doctor("甲柒", 18, "牙科主任", '男', 36000);
        Doctor doctor4 = new Doctor("柒", 18, "牙医", '男', 36000);
        System.out.println(doctor3.equals(doctor4));//false
    }
}

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

    @Override
    public boolean equals(Object obj) {
        //判断两个比较的对象是否相同
        if (this == obj) {
            return true;
        }
        //判断obj是不是Doctor类型或其子类
        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;
    }
}

运行结果

11、现有Person类,里面有方法run、eat,Student类继承了Person类,并重写了run方法,自定义了study方法,试写出对象向上转型和向下转型的代码,并写出各自都可以调用哪些方法,并写出方法输出什么?

package com.homework;

public class Homework11 {
    public static void main(String[] args) {
        //向上转型:父类的引用指向子类对象
        Person11 p = new Student();
        p.run();//student run
        p.eat();//Person11 eat
        //向下转型:把指向子类对象的父类引用,转成指向子类对象的子类引用
        Student s = (Student) p;
        s.run();//student run
        s.study();//student study
        s.eat();//Person11 eat
    }
}

class Person11 {//父类

    public void run() {
        System.out.println("person run");
    }

    public void eat() {
        System.out.println("person eat");
    }
}

class Student extends Person11 {//子类

    public void run() {
        System.out.println("student run");
    }

    public void study() {
        System.out.println("student study..");
    }
}

12、说出==和equals的区别(简答)

13、打印效果如下

案例题目描述:

(1) 做一个Student类,Student类有名称(name),性别(sex),年龄(age),学号(stu_id),做合理封装,通过构造器在创建对象时将4个属性赋值。

(2) 写一个Teacher类,Teacher类有名称(name),性别(sex),年龄(age),工龄(work_age),做合理封装,通过构造器在创建对象时将4个属性赋值。

(3) 抽取一个父类Person类,将共同属性和方法放到Person类

(4) 学生需要有学习的方法(study),在方法里写生"我承诺,我会好好学习。"。

(5) 教师需要有教学的方法(teach),在方法里写上"我承诺,我会认真教学。"。

(6) 学生和教师都有玩的方法(play),学生玩的是足球,老师玩的是象棋,此方法是返回字符串的,分别返回"xx爱玩足球"和"xx爱玩象棋"(其中xx分别代表学生和老师的姓名)。因为玩的方法名称都一样,所以要求此方法定义在父类中,子类实现重写。

(7) 定义多态数组,里面保存2个学生和2个教师,要求按年龄从高到低排序,

(8) 定义方法,形参为Person类型,功能:调用学生的study或教师的teach方法

package com.homework.homework13;

// 抽取一个父类Person类,将共同属性和方法放到Person类
public class Person {
    private String name;
    private char gender;
    private int age;

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

    public String getName() {
        return name;
    }

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

    public char getGender() {
        return gender;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

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

    public String basicInfo() {
        return "姓名:" + name + "\n年龄:" + age + "\n性别:" + gender;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", gender=" + gender +
                ", age=" + age +
                '}';
    }
}
package com.homework.homework13;

// 做一个Student类,Student类有名称(name),性别(sex),年龄(age),学号(stu_id),
// 做合理封装,通过构造器在创建对象时将4个属性赋值。
// 学生需要有学习的方法(study),在方法里写生"我承诺,我会好好学习。"
public class Student extends Person {
    private String stu_id;

    public Student(String name, char gender, int age, String stu_id) {
        super(name, gender, 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(getName() + " 承诺,我会好好学习java~~");
    }

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

    //编写一个输出信息的方法,这样体现封装
    public void printInfo() {
        System.out.println("学生信息:");
        System.out.println(super.basicInfo());
        System.out.println("学号:" + stu_id);
        study();
        System.out.println(play());
    }

    @Override
    public String toString() {
        return "Student{" +
                "stu_id='" + stu_id + '\'' +
                '}' + super.toString();
    }
}
package com.homework.homework13;

// 写一个Teacher类,Teacher类有名称(name),性别(sex),年龄(age),工龄(work_age),
// 做合理封装,通过构造器在创建对象时将4个属性赋值。
//教师需要有教学的方法(teach),在方法里写上"我承诺,我会认真教学。"
public class Teacher extends Person {
    private int work_age;

    public Teacher(String name, char gender, int age, int work_age) {
        super(name, gender, 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(getName() + " 承诺,我会认真教学java...");
    }

    @Override
    public 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());
    }

    @Override
    public String toString() {
        return "Teacher{" +
                "work_age=" + work_age +
                '}' + super.toString();
    }
}
package com.homework.homework13;

public class Homework13 {
    public static void main(String[] args) {
        //测试教师
        Teacher teacher = new Teacher("牛顿", '男', 39, 16);
        teacher.printInfo();
        System.out.println("===========================");
        //测试学生
        Student student = new Student("甲柒", '男', 19, "0217");
        student.printInfo();//封装

        //定义多态数组,里面保存2个学生和2个教师,要求按年龄从高到低排序
        Person[] people = new Person[4];
        people[0] = new Student("jack", '男', 13, "0001");
        people[1] = new Student("marry", '女', 16, "0002");
        people[2] = new Teacher("smith", '男', 36, 5);
        people[3] = new Teacher("scott", '男', 26, 1);

        //创建对象
        Homework13 homework13 = new Homework13();
        homework13.bubbleSort(people);

        //输出排序后的数组
        System.out.println("-----------------排序后的数组------------------");
        for (int i = 0; i < people.length; i++) {
            System.out.println(people[i]);
        }

        //遍历数组,调用test方法
        System.out.println("============================================");
        for (int i = 0; i < people.length; i++) {//遍历多态数组
            homework13.test(people[i]);
        }

    }
    //定义方法,形参为Person类型,功能:调用学生的study或教师的teach方法
    //分析这里会使用到向下转型和类型判断
    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 - 1; 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;
                }

            }
        }
    }
}

运行结果

14、程序阅读题 在main方法中执行:C c = new C(); 输出什么内容

package com.homework;

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类的有参参构造");
    }
}

运行结果

15、什么是多态,多态具体体现有哪些?(可举例说明)

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

1、方法多态

(1)重载体现多态
(2)重写体现多态

2、对象多态

(1)对象的编译类型和运行类型可以不一致,编译类型在定义时,就确定,不能变化
(2)对象的运行类型是可以变化的,可以通过getClasss()来查看运行类型
(3)编译类型看定时时=号的左边,运行类型看=号右边

3、举例说明

package com.homework;

public class Homework15 {
    public static void main(String[] args) {
        AAA obj = new BBB();//向上转型
        AAA b1=obj;
        System.out.println("obj的运行类型=" + obj.getClass());//BBB
        obj = new CCC();//向下转型
        System.out.println("obj的运行类型=" + obj.getClass());//CCC
        obj=b1;
        System.out.println("obj的运行类型=" + obj.getClass());//BBB
    }
}

class AAA {//超类
}

class BBB extends AAA {//父类
}

class CCC extends BBB {//子类
}

运行结果

16、java的动态绑定机制是什么?

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

甲柒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值