简体   繁体   English

多态,找到get的类型

[英]Polymorphism, find the type of a get

I have a question about polymorphism. 我有一个关于多态的问题。

I have a parent Object Case: 我有一个父对象案例:

public class Case {

//a constructor
//some attributes
//some getters and setters

}

Then a child class: 然后是一个子班:

public class CaseNorm extends Case {
public CaseNorm(//some attributes) {
        super(//some attributes);
    }
}

In my parent class Object, I have an attribute: 在我的父类对象中,我有一个属性:

private Case nextCase;

But next case can have a type, caseNorm or other child class from Case. 但是下一个case可以具有Case的类型,caseNorm或其他子类。

Then in a main class I have to get the case and set this in a variable with the correct type. 然后在主类中,我必须获取大小写并将其设置为具有正确类型的变量。 For example 例如

 public class Labyrinthe {

    public static void main(String[] args) {
        Case case1 = new Case(//some attributes);
//set Next Case, who can be a type of caseNorm, caseHyp, caseHym, etc.
    }

}

I want now to get case1, but with the correct type. 我现在想获取case1,但类型正确。 How can I do that? 我怎样才能做到这一点?

EDIT: 编辑:

I try to explain my conception, to better understand my question. 我试图解释我的概念,以更好地理解我的问题。

I have a case class, that a parent case. 我有一个案例类,一个父案例。 This class model a square. 该类模型为正方形。 Then it has child who specialises this square. 然后有孩子专门研究这个广场。 CaseNorm (for a normal square, no color, etc), another can be caseHyp (for a red square with a border), etc. CaseNorm(用于普通正方形,无颜色等),另一个可以是caseHyp(用于带有边框的红色正方形)等。

Each case has a number and a position. 每个案例都有一个数字和位置。 I want to get the next case, if the actual case is 1, nextCase will be the case 2 (who can be a CaseNorm, or a CaseHyp, etc). 我想得到下一个案例,如果实际案例是1,nextCase将是案例2(可以是CaseNorm或CaseHyp等)。 I need to know in my application, which type is nextCase. 我需要在我的应用程序中知道哪种类型是nextCase。

EDIT2: 编辑2:

It seems that I'm again not clear. 看来我还是不清楚。 I try to be more. 我尝试变得更多。

public class Case {

    private int numeroCase;
    private int posX;
    private int posY;
    private int taille;
    private int ligne;
    private int colonne;

    public Case(int numeroCase, int posX, int posY, int taille, int nbLignes, int nbColonnes) {
        this.numeroCase = numeroCase;
        this.posX = posX;
        this.posY = posY;
        this.taille = taille;
        this.ligne = numeroCase / nbColonnes+1;
        this.colonne = numeroCase % nbColonnes+1;
    }

    public int getNumeroCase() {
        return numeroCase;
    }

    public void setNumeroCase(int numeroCase) {
        this.numeroCase = numeroCase;
    }

    public int getPosX() {
        return posX;
    }

    public void setPosX(int posX) {
        this.posX = posX;
    }

    public int getPosY() {
        return posY;
    }

    public void setPosY(int posY) {
        this.posY = posY;
    }

    public int getTaille() {
        return taille;
    }

    public void setTaille(int taille) {
        this.taille = taille;
    }

    public int getLigne() {
        return ligne;
    }

    public void setLigne(int ligne) {
        this.ligne = ligne;
    }

    public int getColonne() {
        return colonne;
    }

    public void setColonne(int colonne) {
        this.colonne = colonne;
    }
}

Subclass: 子类:

public class CaseMur extends Case {
    private Color color = Color.BLACK;
     private Color BorderColor = Color.WHITE;

    public CaseMur(int numeroCase, int posX, int posY, int taille) {
        super(numeroCase, posX, posY, taille);
    }

    public Color getColor() {
        return color;
    }

    public void setColor(Color color) {
        this.color = color;
    }

    public Color getBorderColor() {
        return BorderColor;
    }

