繁体   English   中英

Java 调用继承方法总是返回 null

[英]Java calling inherited method always returns null

我有一个名为 WallTile 的 class

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

public class WallTile extends Tile
{
    private int x;
    private int y;
    private int id=1;
    private ImageIcon image;
    private String[] flags=new String[]{"[IMPASSABLE]", "[SIGHT_BLOCKER]"};
    public WallTile(int x, int y)
    {
        this.x=x;
        this.y=y;
        if((int)(Math.random()*2)==0){
            this.image=Sprites.WALLTILE1_SP;
        }
        else{
            this.image=Sprites.WALLTILE2_SP;
        }
    }

}

继承自 class Tile

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

public class Tile
{
    private int x;
    private int y;
    private int id;
    private ImageIcon image;
    private String[] flags;

    public int getX(){
        return(this.x);
    }
    public int getY(){
        return(this.y);
    }
    public int getId(){
        return(this.id);
    }
    public ImageIcon getImage(){
        //System.out.println(this.image);
        return(this.image);
    }
    public String[] getFlags(){
        return(this.flags);
    }
    public boolean testFlag(String test){//returns true if flag is not present
        return(java.util.Arrays.asList(this.flags).indexOf(test)==-1);
    }
}

创建 wallTile 的实例并调用 Tile 中定义的方法总是返回 null 例如:

WallTile wall = new WallTile(3,7);
System.out.println(wall.getImage());//returns null

但是,如果我从 Tile 复制其中一个函数并将其粘贴到 WallTile 中,则 function 会返回正确的值。

是否可以使用 WallTile 实例调用 tile 中定义的函数,而无需将 tile 中定义的所有函数复制到 WallTile?

简单的修复。 Tile

public class Tile
{
    ...
    protected ImageIcon image;
    ^^^^^^^^^

image更改为protected

然后在WallTile中删除image的声明,它遮蔽了Tile中的声明。 protected将授予WallTile对其image成员的访问权限。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

public class Tile
{
    private int x;
    private int y;
    private int id;
    private ImageIcon image;
    private String[] flags;
    
    public Tile(int x, int y, int id, ImageIcon image, String[] flags){
        this.x=x;
        this.y=y;
        this.id=id;
        this.image=image;
        this.flags=flags;
    }
    
    public int getX(){
        return(this.x);
    }
    public int getY(){
        return(this.y);
    }
    public int getId(){
        return(this.id);
    }
    public ImageIcon getImage(){
        //System.out.println(this.image);
        return(this.image);
    }
    public String[] getFlags(){
        return(this.flags);
    }
    public boolean testFlag(String test){//returns true if flag is not present
        return(java.util.Arrays.asList(this.flags).indexOf(test)==-1);
    }
}

墙砖

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

public class WallTile extends Tile
{
    private int x;
    private int y;
    private int id=1;
    private ImageIcon image;
    private String[] flags;
    
    public WallTile(int x, int y)
    {
        super(x,y,1,Sprites.WALLTILE1_SP,new String[]{"[IMPASSABLE]", "[SIGHT_BLOCKER]"});
        this.x=x;
        this.y=y;
    }

}

暂无
暂无

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

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