【JAVA】实现简单“英雄联盟”信息处理系统实现

229 阅读3分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

【JAVA】实现简单“英雄联盟”信息处理系统

一、问题陈述

主要对游戏《英雄联盟》中的一些人物信息进行处理,使其可以显示出该人物在不同模式中的种族、国家、售价、别称、台词等信息。

二、设计方案

实验以主类【YXLM】为基础,创建【Information】类。同时以该方法为父类创建其两个子类【YDZY】与【Other】,以实现对多种模式下人物不同信息的展现。并通过重写父类中的方法与多态对不用模式下的同名但不同属性的一些数据进行了处理,使其可以正确的输出结果。具体设计如图1所示:

image.png

图1.程序UML图

三、运行结果

image.png

图2.运行结果截图

四、结果分析

该程序通过类的继承、多态及方法重写调用等方式,实现了对游戏人物信息的简单处理。具体通过继承实现了名字等多个信息的处理,通过重写父类的方法实现了不同模式下的售价处理该,通过多态实现的多个信息的输出等等。并对多个类的地址是否相同进行了判断。

五、总结

该实验在多处运用了继承多态等方式,实现了简单的人物信息处理。对于继承,子类可以继承父类所有的非私有方法和属性。若子类与父类中存在同名的方法,则在调用之类时直接覆盖父类中的方法。因而可以做到对多个同属性及不同属性物的混合处理。 在实验过程中由于对一些内容不够熟悉,多次运用互联网方式,进行了学习,并最后能够较好的掌握该内容。但实验中仍有不足之处,例如功能不够丰富,为实现对界面的展出等。但后续有时间还会通过互联网工具学习,继续丰富功能进一步完善系统。

六、程序代码

6.1 YXLM类(主)

public class YXLM {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		Information[] inf = new Information[4];
		inf[0] = new Information();
		inf[1] = new Information("亚索","艾欧尼亚",6300);
		inf[2] = new YDZY("快乐风男","浪人&剑士",5);
		inf[3] = new Other();
		
		System.out.println(inf[0].getInformation1());
		System.out.println(inf[1].getInformation2());
		System.out.println(inf[2].getInformation2());
		System.out.println(inf[3].getInformation2());		
		System.out.println(inf[1].equals(inf[2]));
	}

}

6.2 YDZY类

public class YDZY extends Information{
	private String petname;
	private String race;
	private int price;
	private Other myMgrm = new Other();

public YDZY(String petname,String race,int price) {
	this.petname = petname;
	this.race = race;
	this.price = price;	
	}

public String getInformation2() {
	return "PetName"+petname+"\n"+"Race:"+race+"\n"+"Price:"+price+"\n";
	}
}

6.3 Information类

public class Information{
	private String name;
	private String country;
	private int price;
	
	public Information(String name,String country,int price) {
		super();
		this.name = name;
		this.country = country;
		this.price = price;
	}
	public Information() {
		this.name = "League of Legends";
		this.country = "Americ";
		this.price = 8888888;			
	}

	public String getInformation1() {
		return "GameName:"+name+"\n"+"Country:"+country+"\n";
	}
	public String getInformation2() {
		return "Name"+name+"\n"+"Country:"+country+"Price:"+"\n"+price+"\n";
	}
	public String toString() {
		return (this.name + ":" + this.price);	
	}
	public boolean equals(Object obj) {
        if(this == obj){
            return true;
        }
        if(obj == null){
            return false;
        }
		return false;
	}
}

6.4 Other类

class Other extends Information{

	private String word1;
	private String word2;

	public Other() {
		this.word1 = "且随疾风前行,身后亦需留心";
		this.word2 ="XXXX,常伴吾身";
	}
	
	public String getInformation2() {
		return "台词1:"+word1+"\n"+"台词2:"+word2+"\n";
	}

}