    public void setBorderColor(Color BorderColor) {
        this.BorderColor = BorderColor;
    }

}

CLASS TO SCREEN: 萤幕上的课程:

public class Terrain {

    private int nbColonnes;
    private int nbLignes;
    private int largeurCarre;
    private ArrayList<Case> cases = new ArrayList<Case>();
    private Case caseActuelle;
    private Object caseSuivanteDroite; //next right case
    private Object caseSuivanteGauche; //next left case
    private Object caseSuivanteHaut; //next up case
    private Object caseSuivanteBas; //next bottom case

    public Terrain() {
        constructionStatiqueTerrain();
    }

    private void constructionStatiqueTerrain() {
        this.nbColonnes = 7;
        this.nbLignes = 5;
        this.largeurCarre = 40;

        construitCases();
        caseDebut(1);
        caseFin(24);
        caseMur(12);
//        caseMur(17);caseActuelle.getColonne()
        caseMur(22);
    }

    public Object getCaseSuivanteDroite() {
        return caseSuivanteDroite;
    }

    public void setCaseSuivanteDroite(Case caseActuelle) {
        if (caseActuelle.getColonne() < nbColonnes && !cases.get(caseActuelle.getNumeroCase() + 1).getClass().equals(CaseMur.class)) {
            this.caseSuivanteDroite = cases.get(caseActuelle.getNumeroCase() + 1);
        } else {
            this.caseSuivanteDroite = null;
        }
    }

    public Object getCaseSuivanteGauche() {
        return caseSuivanteGauche;
    }

    public void setCaseSuivanteGauche(Case caseSuivanteGauche) {
        if (caseActuelle.getColonne() != 1 && !cases.get(caseActuelle.getNumeroCase() - 1).getClass().equals(CaseMur.class)) {
            this.caseSuivanteGauche = cases.get(caseActuelle.getNumeroCase() - 1);
        } else {
            this.caseSuivanteGauche = null;
        }
    }

    public Object getCaseSuivanteHaut() {
        return caseSuivanteHaut;
    }

    public void setCaseSuivanteHaut(Case caseSuivanteHaut) {
        if (caseActuelle.getLigne() != 1 && !cases.get(caseActuelle.getNumeroCase() - nbColonnes).getClass().equals(CaseMur.class)) {
            this.caseSuivanteHaut = cases.get(caseActuelle.getNumeroCase() - nbColonnes);
        } else {
            this.caseSuivanteHaut = null;
        }
    }

    public Object getCaseSuivanteBas() {
        return caseSuivanteBas;
    }

    public void setCaseSuivanteBas(Case caseSuivanteBas) {
        if (caseActuelle.getLigne() < nbLignes && !cases.get(caseActuelle.getNumeroCase() + nbColonnes).getClass().equals(CaseMur.class)) {
            this.caseSuivanteBas = cases.get(caseActuelle.getNumeroCase() + nbColonnes);
        } else {
            this.caseSuivanteBas = null;
        }
    }

    public void setCaseActuelle(int numCase) {
        this.caseActuelle = new Case(numCase, cases.get(numCase).getPosX(), cases.get(numCase).getPosY(), largeurCarre, nbLignes, nbColonnes);
        setCaseSuivanteDroite(caseActuelle);
        setCaseSuivanteGauche(caseActuelle);
        setCaseSuivanteHaut(caseActuelle);
        setCaseSuivanteBas(caseActuelle);
    }

    public Case getCaseActuelle() {
        return this.caseActuelle;
    }

    public void caseNormale(int numCase) {
        numCase--;
        cases.set(numCase, new CaseNormale(numCase, cases.get(numCase).getPosX(), cases.get(numCase).getPosY(), largeurCarre));
    }

    private void caseMur(int numCase) {
        numCase--;
        cases.set(numCase, new CaseMur(numCase, cases.get(numCase).getPosX(), cases.get(numCase).getPosY(), largeurCarre));
    }

