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

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么我需要向 Java 接口添加方法,以便在 Junit 單元測試中訪問它?

為什么我需要向 Java 接口添加方法,以便在 Junit 單元測試中訪問它?

Helenr 2023-11-01 17:04:04
我主要是一名 C#、.NET 開發(fā)人員,習(xí)慣 C# 中的接口和 TDD。C# 中的接口主要定義實(shí)現(xiàn)時的契約。Java 中的用法似乎略有不同。特別是,我遇到的每個項(xiàng)目似乎都實(shí)現(xiàn)了用于訪問應(yīng)用程序的基本接口,就好像任何 Java 應(yīng)用程序都需要使用接口一樣。我想我缺少一些基本的理解,所以我真的很感激任何關(guān)于我可以閱讀的好入門書的提示。例如,我有一個如下所示的測試(在我的解決方案中的單獨(dú)“測試”文件夾中):Tests.javapackage com.dius.bowling;class DiusBowlingGameTest {    private BowlingGame bowlingGame;    @BeforeEach    void setUp() {        this.bowlingGame = new DiusBowlingGame();        this.bowlingGame.startGame();    }為了能夠訪問,this.bowlingGame.startGame();我需要將該方法添加到接口中。為什么?我不知道 Java 和 C#/.NET 之間似乎存在差異?界面package com.dius.bowling;/** * Interface for a bowling game. */public interface BowlingGame {    /**     * roll method specifying how many pins have been knocked down     * @param noOfPins no. of pins knocked down     */    void roll(int noOfPins);    /**     * get player's current score     * @return player's current score     */    int score();    void startGame();}Dius保齡球游戲package com.dius.bowling;/** * Scoring system for tenpin bowls */public class DiusBowlingGame implements BowlingGame {    ArrayList<BowlingFrame> gameFrames = new ArrayList<BowlingFrame>();    public void roll (int noOfPins) {       /* Implementation */    }    }    /**     * Activate the 1st frame of the game     */    public void startGame() {        advanceFrame();    };
查看完整描述

2 回答

?
慕神8447489

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

如果它不在接口中,并且您將引用存儲在接口類型的變量中,那么編譯器如何知道該方法存在?


一般來說,分配給變量的值可以是 的任何實(shí)現(xiàn)BowlingGame。除非該方法位于接口上,否則不需要這些類實(shí)現(xiàn)該方法。


為了避免將方法添加到接口中,請將變量類型更改為DiusBowlingGame,或在方法中使用局部變量setUp:


DiusBowlingGame bowlingGame = new DiusBowlingGame();

bowlingGame.startGame();

this.bowlingGame = bowlingGame;


查看完整回答
反對 回復(fù) 2023-11-01
?
陪伴而非守候

TA貢獻(xiàn)1757條經(jīng)驗(yàn) 獲得超8個贊

據(jù)我所知,接口在 C# 和 Java 中的工作方式是相同的。唯一的區(qū)別是,在 C# 中,通常以“I”開頭來命名接口,并且在 C# 中,類和接口都使用運(yùn)算符,而在:Java 中,關(guān)鍵字implements用于接口,關(guān)鍵字extends用于類。您的代碼不需要接口,它也可以這樣完美地工作:


package com.dius.bowling;


class DiusBowlingGameTest {


private DiusBowlingGame bowlingGame;


@BeforeEach

void setUp() {

    this.bowlingGame = new DiusBowlingGame();

    this.bowlingGame.startGame();

}


查看完整回答
反對 回復(fù) 2023-11-01
  • 2 回答
  • 0 關(guān)注
  • 327 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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