【问题标题】:Java calling inherited method always returns nullJava调用继承的方法总是返回null
【发布时间】:2021-05-03 22:51:27
【问题描述】:

我有一个名为 WallTile 的类

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

}

继承自 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 中,该函数会返回正确的值。

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

【问题讨论】:

  • 您在 WallTile 构造函数中设置了图像。不幸的是,您已经遮蔽了 Tile 图像,因此在 Tile 中定义的 getImage() 获取的是 Til3.image 而不是 WallTile.image。
  • 尝试添加此代码:Problem Source
  • 重新定义 Tile 以允许您写入 Tile.image 并从 WallTile 中删除 image 变量。
  • 要么覆盖getImage(),要么在超类中设置字段,而不是在子类中创建字段。在我看来,后一种选择更好。
  • 这是一个旁白,但如果你在父子节点上都声明了属性,那么对象将同时拥有这两个属性,并且调用超级方法将与与子节点方法不同的值进行交互。除非这是预期目标,否则最好不要重新声明 WallTile 上的属性,在父级提供子级可以使用的 getter 方法或给属性适当的访问修饰符(默认,大概),以便子级可以访问它们它有自己的方法。

标签: java class inheritance methods


【解决方案1】:

平铺

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

}

【讨论】:

    【解决方案2】:

    简单的修复。在Tile

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

    image 更改为protected

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-10-19
      • 2020-08-16
      • 1970-01-01
      • 2017-03-26
      • 1970-01-01
      • 1970-01-01
      • 2016-07-10
      相关资源
      最近更新 更多