第5章 子类与继承(JAVA)

实验 1 中国人,北京人和美国人

实验目的:

本实验的目的巩固以下知识点:

(1)子类的继承性

(2)子类对象的创建过程

(3)成员变量的继承与隐藏

(4)方法的继承与重写

//People.java
public class People {
    protected double weight, height;
    public void speakHello(){
        System.out.println("yayayaya");
    }
    public void averageHeight(){
        height=173;
        System.out.println("average height"+height);
    }
    public void averageWeight(){
        weight=70;
        System.out.println("average weight"+weight);
    }
}
//ChinaPeople.java
public class ChinaPeople extends People{
    public void speakHello(){
        System.out.println("您好");
    }
    public void averageHeight(){
        height = 168.78;
        System.out.println("中国人的平均身高:"+height+"厘米");
    }
    public void averageWeight(){
        weight = 65;
        System.out.println("中国人的平均体重:"+weight+"公斤");
    }
    public void chinaGongfu(){
        System.out.println("坐如钟,站如松,睡如弓");
    }
}
//AmericanPeople.java
public class AmericanPeople extends People{
    public void speakHello(){
        System.out.println("How do you do");
    }
    public void averageHeight(){
        height=176;
        System.out.println("average height"+height+"cm");
    }
    public void averageWeight(){
        weight=75;
        System.out.println("average weight"+weight+"kg");
    }
    public void americanBoxing(){
        System.out.println("直拳,勾拳,组合拳");
    }
}
//BeijingPeople.java
public class BeijingPeople extends ChinaPeople{
    public void averageHeight(){
        height = 172.5;
        System.out.println("中国人的平均身高:"+height+"厘米");
    }
    public void averageWeight(){
        weight = 70;
        System.out.println("中国人的平均体重:"+weight+"公斤");
    }
    public void beijingOpera(){
        System.out.println("花脸,青衣,花旦和老生");
    }
}
//Example.java
public class Example {
    public static void main(String[] args) {
        ChinaPeople chinaPeople = new ChinaPeople();
        AmericanPeople americanPeople = new AmericanPeople();
        BeijingPeople beijingPeople = new BeijingPeople();
        chinaPeople.speakHello();
        americanPeople.speakHello();
        beijingPeople.speakHello();
        chinaPeople.averageHeight();;
        americanPeople.averageHeight();;
        beijingPeople.averageHeight();
        chinaPeople.averageWeight();
        americanPeople.averageWeight();
        beijingPeople.averageWeight();
        chinaPeople.chinaGongfu();
        americanPeople.americanBoxing();
        beijingPeople.beijingOpera();
        beijingPeople.chinaGongfu();
    }
}

调试结果

 


实验 2 银行计算利息

实验目的:

掌握重写的目的以及怎样使用super关键字

 

//Bank.java
public class Bank {
    int savedMoney;
    int year;
    double interest;
    double interestRate = 0.29;
    public double computerInterest(){
        interest=year*interestRate*savedMoney;
        return interest;
    }
    public void setInterestRate(double rate){
        interestRate = rate;
    }
}
//ConstructionBank.java
public class ConstructionBank extends Bank{
    double year;
    public double computerInterest(){
        super.year = (int)year;
        double r = year - (int)year;
        int day = (int)(r*1000);
        double yearInterest = super.computerInterest();
        double dayInterest = day*0.0001*savedMoney;
        interest = yearInterest+dayInterest;
        System.out.printf("%d元存在建设银行%d年零%d天的利息:%f元\n",savedMoney,super.year,day,interest);
        return interest;
    }
}
//BankOfDalian.java
public class BankOfDalian extends Bank{
    double year;
    public double computerInterest(){
        super.year = (int)year;
        double r = year - (int)year;
        int day = (int)(r*1000);
        double yearInterest = super.computerInterest();
        double dayInterest = day*0.00012*savedMoney;
        interest = yearInterest+dayInterest;
        System.out.printf("%d元存在大连银行%d年零%d天的利息:%f元\n",savedMoney,super.year,day,interest);
        return interest;
    }
}
//SaveMoney.java
public class SaveMoney {
    public static void main(String[] args) {
        int amount = 8000;
        ConstructionBank bank1 = new ConstructionBank();
        bank1.savedMoney = amount;
        bank1.year = 8.236;
        bank1.setInterestRate(0.035);
        double interest1 = bank1.computerInterest();
        BankOfDalian bank2 = new BankOfDalian();
        bank2.savedMoney = amount;
        bank2.year = 8.236;
        bank2.setInterestRate(0.035);
        double interest2 = bank2.computerInterest();
        System.out.printf("两个银行利息相差%f元\n",interest2-interest1);
    }
}

调试结果


 实验 3 公司支出的总薪水

实验目的:掌握上转型对象的使用。在讲述继承与多态时通过子类对象的上转型体现了继承的多态性,即把子类创建的对象的引用放到一个父类的对象中,得到该对象的一个上转型对象,那么这个上转型对象在调用方法时就可能具有多种形态,不同对象的上转型对象调用同一方法可能产生不同的行为

