簡體   English   中英

沒有getClass()的情況下如何區分Java中的相似對象?

[英]How to differenciate similar objects in Java without getClass()?

我正在用Java做一個簡單的游戲,我認為我有一個設計問題。 我有一個具有所有單元行為的抽象類Unit(例如move(),attack(),getHealth()等)。 然后,我從部隊擴展了三個班級:弓箭手,持槍騎兵和騎手。 其中的每一個都只有一個構造函數來區分它們,因為它們的行為完全相同,但具有不同的統計信息。

public class Archer extends Unit {    
    public Archer(World world, Location location, Player owner) {
        super(new Attack(1, 6, 1), new Defense(1, 4, 2), 10, 4, location, owner);
    }
}

public class Lancer extends Unit {    
    public Lancer(World world, Location location, Player owner){
        super(new Attack(2, 2, 5), new Defense(3,2,3), 15, 5, location, owner);
    }
}

public class Rider extends Unit{    
    public Rider(World world, Location location, Player owner) {
        super(new Attack(3, 2, 4), new Defense(2,4,1), 15, 7, location, owner);
    }
}

現在出現了問題。 我希望能夠使用不同的精靈打印這些單元,因此我制作了具有每個精靈的ArcherUI,LancerUI和RiderUI類。 但是我如何告訴程序打印例如Archer?

一個解決方案可能是為每個Unit做一個getClass(),但這不是一個好方法,因為我想要一個好的面向對象的設計。

我想到的另一個選擇是像這樣為單位添加名稱:

public class Archer extends Unit {  
    String name = "archer";
    public Archer(World world, Location location, Player owner) {
        super(new Attack(1, 6, 1), new Defense(1, 4, 2), 10, 4, location, owner);
    }
}

但是比較此String與制作getClass()相同。 我能做什么? 有什么主意嗎 或任何模式?

如果這三個類之間沒有真正的區別 (除了用於初始化它們的值之外),我認為您應該考慮用UnitType枚舉替換它們,這將(作為副作用)解決您面臨的問題。

enum UnitType {
    ARCHER(new Attack(1, 6, 1), new Defense(1, 4, 2), 10, 4),
    LANCER(new Attack(...), ...)
    RIDER(...)

    private final Attack attack;
    private final Defense defense;


    UnitType(Attack attack, Defence defence, firstValue, secondValue) {
        this.attack = attack;
        ......
    }
}

然后,您可以使用Unit類,而無需子類:

class Unit {
    Unit(UnitType unitType, Location location, Player owner) {
        ....
    }

    public UnitType getType() {
        return unitType;
    }
}

這使您可以在不定義新類的情況下添加單元類型,並使您可以直接區分不同的單元。

我認為,您應該采用這個想法

在單元類中,您可以構建新的方法和屬性,例如

Public Class Unit {
    //......Your previous design

    //Solve with something like this
    //the path of sprite
    String spriteFolder = "";

    //Your main constructor, content still Your previous design
    //add new parameter to constructor, "sprite"
    Public Unit(Attack, Defense, 15, 5, location, owner,        sprite          ){
        //......Your previous design
        spriteFolder = sprite;
    }

    //this is the solution
    public getSpriteFolder(){
        return spriteFolder
    }

}

您可以調用繼承

//this your inheritance system look like
public class Archer extends Unit {    
    public Archer(World world, Location location, Player owner) {
        super(new Attack(1, 6, 1), new Defense(1, 4, 2), 10, 4, location, owner,   "archer/sprite"         );
    }
}

public class Lancer extends Unit {    
    public Lancer(World world, Location location, Player owner){
        super(new Attack(2, 2, 5), new Defense(3,2,3), 15, 5, location, owner,   "lancer/sprite"         );
    }
}

public class Rider extends Unit{    
    public Rider(World world, Location location, Player owner) {
        super(new Attack(3, 2, 4), new Defense(2,4,1), 15, 7, location, owner,   "rider/sprite"         );
    }
}

你可以叫它畫

Archer a = new Archer();
Lancer l = new Lancer();
Rider r = new Rider();

a.getSpriteFolder();
l.getSpriteFolder();
r.getSpriteFolder();

或者如果您將其實例化為數組並想繪制

Unit[] myUnit = new Unit[3];
Unit[0] = new Archer();
Unit[1] = new Lancer();
Unit[2] = new Rider();

for (int i=0; i < Unit.length() ; i++){
    Unit[i].getSpriteFolder();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM