2010-01-06 49 views
0

我想覆蓋父類的方法。我成功地完成了。但是當我嘗試訪問一個屬性。我得到以下錯誤1178:嘗試訪問通過靜態類型的引用留下的不可訪問的屬性

1178: Attempted access of inaccessible property left through a reference with static type 

此屬性是公共的,它被定義。下面是我的代碼

package com.objects { 

import flash.display.MovieClip; 
import flash.events.*; 
import flash.display.Stage; 
import flash.geom.Rectangle; 


public class Brick extends MovieClip { 

public var points:Number = 100; 
public var bWidth:Number = 50; 
public var bHeight:Number = 20; 
private var left:Number; 
private var right:Number; 
private var top:Number; 
private var bottom:Number; 
public var ball:Ball; 
private var lastDistance:Number; 
private var engine:Engine; 

public var hit:Boolean; 


public function Brick():void 
{ 
stop(); 
addEventListener(Event.ENTER_FRAME, loop); 
} 

protected function loop(e:Event):void 
{ 
updateArea(); 
checkBall(); 
} 
protected function updateArea():void { 
left = x; 
right = x + bWidth; 
top = y; 
bottom = y + bHeight; 
} 

protected function killSelf():void { 
engine.score += points; 
engine.numberOfBricks -= 1; 
removeEventListener(Event.ENTER_FRAME,loop); 
engine.removeChild(this); 
} 

protected function checkBall():void 
{ 
if(hitTestObject(ball)) 
{ 

if((ball.x + 5) < left) 
{ 
ball.xspeed *= -1; 
ball.x -= ball.xspeed + 10; 
killSelf(); 
} 
else if((ball.x - 5) > right) 
{ 
ball.xspeed *= -1; 
ball.x += ball.xspeed+10; 
killSelf(); 
} 

else if(ball.y > bottom && ball.x > left && ball.x < right) 
{ 
ball.yspeed *= -1; 
ball.y += ball.yspeed+10; 
killSelf(); 

} 
else if(ball.y < top && ball.x > left && ball.x < right) 
{ 
ball.yspeed *= -1; 
ball.y -= ball.yspeed+10; 
killSelf();     
} 

} 
}//End CheckBall 

public function getEngine(engine:Engine):void 
{ 
this.engine = engine; 
} 
public function getBall(ball:Ball):void 
{ 
this.ball = ball; 
} 
} 
} 

這是壓倒一切的類

package com.objects { 

    import flash.display.MovieClip; 
    import flash.events.*; 
    import flash.display.Stage; 
    import flash.geom.Rectangle; 


    public class Brick2 extends Brick { 

     private var armor:Number = 2; 

     public function Brick2():void 
     { 
      gotoAndStop(2); 
     } 

     override protected function checkBall():void 
     { 
      if(hitTestObject(this.ball)) 
      { 
       if(armor <=0){ 

        if((this.ball.x + 5) < left) 
        { 
         this.ball.xspeed *= -1; 
         this.ball.x -= this.ball.xspeed + 10; 
         killSelf(); 
        } 
        else if((this.ball.x - 5) > right) 
        { 
         this.ball.xspeed *= -1; 
         this.ball.x += this.ball.xspeed+10; 
         killSelf(); 
        } 

        else if(this.ball.y > bottom && this.ball.x > left && this.ball.x < right) 
        { 
         this.ball.yspeed *= -1; 
         this.ball.y += this.ball.yspeed+10; 
         killSelf(); 

        } 
        else if(this.ball.y < top && this.ball.x > left && this.ball.x < right) 
        { 
         this.ball.yspeed *= -1; 
         this.ball.y -= this.ball.yspeed+10; 
         killSelf();     
        } 
       }//end armor condition 
       else 
       { 
        --armor 
       } 
      } 
     }//End Checkthis.ball 

    } 
} 

回答

4

在您的基類中,var left被聲明爲私有。試圖從別處訪問它,包括後代類將失敗。

+0

非常感謝。錯誤消息是說在該方法中的另一個變量是原因。我沒有注意到其他的變數 – numerical25 2010-01-06 23:55:04

1

如果你想在子類中使用一個變量,它必須是保護,不私人

私人意味着只有該類可以看到它,受保護意味着類和它的子類。

2

對於這種情況,我發現了一個簡單的解決方法。 讀取屬性時,請使用普通的動作獲取器。 但是,而不是優雅的動作設置器只需指定類似java的制定者。

public function get property():Number { return _property; } 
myCustomNamespace function setProperty(value:Number):void { _property = value; } 
相關問題