Java打造简单的游戏数据库(继承与多态的应用)

Writted by Bruth_Lee in Southwest University of Science And Technology.

This database is mainly to modify the last code, using the code that repeats the inheritance and polymorphism.
  

这次的数据库主要是对上次的代码进行修改,利用继承与多态改进重复的代码!!


There are some classes as follow.

Test:

package GAME;

import java.util.ArrayList;

public class Test {
	ArrayList<item> listitem = new ArrayList<item>();
	
	public void add(item item1) {
		listitem.add(item1);
	}
	public void list() {
		for(item item1 : listitem) {
//			((Gameplayer) item).print();
			item1.print();
		}
	}
//	private void print() {
//		// TODO Auto-generated method stub
//		
//	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		item i = new item();
		i.add(new Gameplayer("审判者", "英雄人物角色,手持暗影利刃,防高摔,免己方手雷", 100, 78,88, "性感与智慧的化生,VIP玩家必不可少的人物", "好想要啊,就是买不起!"));
		i.add(new Boardgame("王者荣耀","这是风靡全国的一款大型手游!","不好玩,我连我小弟都打不赢!!","5V5 or 1V1 3V3"));
		i.add(new Videogame("双截龙2", "这是一款美国产的智力动作类游戏!", "特别适合小学生玩!!!","1V1 or 2V2",88));
		i.add(new MP3("体面", "于文文", "3:50", "新歌排行榜排名第一!!"));
		i.list();
	}

}

item:

package GAME;

import java.util.ArrayList;
import java.util.List;

public class item extends Test{
	
	protected String title;
	protected String comment;
	public item(String title, String comment) {
		super();
		this.title = title;
		this.comment = comment;
	}
	public item() {
		// TODO Auto-generated constructor stub
	}
	public item(String comment) {
		// TODO Auto-generated constructor stub
		this.comment = comment;
	}
	public void add(item item1) {
		listitem.add(item1);
	}
	public void list() {
		for(item item1 : listitem) {
//			((Gameplayer) item).print();
			item1.print();
		}
	}
	void print() {
		// TODO Auto-generated method stub
		
	}

}
MP3:

package GAME;
	
public class MP3 extends item {
	private String singer;
	private String length;
//	public MP3() {
//		super();
//		// TODO Auto-generated constructor stub
//	}
//	public MP3(String title,String singer,String length, String comment) {
//		super(title, comment);
//		// TODO Auto-generated constructor stub
//	}
	public void print() {
		System.out.println("title : "+title + "  singer : "+singer+"  length : "+length+" comment : "+comment );
		System.out.println();
	}
	public MP3(String title, String singer, String length,String comment) {
	super(title, comment);
	this.singer = singer;
	this.length = length;
}
}


Gameplayer:

package GAME;

import java.util.ArrayList;

public class Gameplayer extends item {
	protected String num_of_players;
	protected String introduction;
	private String name;
	private String feature;
	private int blood_volume;
	private int Aggressitivity;
	private int Defenses;

	public void print() {
//		System.out.println("title : "+title+" num_of_players : "+num_of_players);
		System.out.println("Name : "+name+"  Feature : "+feature);
		System.out.println("Blood_volume : "+blood_volume+"  Aggressitivity : "+Aggressitivity+"  Defenses : "+Defenses);
		System.out.println("introduction : "+introduction+"  comment : "+comment);
		System.out.println("****************************************************");
		System.out.println();

	}
public Gameplayer( String name, String feature,
		int blood_volume, int aggressitivity, int defenses, String introduction,String comment) {
	super( comment);
	this.introduction = introduction;
	this.name = name;
	this.feature = feature;
	this.blood_volume = blood_volume;
	Aggressitivity = aggressitivity;
	Defenses = defenses;
}
public Gameplayer(String title,String comment, String introduction2, String num_of_players) {
	// TODO Auto-generated constructor stub
	super(title,comment);
	this.introduction = introduction2;
	this.num_of_players = num_of_players;
}

public Gameplayer(String title, String comment, String introduction) {
	super(title, comment);
	this.introduction = introduction;
}

	
}

Boardgame:

package GAME;

public class Boardgame extends Gameplayer {


	public Boardgame(String title,String introduction, String comment,String num_of_players) {
		super(title, introduction,comment,num_of_players);
		// TODO Auto-generated constructor stub
	}

	public void print() {
		System.out.println("Top1 : "+title+"  num_of_players : "+num_of_players);
		System.out.println("introduction : "+introduction+"  comment : "+comment);
		System.out.println("****************************************************");
		System.out.println();
	}

}

Videogame:

package GAME;

public class Videogame extends Gameplayer {
	private int hot;
	public Videogame(String title, String introduction,String comment,String num_of_players,int hot) {
		super(title, introduction,comment,num_of_players);
		this.hot = hot;
		// TODO Auto-generated constructor stub
	}

	public void print() {
		System.out.println("Hot : "+hot+" title : "+title+" num_of_players : "+num_of_players);
		System.out.println("introduction : "+introduction+" comment : "+comment);
		System.out.println("****************************************************");
		System.out.println();

	}
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

江湖无为

感谢你们的鼓励

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

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

打赏作者

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

抵扣说明:

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

余额充值