    public void caseDebut(int numCase) {
        numCase--;
        cases.set(numCase, new CaseDebut(numCase, cases.get(numCase).getPosX(), cases.get(numCase).getPosY(), largeurCarre));
        this.caseActuelle = new Case(numCase, cases.get(numCase).getPosX(), cases.get(numCase).getPosY(), largeurCarre, nbLignes, nbColonnes);
        this.caseSuivanteDroite = cases.get(numCase + 1);
    }

    private void caseFin(int numCase) {
        numCase--;
        cases.set(numCase, new CaseFin(numCase, cases.get(numCase).getPosX(), cases.get(numCase).getPosY(), largeurCarre));
    }

    private void construitCases() {
        int numeroCase = 1;
        for (int i = 0; i < nbLignes; i++) {
            for (int j = 0; j < nbColonnes; j++) {
                addCase(new CaseNormale(numeroCase, j * largeurCarre, i * largeurCarre, largeurCarre));
                numeroCase++;
            }
        }
    }

    public ArrayList<Case> getCases() {
        return cases;
    }

    public void addCase(Case case1) {
        cases.add(case1);
    }

    public int getNbColonnes() {
        return nbColonnes;
    }

    public void setNbColonnes(int nbColonnes) {
        this.nbColonnes = nbColonnes;
    }

    public int getNbLignes() {
        return nbLignes;
    }

    public void setNbLignes(int nbLignes) {
        this.nbLignes = nbLignes;
    }

    public int getLargeurCarre() {
        return largeurCarre;
    }

    public void setLargeurCarre(int largeurCarre) {
        this.largeurCarre = largeurCarre;
    }

}

In the getCaseSuivanteGauche method, I have to return a Object case, I want to return a case and the use so: 在getCaseSuivanteGauche方法中,我必须返回一个Object案例,我想返回一个案例并按如下方式使用:

Main class: 主班:

Terrain terrain = new Terrain();
terrain.getCaseSuivanteBas().getColor();

but getCaseSuivantBas return an Object or a Case if I change Object by case, and then I cannot catch the CaseNormal methods. 但是如果我逐个更改对象,则getCaseSuivantBas返回一个对象或一个Case,然后我将无法捕获CaseNormal方法。

Case is already a "correct" type. Case已经是“正确”类型。

You need to use the constructor of the concrete type that you want when creating an object like new CaseNorm(attribute1, attribute2, attribute3) . 创建像new CaseNorm(attribute1, attribute2, attribute3)类的对象时,需要使用所需的具体类型的构造函数。 You don't need to know the type CaseNorm later when using the object. 以后使用该对象时,您无需知道CaseNorm类型。 So, you can assign the object to a variable with type Case and you can pass the object to code that does not know what kind of Case the object is. 因此,您可以将对象分配给Case类型的变量,并且可以将对象传递给不知道对象是哪种Case的代码。 This is what polymorphism is about. 这就是多态性。 There is a common way to use Case s and the actual implementation of them can vary (by overriding concrete methods or implementing abstract methods). 有一种使用Case的通用方法,并且它们的实际实现可以有所不同(通过覆盖具体方法或实现抽象方法)。

You're not being really clear in your question. 您的问题并不十分清楚。

Given the following class: 给定以下类别:

class Case {
    // some variables
    private Case nextCase;
    public Case(constructorarg1, arg2, arg3, Case theNextCase) {
        // assign arguments to variables
        nextCase = Case;
}

And there are multiple subtypes of Case ( SpecialCase , CoolCase , CaseInARedDress ). 而且有多种Case类型( SpecialCaseCoolCaseCaseInARedDress )。

Your constructor should assign the nextCase... or there should be some setter. 您的构造函数应分配nextCase ...或应该有一些设置器。

The actual type which is being used is not important, as long as it is a subtype of Case . 使用的实际类型并不重要,只要它是Case的子类型即可。 This subtyping allows you to treat any subtype without knowing it's actual type. 此子类型使您可以在不知道其实际类型的情况下对其进行处理。

Are you asking something different? 您有其他疑问吗? If so, please clarify and/or say what error you get / where things go wrong. 如果是这样,请澄清和/或说出您遇到什么错误/哪里出了问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM