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();
}
}