第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何訪問由構(gòu)造函數(shù)生成的對(duì)象變量

如何訪問由構(gòu)造函數(shù)生成的對(duì)象變量

米琪卡哇伊 2022-10-12 15:29:40
我有 2 個(gè)按鈕,1 個(gè)使用 round1rock 中的構(gòu)造函數(shù)類,而其他 1 個(gè)嘗試訪問這些參數(shù)。我的代碼有什么問題?構(gòu)造函數(shù)類public ROCK(int hp, int stamina, int attack, int speed, String type){   this.hp=hp;     this.stamina= stamina;   this.attack= attack;   this.speed = speed;   this.type = type;}2個(gè)按鈕:private void continueRound1 (ActionEvent event){       ROCK round1Rock= new ROCK( 500, 100, 100, 100, "Metamorphic");    }    private void Attack (ActionEvent event){        round1Rock.hp = 12;    }我如何訪問以前制作的對(duì)象?
查看完整描述

4 回答

?
小怪獸愛吃肉

TA貢獻(xiàn)1852條經(jīng)驗(yàn) 獲得超1個(gè)贊

當(dāng)你定義


private void continueRound1 (ActionEvent event){

   ROCK round1Rock= new ROCK( 500, 100, 100, 100, "Metamorphic");

}

您ROCK round1Rock只是為函數(shù)定義continueRound1。要Attack訪問該對(duì)象,您需要round1Rock在類級(jí)別上進(jìn)行定義。


嘗試:


ROCK round1Rock = null;


private void continueRound1 (ActionEvent event){

  round1Rock= new ROCK( 500, 100, 100, 100, "Metamorphic");

}

private void Attack (ActionEvent event){

    round1Rock.hp = 12;


}


查看完整回答
反對(duì) 回復(fù) 2022-10-12
?
鴻蒙傳說

TA貢獻(xiàn)1865條經(jīng)驗(yàn) 獲得超7個(gè)贊

在類級(jí)別定義round1Rock,


class someclass


{


   private ROCK round1Rock;



          -----


   private void continueRound1 (ActionEvent event){

       round1Rock= new ROCK( 500, 100, 100, 100, "Metamorphic");

    }



    private void Attack (ActionEvent event){

        round1Rock.hp = 12;


    }


           ------

}


查看完整回答
反對(duì) 回復(fù) 2022-10-12
?
莫回?zé)o

TA貢獻(xiàn)1865條經(jīng)驗(yàn) 獲得超7個(gè)贊

而不是在 continueRound1 方法中創(chuàng)建一個(gè)新的 Rock 對(duì)象。您可以在類范圍內(nèi)創(chuàng)建新的 Rock 對(duì)象并設(shè)置私有訪問。這將解決您的問題。

附加提示:每次單擊按鈕時(shí),您都會(huì)創(chuàng)建一個(gè)新對(duì)象。如果我編寫程序無限期地單擊按鈕,這將導(dǎo)致OutOfMemoryError 。

以下是我避免此問題的見解:

  1. 我假設(shè)每個(gè)客戶都需要新的搖滾樂。因此,在客戶端類中創(chuàng)建一個(gè) Empty Rock 對(duì)象。

  2. 在您的客戶端構(gòu)造函數(shù)中,您可以使用巖石類型的默認(rèn)值初始化巖石對(duì)象。getDefaultRockForType 將幫助您創(chuàng)建任意數(shù)量的巖石類型。因此,我們將客戶端類中帶有一些值的 Rock 對(duì)象的實(shí)現(xiàn)細(xì)節(jié)隱藏為 Rock 類中的標(biāo)準(zhǔn)化值。

這是我的代碼片段:

Class Client {

       private Rock round1Rock =  new Rock();

       Client() {

          round1Rock = round1Rock.getDefaultRockForType("Metamorphic"); 

       }

       private void continueRound1 (ActionEvent event){

            round1Rock= round1Rock.getDefaultRockForType("Metamorphic");

       }


    private void Attack (ActionEvent event){

        round1Rock.setHp(12);


    }

}

在您的 Rock 類中,您可以提供由您的巖石類型決定的默認(rèn)值。


類搖滾:


public Rock getDefaultRockForType(String type){

   if(type.equals("Metamorphic")){

         this.hp=500;  

         this.stamina= 100;

         this.attack= 100;

         this.speed = 100;

         this.type = type;

   }

}


查看完整回答
反對(duì) 回復(fù) 2022-10-12
?
富國滬深

TA貢獻(xiàn)1790條經(jīng)驗(yàn) 獲得超9個(gè)贊

首先像這樣聲明 ROCK 的實(shí)例是全局的


private ROCK round1Rock = null;

private void continueRound1 (ActionEvent event){

       round1Rock= new ROCK( 500, 100, 100, 100, "Metamorphic");

    }


    private void Attack (ActionEvent event){

        round1Rock.hp = 12;


    }

在您的 Attack 動(dòng)作偵聽器中,您可能無法訪問變量 hp,因?yàn)樗赡苁撬接械?,因此最好?Rock 類創(chuàng)建 setter 和 getter 方法,然后使用它。


搖滾類:


public ROCK(int hp, int stamina, int attack, int speed, String type){

   this.hp=hp;  

   this.stamina= stamina;

   this.attack= attack;

   this.speed = speed;

   this.type = type;

}


public void setHP(int hp){

   this.hp = hp

}


public void getHP(){

  return hp;

}

然后在你的其他班級(jí)使用這個(gè):


 private void Attack (ActionEvent event){

            round1Rock.setHP(12); //this will update the value


  }


查看完整回答
反對(duì) 回復(fù) 2022-10-12
  • 4 回答
  • 0 關(guān)注
  • 171 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)