-
- 实验目的
- 掌握方法的继承,重载与覆盖;
- 掌握抽象类的定义与继承;
- 理解多态的概念,掌握接口的实现方法。
- 实验工具
- 编程语言:Python;
- 开发环境:Eclipse(或MyEclipse、NetBeans、IntelliJ IDEA等)。
- 实验题目
使用面向对象的多态性模仿会员卡消费系统,设计会员卡类,可派生不同的会员卡类型,当使用不同的会员卡消费时其折扣不同。如一般会员卡消费打9折;VIP会员卡打7折;超级VIP卡打5折。其具体的实现效果如下所示:
输入应消费的金额->选择相应的会员卡->显示实际的应付金额和消费状态信息。
实验要求:
- 程序设计必须符合面向对象的编程思想;
- 设计会员卡类,包含卡类型和卡内金额两个属性;
- 由会员卡类进一步派生出不同类型的会员卡,如:一般会员卡消费打9折;VIP会员卡打7折;超级VIP卡打5折;
- 对不同会员卡类进行具体实现;
- 设计并实现刷卡机类,主要用于人机交互和显示消费状态信息;
- 创建不同类型会员卡的具体对象,并进行相应调用显示消费状态信息。
- 实验步骤
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\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;
}
}
}
}
-
- 实验心得
简要介绍你在实验中使用到的继承、多态、封装等机制的使用注意事项?
1.子类继承了所有的属性和方法,非私有的属性和方法可以在子类中直接访问,但是私有的属性和方法不能在子类直接访问,要通过公共的方法去访问。