简体   繁体   中英

Java calling inherited method always returns null

I have a class called 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;
        }
    }

}

which inherits from 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);
    }
}

creating an instance of wallTile and calling a method defined in Tile always returns null ex:

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

However, if I copy one of the functions from Tile and paste it into WallTile, that function returns the correct value.

Is there any to call the functions defined in tile with an instance of WallTile without having to copy all of the functions defined in tile over to WallTile?

Simple fix. In Tile

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

change image to protected .

Then in WallTile remove the declaration of image , which was shadowing the declaration in Tile . protected will grant WallTile access to its image member.

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

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;
    
    public WallTile(int x, int y)
    {
        super(x,y,1,Sprites.WALLTILE1_SP,new String[]{"[IMPASSABLE]", "[SIGHT_BLOCKER]"});
        this.x=x;
        this.y=y;
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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