簡體   English   中英

Netbeans變量繼承問題Java

[英]Netbeans Variable Inheritance Issue Java

因此,我到處尋找該錯誤的答案,但沒有確切的結果。 我是Java和Netbeans的新手,到目前為止,我所做的一切都是在BlueJ上進行的。 當我將一個類擴展到另一個類時,變量和方法應該被繼承,但是我不斷遇到變量未找到的錯誤。 這是超類:

package Runner2D;
import java.awt.*;
public class Block {
    protected boolean power;
    public int width;
    public int height;
    public int xPos;
    public int yPos;
    public boolean hit;
    public Block( int x, int y ){
        xPos = x;
        yPos = y;
        width = 30;
        height = 30;
        power = false;
    } // end Block
    public Block( ){
        xPos = ( int ) ( Math.random() * 501 );
        yPos = ( int ) ( Math.random() * 501 );
        width = 40;
        height = 40;
    } // end Block
    public void drawSquare( Graphics2D g2 ){
        g2.fillRect( xPos, yPos, width, height );
    } // end 
} // end Block

這是子類:

package runner2d;    
import java.awt.*;    
public class Invincibility extends Block{    
    public Invincibility( int x, int y ){    
        super( x, y );
        power = true;
        hit = false;
    } // end Invinsibility
    public void setHit( boolean b ){
        hit = b;
    } // end setHit
    public void drawSquare( Graphics2D g2 ){
        if ( !hit ) g2.fillRect( xPos, yPos, width, height );
        else xPos = - 40;`enter code here`
    } // end drawSquare
} // end class

確切的錯誤是找不到符號。 這在BlueJ中完全正常。

您的程序包名稱runner2dRunner2D不同。您的類應該在同一程序包下,或者您應該將一個程序導入另一個程序包。

暫無
暫無

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

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