java 多态应用 编写 LOL商店英雄购买

package dierzhou;

import java.util.Scanner;
class Heros{ 
	 String name;
	 String Attribute;
	 int price;
	public String getName() {//英雄
		return name;
	}
	public String getAttribute() {//属性
		return Attribute;
	}
	public int getPrice() {//英雄价格
		return price;
	}
	public void shanghai(){  //基础伤害
	}
}

class Ap extends Heros{
	@Override
	public void shanghai() {
		// TODO Auto-generated method stub
		if(6000<this.price){
			System.out.println(this.name+"的基础伤害为150");
		}else if(3500<=this.price){
			System.out.println(this.name+"的基础伤害为145");
		}else if(450<=this.price){
			System.out.println(this.name+"的基础伤害为143");
		}
	}
}
	class Ad extends Heros{
		@Override
		public void shanghai() {
			// TODO Auto-generated method stub
			if(6000<this.price){
				System.out.println(this.name+"的基础伤害为152");
			}else if(3500<=this.price){
				System.out.println(this.name+"的基础伤害为146");
			}else if(450<=this.price){
				System.out.println(this.name+"的基础伤害为141");
			}
		}
	}
	class maste{	   //多态
	public void toStr(Heros heros){
		heros.shanghai();
	}
}
public class LOL {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		maste ma=new maste();
		System.out.println("*******|||LOL商店|||********");
		System.out.println("ap英雄有:沙皇 ¥6300 \t妖姬¥ 4800\t小法 ¥1350");
         Ap ap=new Ap();
         Scanner input=new Scanner(System.in);
         System.out.println("选择你要购买的ap英雄:");
         String h1=input.next();
         System.out.println("请进行付款:");
         int m1=input.nextInt();
         ap.name=h1;
         ap.price=m1;
         ap.Attribute="法师";
         System.out.println("恭喜你购买"+ap.getName()+"成功"+"\n付款金额为"+ap.getPrice()+"\n英雄属性:"+ap.getAttribute());
         ma.toStr(ap);//多态
         System.out.println("ad英雄有:蛮王 ¥6300 \t剑魔¥ 4800\t剑圣 ¥460");
         Ad ad=new Ad();
         System.out.println("选择你要购买的ad英雄");
         String h2=input.next();
         System.out.println("请进行付款:");
         int m2=input.nextInt();
         ad.name=h2;
         ad.price=m2;
         ad.Attribute="战士";
         System.out.println("恭喜你购买"+ad.getName()+"成功"+"\n付款金额为"+ad.getPrice()+"\n英雄属性:"+ad.getAttribute());
         ma.toStr(ad);
	}
}

效果:

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值