abstract class Employee {
    public abstract double earnings();
}

class YearWorker extends Employee{

    @Override
    public double earnings() {
        return 12000;
    }
}

class MonthWorker extends Employee{

    @Override
    public double earnings() {
        return 12*2300;
    }
}

class  WeekWorker extends Employee{

    @Override
    public double earnings() {
        return 52*780;
    }
}


class Company{
    Employee[] employee;
    double salaries=0;

    public Company(Employee[] employee) {
        this.employee = employee;
    }

    public double salariesPay(){
        salaries=0;
        //计算salaries
        for(int i=0;i<employee.length;i++){
            salaries=salaries+employee[i].earnings();
        }
        return salaries;
    }
}
public class CompanySalary {
    public static void main(String[] args) {
        Employee[] employee = new Employee[29];
        for (int i = 0; i < employee.length; i++) {
            if(i%3==0)
                employee[i]=new WeekWorker();
            else if (i%3==1)
                employee[i] = new MonthWorker();
            else if (i%3==2)
                employee[i] = new YearWorker();
        }
        Company company = new Company(employee);
        System.out.println("公司薪水总额:"+company.salariesPay()+"元");
    }
}

调试结果


实验 4 女孩相亲 

实验目的:掌握面向抽象编程的应用

如有错误,请不吝赐教

//Person.java
public abstract class Person {
    int height;
    int weight;
    //look按照颜值打分0-100分
    int look;
    public abstract void height();
    public abstract void weight();
    public abstract void look();
}
//Person.java
public class Girl extends Person{
    int he=180;
    int we=70;
    int lo=80;

    @Override
    public void height() {
        System.out.println("身高不低于"+he+"cm");
    }

    @Override
    public void weight() {
        System.out.println("体重不高于"+we+"kg");
    }

    @Override
    public void look() {
        System.out.println("颜值不低于"+lo+"分");
    }
}
//Boy.java
public class Boy extends Person{
    @Override
    public void height() {

    }

    @Override
    public void weight() {

    }

    @Override
    public void look() {

    }
}
//Main.java
public class Main {
    public static void main(String[] args) {
        Girl girl = new Girl();
        int[] he = {180,165,178,172,169,190,175,170,188,182,160,179,188,187,177};
        int[] we = {60,77,65,69,82,66,78,58,69,71,70,65,67,61,80};
        int[] lo = {80,60,65,55,75,88,81,77,79,85,76,61,80,90,66};
        Person[] boys = new Person[15];
        for (int i = 0; i < boys.length; i++) {
            boys[i] = new Boy();
            boys[i].height = he[i];
            boys[i].weight = we[i];
            boys[i].look = lo[i];
        }
        show0();
        while(true){
            Scanner sc = new Scanner(System.in);
            String choose = sc.next();
            switch (choose){
                case "1" :
                    show1(girl);
                    break;
                case "2" :
                    show2(boys,girl);
                    break;
                case "3" :
                    show3(girl);
                    break;
                case "4":
                    System.exit(0);
                default:
                    System.out.println("没有这个选项,请重新输入");
            }
        }
    }
    //显示界面
    private static void show0(){
        System.out.println("请选择输入序号进行查看");
        System.out.println("1.查看女孩择偶要求");
        System.out.println("2.查看数据库中符合的男孩");
        System.out.println("3.查询自己是否符合条件");
        System.out.println("4.退出");
    }

    //打印符合条件的男孩
    private static void show2(Person[] boys,Girl girl){
        //System.out.println("符合要求的男孩:");
        int j = 1;
        for (int i = 0; i < boys.length; i++) {
            if(boys[i].height>= girl.he&&boys[i].weight<= girl.we&&boys[i].look>= girl.lo){
                System.out.println("男孩"+j+"的身高:"+boys[i].height+" 体重:"+boys[i].weight+" 颜值分数:"+boys[i].look);
                j++;
            }
        }
    }

    //打印女孩基本要求
    private static void show1(Girl girl){
        System.out.println("女孩的基本要求:");
        girl.height();
        girl.weight();
        girl.look();
    }

    //查询自己
    private static void show3(Girl girl){
        Scanner my = new Scanner(System.in);
        while(true){
            System.out.print("输入你的身高:");
            int i = my.nextInt();
            if(i>=girl.he){
                System.out.print("输入你的体重:");
                int j = my.nextInt();
                if(j<= girl.we){
                    System.out.print("输入你的颜值:");
                    int k = my.nextInt();
                    if(k>= girl.lo) {
                        System.out.println("恭喜你,你符合女孩的基本要求!");
                        break;
                    }
                    else{
                        System.out.println("对不起,你不符合条件");
                        break;
                    }
                }
                else {
                    System.out.println("对不起,你不符合条件");
                    break;
                }
            }
            else{
                System.out.println("对不起,你不符合条件");
                break;
            }

        }
    }

}

调试结果:

  • 5
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

云上成理

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

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

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

打赏作者

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

抵扣说明:

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

余额充值