The world of Dragon Ball is composed of various warriors such as Goku, Android Cell, and Majin Buu. This time, we will design and implement a small case while focusing on those who are different from ordinary people.
Summary of frequently used items
Warrior features: Fighting function → Treat as an interface
Fighter.java
interface Fighter {
void fight();
}
Warriors possess the above functions and have the following characteristics
DragonballWarrior.java
public class DragonballWarrior implements Fighter {
private String name;
private int property;
private int HP;
@Override
public void fight() {
}
public void getSkill() {
}
public void doSkill() {
}
}
Goku can be treated as a subclass of warriors Summarize the common items of warriors and further subdivide
- Saiyan (Super Saiyan under certain conditions / Monkey when looking at the moon)
Saiyan.java
public class Saiyan extends DragonballWarrior {
/** [1:normal / 2:monkey / 3:super-saiyan] */
private int status;
public void changeStatus(Saiyan saiyan) {
this.status = saiyan.status;
}
}
I want to design a Tenkaichi Budokai that fights warriors
- Field
Tenkaichi Budokai-Thinking about the has-a relationship of warriors Define superclass constructors and getters / setters
DragonballWarrior.java
public class DragonballWarrior implements Fighter {
private String name;
private int property;
private int HP;
/** default constractor */
DragonballWarrior() {
}
DragonballWarrior(String name, int property, int HP) {
this.name = name;
this.property = property;
this.HP = HP;
}
@Override
public void fight() {
}
public void getSkill() {
}
public void doSkill() {
}
// S - getter,setter
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getProperty() {
return property;
}
public void setProperty(int property) {
this.property = property;
}
public int getHP() {
return HP;
}
public void setHP(int hP) {
HP = hP;
}
// E - getter,setter
}
Saiyan.java
public class Saiyan extends DragonballWarrior {
/** [1:normal / 2:monkey / 3:super-saiyan] */
private int status = 0;
public Saiyan(String name, int property, int HP) {
super(name, property, HP);
}
public void changeStatus(Saiyan saiyan) {
this.status = saiyan.status;
}
@Override
public void doSkill() {
if (super.getName().equals("Goku")) {
System.out.println("kamehame-ha");
}
}
@Override
public void fight() {
if (super.getName().equals("Goku")) {
System.out.println("ora-Goku!");
}
}
Saiyan.java
public class MartialArtist extends DragonballWarrior {
public MartialArtist(String name, int property, int HP) {
super(name, property, HP);
}
@Override
public void doSkill() {
if (super.getName().equals("Kuririn")) {
System.out.println("kien-zan");
}
}
@Override
public void fight() {
if (super.getName().equals("Kuririn")) {
System.out.println("oreha-Kuririn!");
}
}
}
BattleTournament.java
public class BattleTournament {
private List<DragonballWarrior> dwlist;
public static void main(String args[]) {
List<DragonballWarrior> dwlist = new ArrayList<>();
dwlist.add(new Saiyan("Goku", 50000, 200));
dwlist.add(new Saiyan("Vegeta", 40000, 250));
dwlist.add(new MartialArtist("Kuririn", 3000, 80));
dwlist.add(new MartialArtist("Kamesennin", 3000, 80));
for (DragonballWarrior dw : dwlist) {
dw.fight();
dw.doSkill();
}
}
}
Execution result.java
ora-Goku!
kamehame-ha
oreha-Kuririn!
kien-zan
It ’s full of stuff, I was able to confirm that polymorphism is working
→ Someday
3 months tester → I thought I had to go to basic design and learn design patterns Perfect cell is singleton class design, technique is interface or abstract class and composition relationship
Now I would like to summarize anonymous classes-> generics-> lambda expressions and functional interfaces-> Stream API.