java实验报告:实验二 类的继承与多态

注:博主为初学java,此实验报告代码仅供参考,如有错误,还望指正。

水平有限,仅供参考






实验目的及要求

1.掌握方法的继承,重载与覆盖。
2.掌握抽象类的定义与继承。
3.理解多态的概念,掌握接口的实现方法。

类的继承性

继承性
继承是一种由已有的类创建新类的机制。利用继承,可以先创建一个共有属性的一般类,根据该一般类再创建具有特殊属性的新类,新类继承一般类的属性和行为,并根据需要增加自己的新的属性和行为。由继承而得到的类称为子类,被继承的类称为父类(超类)。Java不支持多重继承(子类只能有一个父类)。
多态性
多态性就是指父类的某个方法被其子类重写时,可以各自产生自己的功能行为。
当一个类有很多子类时,并且这些子类都重写了父类中的某个方法。那么当子类创建的对象的引用放到一个父类的对象中时,就得到了该对象的一个上转型对象,那么这个上转的对象在调用这个方法时就可能具有多种形态。

实验内容和步骤

使用面向对象的多态性模仿会员卡消费系统,设计会员卡类,可派生不同的会员卡类型,当使用不同的会员卡消费时其折扣不同。如一般会员卡消费打9折;VIP会员卡打7折;超级VIP卡打5折。
输入应消费的金额,选择相应的会员卡,会显示实际的应付金额和消费状态信息。

实验步骤:

1、设计会员卡类,包含卡类型和卡内金额两个属性。
2、由会员卡类进一步派生出不同类型的会员卡,如:一般会员卡消费打9折;VIP会员卡打7折;超级VIP卡打5折。
3、对不同会员卡类进行具体实现。
4、设计并实现刷卡机类,主要用于人机交互和显示消费状态信息。
5、创建不同类型会员卡的具体对象,并进行相应调用显示消费状态信息。

CardBuyBooks.java

package com.taor;
import java.util.ArrayList; // 引入 ArrayList 类
import java.util.Scanner;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.InputMismatchException;


// 添加会员卡类
abstract class Card{          // 抽象类:会员卡类
    private String type;    // 会员卡类型
    private int discount;   // 折扣
    private float amount;   // 余额
    Card(String _type){
        this.type = _type;
    }
    public String getType(){    // 获得购物卡类型
        return type;
    }
    public int getDiscount() {
        return discount;
    }

    public boolean comsume(float totalPrice){       // 是否支付成功
        if (this.amount >= this.shouldPay(totalPrice)){
            this.amount = this.amount - this.shouldPay(totalPrice);
            return true;
        } else{
            return false;
        }
    }
    public abstract float shouldPay(float totalPrice);  // 应支付
    public float getAmount(){                           // 获得余额
        return amount;
    }
    public void setAmount(float amount){                // 设置余额
        this.amount = amount;
    }
    public void setDiscount(int discount){            // 设置折扣
        this.discount = discount;
    }
}

class generalCard extends Card{                         // 普通会员卡
    generalCard(String _type,int dis){
        super(_type);
        this.setDiscount(dis);
    }
    public float shouldPay(float totalPrice) {
        totalPrice = totalPrice * 0.9f;
        if(totalPrice > this.getAmount())
            return -1;
        else {
            setAmount(getAmount()-totalPrice);
            return totalPrice;
        }
    }
}

class vipCard extends Card{                             // vip
    vipCard(String _type,int dis){
        super(_type);
        this.setDiscount(dis);
    }
    public float shouldPay(float totalPrice) {
        totalPrice = totalPrice * 0.7f;
        if(totalPrice > this.getAmount())
            return -1;
        else {
            setAmount(getAmount()-totalPrice);
            return totalPrice;
        }
    }
}


class svipCard extends Card{                            // svip
    svipCard(String _type,int dis){
        super(_type);
        this.setDiscount(dis);
    }
    public float shouldPay(float totalPrice) {
        totalPrice = totalPrice * 0.5f;
        if(totalPrice > this.getAmount())
            return -1;
        else {
            setAmount(getAmount()-totalPrice);
            return totalPrice;
        }
    }
}




