当前位置: 首页 > article >正文

【韩顺平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


向上转型,方法只能调用编译类型(左侧)

353. 本章作业12


http://www.kler.cn/a/516040.html

相关文章:

  • 考研机试:买房子
  • vector的使用,以及部分功能的模拟实现(C++)
  • 【Unity】ScrollViewContent适配问题(Contentsizefilter不刷新、ContentSizeFilter失效问题)
  • Golang之Context详解
  • 计算机网络——网络层
  • dp 凸优化
  • salesforce apex测试类如果有多个httpmock,则只会返回一个,导致可能不符合预期
  • `std::make_shared` 无法直接用于单例模式,因为它需要访问构造函数,而构造函数通常是私有的
  • Linux - 五种常见I/O模型
  • Spring MVC:综合练习 - 深刻理解前后端交互过程
  • PaSa - 大型语言模型提供支持的高级论文搜索代理
  • 使用KNN实现对鸢尾花数据集或者自定义数据集的的预测
  • 基于JAVA的微信点餐小程序设计与实现(LW+源码+讲解)
  • FCA-FineReport试卷
  • 数据挖掘常用算法模型简介
  • 有关Android Studio的安装与配置并实现helloworld(有jdk的安装与配置)(保姆级教程)
  • 云计算和服务器
  • 软件工程的本质特征
  • 无人机高速无刷动力电机核心设计技术
  • Python 之 Excel 表格常用操作
  • 考研机试:学分绩点
  • linux 扩容
  • MySQL 中开启二进制日志(Binlog)
  • 0164__【GNU】gcc -O编译选项 -Og -O0 -O1 -O2 -O3 -Os
  • three.js+WebGL踩坑经验合集(1):THREE.Line无故消失的元凶
  • c++-------------------------继承