概述
目录:
- 题目1
- 题目2
- 题目3
- 题目4
- 题目5
- 题目6
- 题目7
- 题目8
- 题目9
- 题目10
- 题目11
- 题目12
- 题目13
- 题目14
- 题目15
- 题目16
题目1
1.定义一个Person类 {name, age, job} ,初始化Person对象数组,有3个person对象,并安照 age从大到小进行排序,提示,使用冒泡排序。Homework01.java
public class Homework01 {
public static void main(String[] args) {
//初始化Person 对象数组,有3个person对象
Person[] persons = new Person[3];
persons[0] = new Person("mary",30, "PHP工程师");
persons[1] = new Person("tom",50, "大数据工程师");
persons[2] = new Person("smith",10, "JavaEE工程师");
//输出当前对象数组
for (int i = 0; i < persons.length; i++) {
System.out.println(persons[i]);//不重写就是默认对象的.toString() ,后边要重写toString
}
//使用冒泡排序
Person tmp = null;//临时变量,用于交换
for(int i = 0; i < persons.length -1 ;i++) {//外层循环
for(int j = 0; j < persons.length -1 - i; j++) {//内层循环
//并按照 age 从 大到 小进行排序, 如果前面的人的age < 后面人的年龄,就交换
// 要求按照名字的长度从小到大 if(persons[i].getName().length() > persons[i+1].getName().length())
if(persons[j].getAge() > persons[j+1].getAge()) {
tmp = persons[j];
persons[j] = persons[j+1];
persons[j+1]= tmp;
}
}
}
System.out.println("排序后的效果");
for (int i = 0; i < persons.length; i++) {
System.out.println(persons[i]);//默认对象的.toString()
}
}
/*定义一个Person类 {name, age, job}, 初始化Person 对象数组,有3个person对象,
并按照 age 从 大到 小进行排序, 提示,使用冒泡排序 */
}
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 //重写toString
public String toString() {
return "Person{" +
"name='" + name + ''' +
", age=" + age +
", job='" + job + ''' +
'}';
}
}
题目2
- 写出四种访问修饰符和各自的访问权限
题目3
- 编写老师类 HomeworkO3.java
(1)要求有属性 “姓名name”,“年龄age”,“职称post”,“基本工资“salary”
(2)编写业务方法,introduce (),实现输出一个教师的信息。
(3)编写教师类的三个子类:教授类(Professor)、副教授类、讲师类。工资级别分别为:教受为1.3、副教授为1.2、讲师类1.1。在三个子类里面都重写父类的introduce()方法。
(4)定义并初始化一个老师对象,调用业务方法,实现对象基本信息的后台打印。
方法1:把grade等级写在父类Teacher里
主类代码:
public class Homework03 {
public static void main(String[] args) {
Professor professor = new Professor("贾宝玉", 30, "高级职称", 30000, 1.3);
professor.introduce();
}
}
/*
(1) 要求有属性“姓名name”,“年龄age”,“职称post”,“基本工资salary”
(2) 编写业务方法, 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);
}
}
子类Professor代码:
//子类
public class Professor extends Teacher {
//特有属性可以自己增加,这里没有就不写了...
public Professor(String name, int age, String post, double salary, double grade) {
super(name, age, post, salary, grade);
}
@Override
public void introduce() {
System.out.println(" 这是教授的信息 ");
super.introduce();
}
}
方法2:把grade等级写在每一个子类里
主类代码:
public class Homework03 {
public static void main(String[] args) {
Teacher professor = new Professor("java", 223, "教授", 2300,"s");
System.out.println(professor.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) {
this.name = name;
this.age = age;
this.post = post;
this.salary = salary;
// this.grade =grade;
}
public String introduce(){
return "教师信息为:"+ "姓名"+name+"年龄"+age+"职称"+post+"薪水"+salary;
}
// public void introduce() {
// System.out.println("name: " + name + " age: " + age
// + " post: " + post + " salary:" + salary + " 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;
}
@Override
public String toString() {
return "Teacher{" +
"name='" + name + ''' +
", age=" + age +
", post='" + post + ''' +
", salary=" + salary +
'}';
}
}
子类Professor代码:
public class Professor extends Teacher{
private String grade;
public Professor(String name, int age, String post, double salary, String grade) {
super(name, age, post, salary);
this.grade = grade;
}
@Override
public String introduce() {
System.out.println("教授信息为");
return super.introduce()+"级别为"+grade;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
@Override
public String toString() {
return "Professor{" +
"grade='" + grade + ''' +
'}';
}
}
题目4
- 通过继承实现员工工资核算打印功能 Homework04.java
父类:员工类(Employee)
子类:部门经理类(Manager)、普通员工类(Worker)
(1)部门经理工资=1000+单日工资* 天数* 等级(1.2)。=>奖金+基本工资
(2)普通员工工资=单日工资 * 天数 * 等级(1.0); =>基本工资
(3)员工属性:姓名,单日工资,工作天数
(4)员工方法(打印工资)
(5)普遍员工及部门经理都是员工子类,需要重写打印工资方法。
(6)定义并初始化普通员工对象,调用打印工资方法输出工资,定义并初始化部门经理对象,调用打印工资方法输出工资
方法1:创建Manager对象时,奖金是多少并不是确定的,因为在构造器中,不给bonus,可以在主类通过setBonus动态设置奖金。(我认为这一步无所谓,用方法二也一样。)
主类和父类代码:
public class Homework04 {
public static void main(String[] args) {
Manager manage = new Manager("刘备", 100, 20, 1.2);
//设置奖金
manage.setBonus(3000);
//打印经理的工资情况
manage.printSal();
Worker worker = new Worker("关羽",50, 10, 1.0);
worker.printSal();
}
}
//父类员工Employee
class Employee {
//属性
//员工属性:姓名,单日工资,工作天数
private String name;
private double daySal;
private int workDays;
//分析出还有一个属性等级
private double grade;
//方法[构造器,getter 和 setter]
//打印工资方法
//方法 void printSal() {}
public void printSal() {
System.out.println(name + " 工资=" + daySal * workDays * grade);
}
public Employee(String name, double daySal, int workDays, double grade) {
this.name = name;
this.daySal = daySal;
this.workDays = workDays;
this.grade = grade;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getDaySal() {
return daySal;
}
public void setDaySal(double daySal) {
this.daySal = daySal;
}
public int getWorkDays() {
return workDays;
}
public void setWorkDays(int workDays) {
this.workDays = workDays;
}
public double getGrade() {
return grade;
}
public void setGrade(double grade) {
this.grade = grade;
}
}
子类经理Manager代码:
public class Manager extends Employee {
//特有属性
private double bonus;
//创建Manager对象时,奖金是多少并不是确定的,因为老师在构造器中,不给bonus,可以通过setBonus
public Manager(String name, double daySal, int workDays, double grade) {
super(name, daySal, workDays, grade);
}
//方法:重写父类的 printSal
@Override
public void printSal() {
//因为经理的工资计算方式和Employee不一样,所以我们重写
System.out.println("经理 " + getName() + " 工资是="
+ (bonus + getDaySal() * getWorkDays() * getGrade()));
}
public double getBonus() {
return bonus;
}
public void setBonus(double bonus) {
this.bonus = bonus;
}
}
子类普通员工Worker代码:
public class Worker extends Employee{
//分析普通员工没有特有的属性
public Worker(String name, double daySal, int workDays, double grade) {
super(name, daySal, workDays, grade);
}
//重写printSal
//因为普通员工和Employee输出工资情况一下,所以直接调用父类的printSal()
@Override
public void printSal() {
System.out.print("普通员工 ");//自己的输出信息
super.printSal();//调用父类的方法,复用代码
}
}
方法2:创建Manager对象时,在构造器中给bonus。
主类和父类代码:
public class Homework04 {
public static void main(String[] args) {
Manager s = new Manager("大猛子", 100, 20, 1.2, 1000);
s.printSal();
Worker worker = new Worker("小猛子", 80, 30, 1.0);
worker.printSal();
}
}
class Employee {
private String name;
private double daySal;
private int workDays;
private double grade;
public void printSal() {
System.out.println(name + "的工资=" + daySal * workDays * grade);
}
public Employee(String name, double daySal, int workDays, double grade) {
this.name = name;
this.daySal = daySal;
this.workDays = workDays;
this.grade = grade;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getDaySal() {
return daySal;
}
public void setDaySal(double daySal) {
this.daySal = daySal;
}
public int getWorkDays() {
return workDays;
}
public void setWorkDays(int workDays) {
this.workDays = workDays;
}
public double getGrade() {
return grade;
}
public void setGrade(double grade) {
this.grade = grade;
}
}
子类经理Manager代码
public class Manager extends Employee{
private double bonus;
@Override
public void printSal() {
//super.printSal();
System.out.println("经理 "+getName()+" 工资为:"+(bonus + getDaySal()*getWorkDays()*getGrade()));
}
public Manager(String name, double daySal, int workDays, double grade, double bonus) {
super(name, daySal, workDays, grade);
this.bonus = bonus;
}
public double getBonus() {
return bonus;
}
public void setBonus(double bonus) {
this.bonus = bonus;
}
}
子类普通员工Worker代码:
public class Worker extends Employee {
public Worker(String name, double daySal, int workDays, double grade) {
super(name, daySal, workDays, grade);
}
@Override
public void printSal() {
System.out.print("普通员工 ");
super.printSal();
}
}
题目5
- 设计父类一员工类。子类:工人类(Worker),农民类(Peasant),教师类(Teacher),科学家类(Scientist).服务生类(Waiter)。
(1)其中工人,农民,服务生只有基本工资sal
(2)教师除基本工资外还有课酬(元/天) classDay, classSal
(3)科学家除基本工资外,还有年终奖bonus
(4)编写一个测试类,将各种类型的员工的全年工资打印出来
HomeworkO5.java 放在com.hspedu.homework.homework5包
主类代码:
public class Homework05 {
public static void main(String[] args) {
Worker jack = new Worker("jack", 10000);
jack.setSalMonth(15);//灵活修改带薪月份
jack.printSal();
Peasant smith = new Peasant("smith", 20000);
smith.printSal();
//老师测试
Teacher teacher = new Teacher("顺平", 2000);
//老师有课时费
teacher.setClassDays(360);
teacher.setClassSal(1000);
teacher.printSal();
//科学家
Scientist scientist = new Scientist("钟南山", 20000);
scientist.setBonus(2000000);
scientist.printSal();
}
}
父类员工Employee代码:
注意:这里年薪有的发12个月,有的13个15个,变化设置成属性,然后不加入构造器,而是用get和set在主类设置。
public class Employee { //父类
//属性
//分析有一个带薪的月份 13 , 15, 12
private String name;
private double sal;
private int salMonth = 12;
//方法
//打印全年工资
public void printSal() {
System.out.println(name + " 年工资是: " + (sal * salMonth));
}
public Employee(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 int getSalMonth() {
return salMonth;
}
public void setSalMonth(int salMonth) {
this.salMonth = salMonth;
}
}
子类 农民工Peasant
public class Peasant extends Employee {//子类
//属性
//农民,服务生只有基本工资 sal
//方法
public Peasant(String name, double sal) {
super(name, sal);
}
//年工资
@Override
public void printSal() {
System.out.print("农民 ");
super.printSal();
}
}
子类 工人 worker 代码:
public class Worker extends Employee{ //子类
//属性
//工人,农民,服务生只有基本工资 sal
public Worker(String name, double sal) {
super(name, sal);
}
//方法
@Override
public void printSal() {
System.out.print("工人 ");
super.printSal();//使用父类的printSal()
}
}
子类Teacher代码:
注意:这里 classDays 和 classSal没有放入构造器,也是在主类用set方法设置。
public class Teacher extends Employee{//子类
//特有属性
private int //课时费
public Teacher(String name, double sal) {
super(name, sal);
}
//方法-重写printSal
@Override
public void printSal() { //老师不能使用super.printSal(),要重写下
System.out.print("老师 ");
System.out.println(getName() + " 年工资是: "
+ (getSal() * getSalMonth() + classDays * classSal ));
}
public int getClassDays() {
return classDays;
}
public void setClassDays(int classDays) {
this.classDays = classDays;
}
public double getClassSal() {
return classSal;
}
public void setClassSal(double classSal) {
this.classSal = classSal;
}
}
子类Scientist代码:
注意:这里 bonus 没有放入构造器,也是在主类用set方法设置。
public class Scientist extends Employee{ //子类
//特有属性
//年终奖 bonus
private double bonus;
//方法
public Scientist(String name, double sal) {
super(name, sal);
}
//重写年工资打印
@Override
public void printSal() {
System.out.print("科学家 ");
System.out.println(getName() + " 年工资是: " + (getSal() * getSalMonth() + bonus));
}
public double getBonus() {
return bonus;
}
public void setBonus(double bonus) {
this.bonus = bonus;
}
}
题目6
1、super访问父类。(不仅局限于父类,还可以往上一类)
2、this先从本类访问(本类所有的成员都可以访问),本类没有再去父类或者 超类,但是,this在本类访问了name,就不能再访问超类的name了。假如this在父类中访问了一个成员,就不能再去超类访问了。。
题目7
题目8
主类代码:
public class Homework08 {
public static void main(String[] args) {
// 第1小问测试checkingAccount
// CheckingAccount checkingAccount = new CheckingAccount(1000);
// checkingAccount.deposit(10);// 1010 - 1 = 1009
// checkingAccount.withdraw(9);//1009 - 9 = 1000 -1= 999
// 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());//1400-1=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());//1412.99 -200 =1112.99
savingsAccount.deposit(100);//扣手续费
System.out.println(savingsAccount.getBalance());//1112.99 + 100 = 1212.99 - 1 = 1211.99
}
}
子类checkingAccount代码:
/*
在上面类的基础上扩展 新类CheckingAccount对每次存款和取款都收取1美元的手续费
*/
public class CheckingAccount extends BankAccount{//新的账号
//属性
public CheckingAccount(double initialBalance) {
super(initialBalance);
}
@Override
public void deposit(double amount) {//存款
super.deposit(amount - 1);//巧妙的使用了父类的 deposit
//1 块钱转入银行的账号
}
@Override
public void withdraw(double amount) {//取款
super.withdraw(amount + 1);//比如说取10元,加上手续费就是要取11元
//1 块钱转入银行的账号
}
}
子类SavingsAccount代码:
/*
扩展前一个练习的BankAccount类,
新类SavingsAccount每个月都有利息产生(earnMonthlyInterest方法被调用),
并且有每月三次免手续费的存款或取款。在earnMonthlyInterest方法中重置交易计数
*/
public class SavingsAccount extends BankAccount {
//分析
//新增加属性
private int count = 3;
private double rate = 0.01;//利率
public void earnMonthlyInterest() {//每个月初,我们统计上个月的利息,同时将count=3
count = 3;
super.deposit(getBalance() * rate); //调用的是父类
}
@Override
public void deposit(double amount) {
//判断是否还可以免手续费
if(count > 0) {
super.deposit(amount);
} else {
super.deposit(amount - 1);//1 块转入银行
}
count--;//减去一次
}
@Override
public void withdraw(double amount) {//取款
//判断是否还可以免手续费
if(count > 0) {
super.withdraw(amount);
} else {
super.withdraw(amount + 1);//1 块转入银行
}
count--;
}
public SavingsAccount(double initialBalance) {
super(initialBalance);
}
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;
}
}
题目9
-
设计一个Point类,其x和y坐标可以通过构造器提供。提供一个子类LabeledPoint,其构造器接受一个标签值和x,y坐标,比如:new LabeledPoint("Black”,1929,230.07),写出对应的构造器即可。HomeworkO9.java
此题的用意在于:已知需求,反写构造器
public class Homework09 {
public static void main(String[] args) {
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个参数的构造器,重写父类(Object)的equals(方法:public boolean equals(Object obi),并判断测试类中创建的两个对象是否相等。相等就是判断属性是否相同。 Homework10.java
提示:此题可以参考学习笔记第八章(中)的 8.12.2 如何重写equals 方法
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));//T equals重写了,是判断属性是否相同。
}
}
class Doctor {
//属性
//{name, age, job, gender, sal}
private String name;
private int age;
private String job;
private char gender;
private double sal;
//5个参数的构造器
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;
}
//方法
//相应的getter()和setter()方法
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;
}
//重写父类(Object)的equals()方法:public boolean equals(Object obj),并判断测试类中创建的两个对象是否相等。相等就是判断属性是否相同
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
考察向上转型,向下转型。
可以参考文章文章https://blog.csdn.net/m0_50736744/article/details/121377193中的 8.11.5 多态注意事项和细节讨论-向上转型 和 向下转向 部分。
小总结:其实访问方法时,可以理解成 访问方法看运行类型(右边)-指输出结果。
可以参考文章https://blog.csdn.net/m0_50736744/article/details/121377193中的8.11.6 课堂练习的2
题目12
题目13
注意:本题7、8小问好好领会。(7、8小问需要重写toString方法)
主类代码:7,8小问放在了主类
public class Homework13 {
public static void main(String[] args) {
//测试老师
Teacher teacher = new Teacher("张飞", '男', 30, 5);
teacher.printInfo();
//测试
Student student = new Student("小明", '男', 15, "00023102");
System.out.println("-----------------------------------");
student.printInfo();//封装
//定义多态数组,里面保存2个学生和2个教师,要求按年龄从高到低排序
Person[] persons = new Person[4]; //多态数组用父类创建
persons[0] = new Student("学生jack", '男', 10, "0001");
persons[1] = new Student("学生mary", '女', 20, "0002");
persons[2] = new Teacher("smith", '男', 36, 5);
persons[3] = new Teacher("scott", '男', 26, 1);
//创建对象
Homework13 homework13 = new Homework13();
homework13.bubbleSort(persons);
//输出排序后的数组
System.out.println("---排序后的数组-----");
for(int i = 0; i < persons.length; i++) {
System.out.println(persons[i]);
}
//遍历数组,调用test方法
System.out.println("=======================");
for (int i = 0; i < persons.length; i++) {//遍历多态数组
homework13.test(persons[i]);
}
}
//定义方法,形参为Person类型,功能:调用学生的study或教师的teach方法
//分析这里会使用到向下转型和类型判断
public void test(Person p) {
if(p instanceof Student) {//p 的运行类型如果是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;
}
}
}
}
}
父类Person类:
/*抽取一个父类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;
}
//编写一个play 方法, 把共有的输出内容写到父类
public String play() {
return name + "爱玩";
}
//返回一个基本信息
/*
姓名:张飞
年龄:30
性别:男
*/
public String basicInfo() {
return "姓名: " + name + "n年龄: " + age + "n性别: " + gender;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + ''' +
", gender=" + gender +
", age=" + age +
'}';
}
}
子类Student类:
/*学生需要有学习的方法(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");
}
/**
* 学生爱玩足球
* @return
*/
@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();
}
}
子类Teacher类:
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;
}
//教师需要有教学的方法(teach),在方法里写上“我承诺,我会认真教学。
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();
}
}
题目14
注意:千万别忘了B类中隐藏的那个Super
答案:我是A类,hahah我是B类的有参构造,我是c类的有参参构造,我是c类的无参构造
题目15
- 什么是多态 , 多态具体体现有哪些? (可举例说明)
答案:
多态:方法或对象具有多种形态,是0OP的第三大特征,是建立在封装和继承基础之上多态具体体现
1.方法多态
(1)重载体现多态(2)重写体现多态
2.对象多态
(1)对象的编译类型和运行类型可以不一致,编译类型在定义时,就确定,不能变化
(2)对象的运行类型是可以变化的,可以通过getClasss ( ) 来查看运行类型
(3)编译类型看定义时=号的左边,运行类型看=号的右边
3.举例说明
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 {//子类
}
内存分析图顺序:黄–>红–>蓝
注意:这里obj的运行类型可以改变,但是编译类型不能改变,AAA obj就决定了obj的编译类型是AAA
题目16
-
java的动态绑定机制是什么? Homework16.java
答案:
1.当调用对象的方法时,该方法会和对象的内存地址/运行类型绑定。
2.当调用对象的属性时,没有动态绑定机制,哪里声明,那里使用。
最后
以上就是贪玩泥猴桃为你收集整理的java学习笔记(8) 第8章(下)- 面向对象编程(中级部分) - 练习题题目1题目2题目3题目4题目5题目6题目7题目8题目9题目10题目11题目12题目13题目14题目15题目16的全部内容,希望文章能够帮你解决java学习笔记(8) 第8章(下)- 面向对象编程(中级部分) - 练习题题目1题目2题目3题目4题目5题目6题目7题目8题目9题目10题目11题目12题目13题目14题目15题目16所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复