public class CardBuyBooks {
    // 显示花费
    static void Show (Card mc,float cosume){                                             // pos机,刷卡消费
        System.out.print("\t您的会员卡为 "+mc.getType());
        System.out.println("\t可以享受 "+ mc.getDiscount() + " 优惠" );
        System.out.println("\t本次花费应支付的金额 "+cosume);
        float pay = mc.shouldPay(cosume);
        if(pay == -1){
            System.out.println("\t支付失败! 余额不足");
        }else  {
            System.out.print("\t实际支付金额为 "+ mc.shouldPay(cosume) );
            System.out.println("\n\t您的会员卡剩余金额为: "+mc.getAmount());
            System.out.println("\t\n\n\n\n\n\n\n\n\n\n\n\n\n\n" );
        }
    }

    public static void main(String[] args){
        generalCard cd1 = new generalCard("普通会员",9);
        vipCard cd2 = new vipCard("VIP",7);
        svipCard cd3 = new svipCard("SVIP",5);

        for(int i = 0; i < 100; i++){
            System.out.println("\t会员卡消费打折系统");
            System.out.println("*****************************");
            System.out.print("\t请输入会员卡种类\n\t1、普通会员卡,9折优惠\n\t2、vip会员卡,7折优惠\n\t3、svip会员卡,5折会员卡\n>>>");
            boolean tag = false;
            String type;
            do{
                tag = false;
                Scanner scType = new Scanner(System.in);
                type = scType.next();
                if(!(type.equals("1")||type.equals("2")||type.equals("3"))){
                    tag = true;System.out.println("输入错误,请重新输入");
                    System.out.print("1、普通会员卡,9折优惠\n\t2、vip会员卡,7折优惠\n\t3、svip会员卡,5折会员卡\n>>>");
                }
            }while(tag);

            System.out.print("\t请输入会员卡内余额(0~100000)\n>>>");
            Scanner scBalance = new Scanner(System.in);
            float balance = scBalance.nextFloat();
            System.out.print("\t请输入需要支付的金额\n>>>");
            Scanner scPay = new Scanner(System.in);
            float pay = scBalance.nextFloat();

            switch(type)
            {
                 case "1":
                     cd1.setAmount(balance);    // 设置卡内金额
                     Show(cd1,pay);
                    break;
                case "2":
                    cd2.setAmount(balance);
                    Show(cd2,pay);
                    break;
                case "3":
                    cd3.setAmount(balance);
                    Show(cd3,pay);
                    break;
                default:
                    System.out.println("\t错误\n");
                    break;
            }

        }
    }

}


//    public static Card creatCard()
//    {
//        boolean tag = false;    // 检查输入是否正确
//        do{
//            tag = false;
//            Scanner scType = new Scanner(System.in);
//            String type = scType.next();
//            Card cd ;
//            switch(type)
//            {
//                case "1":
//                    cd = new generalCard("generalCard",9);
//                    break;
//                case "2":
//                    cd = new vipCard("vipCard",7);
//                    break;
//                case "3":
//                    cd = new svipCard("svipCard",5);
//                    break;
//                default:
//                    System.out.println("输入错误,请重新输入");
//                    System.out.println("1、普通会员卡,9折优惠\n\t2、vip会员卡,7折优惠\n\t3、svip会员卡,5折会员卡\n>>>");
//                    tag = true;
//                    break;
//            }
//        }while(tag);
//        return this;
//    }

//
//    generalCard cd1 = new generalCard();
//        cd1.type = "generalCard";
//                vipCard cd2 = new vipCard();
//                cd2.type = "vipCard";
//                svipCard cd3 = new svipCard();
//                cd3.type = "svipCard";
  • 4
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

我叫RT

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

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

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

打赏作者

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

抵扣说明:

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

余额充值