2 回答

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;

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();
}
添加回答
舉報