JAVA语言(继承与多态)

    1. 实验目的
  1. 掌握方法的继承,重载与覆盖;
  2. 掌握抽象类的定义与继承;
  3. 理解多态的概念,掌握接口的实现方法。
    1. 实验工具
  4. 编程语言:Python;
  5. 开发环境:Eclipse(或MyEclipse、NetBeans、IntelliJ IDEA等)。
    1. 实验题目

使用面向对象的多态性模仿会员卡消费系统,设计会员卡类,可派生不同的会员卡类型,当使用不同的会员卡消费时其折扣不同。如一般会员卡消费打9折;VIP会员卡打7折;超级VIP卡打5折。其具体的实现效果如下所示:

输入应消费的金额->选择相应的会员卡->显示实际的应付金额和消费状态信息。

实验要求:

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

1. 安装JDK、Java集成式开发环境,并配置相应开发环境;

2. 按照题目要求,编写相应Java程序。

(请附上详细代码、运行截图等内容)

import java.util.Scanner;
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{                          
    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{                           
    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){                                             
        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\t2vip会员卡,7折优惠\n\t3svip会员卡,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\t2vip会员卡,7折优惠\n\t3svip会员卡,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;
            }

        }
    }

}

    1. 实验心得

简要介绍你在实验中使用到的继承、多态、封装等机制的使用注意事项?

1.子类继承了所有的属性和方法,非私有的属性和方法可以在子类中直接访问,但是私有的属性和方法不能在子类直接访问,要通过公共的方法去访问

  • 17
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值