【韩顺平Java笔记】第8章:面向对象编程(中级部分)【343-353】
343. 本章作业02
写出四种访问修饰符和各自的访问权限
访问修饰符 | 本类 | 同包 | 子类 | 不同包 |
---|---|---|---|---|
public | √ | √ | √ | √ |
protected | √ | √ | √ | × |
默认 | √ | √ | ×(不同包的子类不能访问,同一个包的子类可以) | × |
private | √ | × | × | × |
344. 本章作业03
Teacher.java
package com.Marisa.homework03;
public 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.java
package com.Marisa.homework03;
//子类
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();
}
}
Homework03.java
package com.Marisa.homework03;
public class Homework03 {
public static void main(String[] args) {
Professor professor = new Professor("田所浩二", 24, "学生", 114514, 1.3);
professor.introduce();
}
}
运行结果:
这事教授的信息
name田所浩二 age24 post学生 salary:114514.0 grade1.3
345. 本章作业04
Employee.java
package com.Marisa.homework04;
public class Employee {
private String name;
private double daySal; //单日工资
private int workDays; //工作日数量
//分析出来还有一个属性等级
private double 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;
}
//打印工资方法
public void printSal(){
System.out.println(name + " 工资=" + daySal * workDays * grade);
}
}
Manager.java
package com.Marisa.homework04;
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);
}
public double getBonus() {
return bonus;
}
public void setBonus(double bonus) {
this.bonus = bonus;
}
//重写弗雷的printSal
@Override
public void printSal() {
//经理的工资计算方式和Employee不一样,所以我们重写
System.out.println("经理 " + getName() + " 工资=" + (bonus + getDaySal() * getWorkDays() * getGrade()));
}
}
Worker.java
package com.Marisa.homework04;
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();
}
}
Homework04.java
package com.Marisa.homework04;
public class Homework04 {
public static void main(String[] args) {
Manager manager = new Manager("田所浩二", 100, 20, 1.2);
//设置奖金
manager.setBonus(3000);
//打印经理工资情况
manager.printSal();
Worker worker = new Worker("我修院", 50, 10 , 1.0);
worker.printSal();
}
}
346. 本章作业05
Employee.java
package com.Marisa.homework05;
public class Employee {
//分析有一个带薪的月份
private String name;
private double sal;
private int salMonth = 12;//默认12薪
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;
}
//打印全年工资
public void printSal(){
System.out.println(name + " 年工资是:" + getSalMonth() * getSal());
}
}
Peasant.java
package com.Marisa.homework05;
public class Peasant extends Employee{
public Peasant(String name, double sal) {
super(name, sal);
}
//只有基本工资
@Override
public void printSal() {
System.out.print("农民 ");
super.printSal();
}
}
Scientist.java
package com.Marisa.homework05;
public class Scientist extends Employee{
//特有属性年终奖
private double bouns;
public Scientist(String name, double sal) {
super(name, sal);
}
public double getBouns() {
return bouns;
}
public void setBouns(double bouns) {
this.bouns = bouns;
}
@Override
public void printSal() {
System.out.print("科学家 ");
System.out.println(getName() + " 年工资是:"+ (getSal() * getSalMonth() + bouns));
}
}
Teacher.java
package com.Marisa.homework05;
public class Teacher extends Employee{
//特有属性
private int classDays;//一年上课天数
private double classSal;//课时费
//通过set get方法设置课时费
public Teacher(String name, double sal) {
super(name, sal);
}
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;
}
@Override
public void printSal() {
System.out.print("老师 ");
//老师不能使用super.printSal()
System.out.println(getName() + " 年工资是:" + (getSal()*getSalMonth() + classSal*classDays));
}
}
Waiter.java
package com.Marisa.homework05;
public class Waiter extends Employee{
public Waiter(String name, double sal) {
super(name, sal);
}
@Override
public void printSal() {
System.out.print("服务生 ");
super.printSal();
}
}
Worker.java
package com.Marisa.homework05;
public class Worker extends Employee{
//只有基本工资
public Worker(String name, double sal) {
super(name, sal);
}
@Override
public void printSal() {
System.out.print("工人 ");
super.printSal();
}
}
Homework05.java
package com.Marisa.homework05;
public class Homework05 {
public static void main(String[] args) {
Worker jack = new Worker("jack", 10000);
jack.setSalMonth(15);//15薪
jack.printSal();
Peasant smith = new Peasant("smith", 20000);
smith.printSal();
Teacher mary = new Teacher("mary", 2000);
//老师有课时费
mary.setClassDays(360);
mary.setClassSal(1000);
mary.printSal();
Scientist mike = new Scientist("mike", 20000);
mike.setBouns(2000000);
mike.printSal();
Waiter tom = new Waiter("tom", 114514);
tom.printSal();
}
}
运行结果:
工人 jack 年工资是:150000.0
农民 smith 年工资是:240000.0
老师 mary 年工资是:384000.0
科学家 mike 年工资是:2240000.0
服务生 tom 年工资是:1374168.0
347. 本章作业06
this先从本类查找,本类没有可以去调用父类(注意private)
super不仅仅限于它的父类,它父类的父类也能调用
348. 本章作业08
- Test是Demo的父类,所以new Demo().test()先执行Test的构造器,先是无参构造器输出一行Test
- 然后new Demo().test()继续执行本类的无参构造器输出Demo
- 然后new Demo().test()调用的是本类的test方法,输出两行,一行是Rose,一行是Jack
- new Demo(“john”).test(),调用的是有参构造器,然后调用父类有参构造器,此时super.name为john,this.name还是Jack
- new Demo(“john”).test(),输出一行john,一行Jack
349. 本章作业08
BankAccount.java
package com.Marisa.homework08;
public class BankAccount {
private double balance; //余额
public BankAccount(int 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;
}
}
CheckingAccount.java
package com.Marisa.homework08;
public class CheckingAccount extends BankAccount{
public CheckingAccount(int balance) {
super(balance);
}
@Override
public void deposit(double amount) {
super.deposit(amount - 1);//每次存款收取1美元手续费
//1 块钱如银行的账号
}
@Override
public void withdraw(double amount) {
super.withdraw(amount + 1);
}
}
SavingAccount.java
package com.Marisa.homework08;
public class SavingAccount extends BankAccount{
//新增加的属性
private int count = 3; //记录几次存款或取款
private double rate = 0.01; //存款利率
public SavingAccount(int balance) {
super(balance);
}
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);
}
count--;
}
@Override
public void withdraw(double amount) {
if(count > 0){
super.withdraw(amount);
} else {
super.withdraw(amount + 1);
}
count--;
}
public void earnMonthlyInterest(){
count = 3; // 重置免手续费次数
super.deposit(getBalance() * rate);
}
}
Homework08.java
package com.Marisa.homework08;
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());
//测试
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());
//到了月初(假设定时器自动调用一下earnMonthlyInterest)
savingAccount.earnMonthlyInterest();
System.out.println(savingAccount.getBalance());//1399 + 13.99
//再取款,定时器重置count,免手续费
savingAccount.withdraw(100);
System.out.println(savingAccount.getBalance());
savingAccount.withdraw(100);
savingAccount.withdraw(100);
System.out.println(savingAccount.getBalance());
//第三次后多扣手续费
savingAccount.withdraw(100);
System.out.println(savingAccount.getBalance());
}
}
输出结果:
999.0
1300.0
1399.0
1412.99
1312.99
1112.99
1011.99
350. 本章作业09
Point.java
package com.Marisa.homework09;
public class Point {
private double x;
private double y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
}
LabeledPoint.java
package com.Marisa.homework09;
public class LabeledPoint extends Point{
//坐标标签
private String label;
public LabeledPoint(String label, double x, double y) {
super(x, y);
this.label = label;
}
}
Homework09.java
package com.Marisa.homework09;
public class Homework09 {
public static void main(String[] args) {
LabeledPoint black = new LabeledPoint("Black", 1929, 230.07);
}
}
351. 本章作业10
Doctor.java
package com.Marisa.homework10;
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;
}
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;
}
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.job.equals(doctor.job) && this.gender == doctor.gender && this.sal == doctor.sal;
}
}
Homework10.java
package com.Marisa.homework10;
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(doctor2.equals(doctor1));
Doctor doctor3 = new Doctor("jack", 21, "牙科医生", '男', 20000);
Doctor doctor4 = new Doctor("jack", 22, "牙科医生", '男', 20000);
System.out.println(doctor3.equals(doctor4));
}
}
运行结果:
true
false
352. 本章作业11
向上转型,方法只能调用编译类型(左侧)