没有花括号的Kotlin课程

时间:2017-12-10 04:04:38

标签: kotlin kotlin-android-extensions kotlin-extension

我注意到我们可以在没有大括号的情况下在Kotlin中创建类,如下所示。

//Example classFile.kt
class Empty

class SecondEmpty

fun firstMethod() {

}

我的问题是,为什么我们需要这样的功能?在哪种情况下我们可以使用这个?

在上面的例子中,我编写了一个名为firstMethod()的方法,如何从其他对象中调用它?

1 个答案:

答案 0 :(得分:2)

What is the purpose of empty class in Kotlin?已经讨论过空类。

关于你的import javax.swing.JOptionPane; public class TicTacToe { public static void main(String[] args) { char gameboard[][] = new char[3][3]; printCurrentState(gameboard); int Turn = 0; int gameon=0; while(gameon<9) { if(Turn==0) { JOptionPane.showMessageDialog(null, "Player 1's turn aka X"); } else { JOptionPane.showMessageDialog(null, "Player 2's turn aka O"); } boolean proceed=true; String yo =JOptionPane.showInputDialog("Enter the row and column"+ "one"+ "after another. Example-for row 1 and comlumn 2 type \"12\""); if(yo.length()!=2) { JOptionPane.showMessageDialog(null, "You did not put the row"+ "and column one after another- Try again"); proceed=false; } if(proceed==true) { String aa= yo.charAt(0)+""; int x = Integer.parseInt(aa)-1; String ba= yo.charAt(1)+""; int y = Integer.parseInt(ba)-1; if((x < 0 || x > 2)||(y<0||y>2)) { JOptionPane.showMessageDialog(null, "Please enter a row and"+ "column that both at least 1 and no bigger than 3. Try again!"); yo =JOptionPane.showInputDialog("Enter the row and column one"+ "after another. Example-for row 1 and comlumn 2 type \"12\""); aa= yo.charAt(0)+""; x = Integer.parseInt(aa)-1; ba= yo.charAt(1)+""; y = Integer.parseInt(ba)-1; } if(gameboard[x][y] == 'X' || gameboard[x][y] == 'O') { JOptionPane.showMessageDialog(null, "That is already taken"+ "by an "+ gameboard[x][y]+". Go again!"); yo =JOptionPane.showInputDialog("Enter the row and column"+ "one after another. Example-for row 1 and comlumn 2 type \"12\""); aa= yo.charAt(0)+""; x = Integer.parseInt(aa)-1; ba= yo.charAt(1)+""; y = Integer.parseInt(ba)-1; } if(Turn== 0) { gameboard[x][y] = 'X'; printCurrentState(gameboard); isHorizontalWin(gameboard); isVerticalWin(gameboard); isDiagnolWin(gameboard); Turn++; } else if(Turn == 1) { gameboard[x][y] = 'O'; printCurrentState(gameboard); isHorizontalWin(gameboard); isVerticalWin(gameboard); isDiagnolWin(gameboard); Turn--; } gameon++; } } if(isHorizontalWin(gameboard)==false&&isVerticalWin(gameboard)==false&&isDiagnolWin(gameboard)==false) { JOptionPane.showMessageDialog(null, "There was no winner this game "); } } public static void printCurrentState(char gameboard[][]) { System.out.println(" COLUMN"); System.out.println(" 1 2 3"); System.out.print(gameboard[0][0] + " | " + gameboard[0][1] + " | " + gameboard[0][2] +" 1 R"+ "\n" + "--|---|--\n" + gameboard[1][0] + " | " + gameboard[1][1] + " | " + gameboard[1][2] +" 2 O"+ "\n" + "--|---|--\n" + gameboard[2][0] + " | " + gameboard[2][1] + " | " + gameboard[2][2] +" 3 W "+"\n"); System.out.println(); } public static boolean isHorizontalWin(char[][] gameboard) { int tally[][]= new int[3][3]; for(int r = 0; r < 3; r++) { for(int c = 0; c < 3; r++) { if(gameboard[r][c]=='O') { tally[r][c]=10; } else if(gameboard[r][c]=='X') { tally[r][c]=1; } else { tally[r][c]=0; } } } int c=0; for(int m = 0; m < 3; m++) { c++; int cool = 0; for(int n = 0; n < 3; n++) { cool += tally[m][n]; if(cool == 30) { JOptionPane.showMessageDialog(null, "Player 2(O) is the winner with a horizontal win in row " +c); System.exit(0); return true; } else if(cool==3) { JOptionPane.showMessageDialog(null, "Player 1(X) is the winner with a horizontal win in row " + c); System.exit(0); return true; } } } return false; } public static boolean isVerticalWin(char[][] gameboard) { int tally[][]= new int[3][3]; for(int r = 0; r < 3; r++) { for(int c = 0; c < 3; r++) { if(gameboard[r][c]=='O') { tally[r][c]=10; } else if(gameboard[r][c]=='X') { tally[r][c]=1; } else { tally[r][c]=0; } } } int r=0; for(int m = 0; m < 3; m++) { r++; int cool = 0; for(int n = 0; n < 3; n++) { cool += tally[n][m]; if(cool == 30) { JOptionPane.showMessageDialog(null, "Player 2(O) is the winner with a vertical win in column " +r); System.exit(0); return true; } else if(cool==3) { JOptionPane.showMessageDialog(null, "Player 1(X) is the winner with a vertical win in column " + r); System.exit(0); return true; } } } return false; } public static boolean isDiagnolWin(char[][] gameboard) { if((gameboard[0][0]=='O'&&gameboard[1][1]=='O'&&gameboard[2][2]=='O')||(gameboard[0][2]=='O'&&gameboard[1][1]=='O'&&gameboard[3][1]=='O')) { JOptionPane.showMessageDialog(null, "Player 2(O) is the winner with a diagonal win" ); System.exit(0); return true; } if((gameboard[0][0]=='X'&&gameboard[1][1]=='X'&&gameboard[2][2]=='X')||(gameboard[0][2]=='X'&&gameboard[1][1]=='X'&&gameboard[2][0]=='X')) { JOptionPane.showMessageDialog(null, "Player 1(X) is the winner with a diagonal win" ); System.exit(0); return true; } return false; } :在Kotlin,我们有所谓的顶级功能。这些可以在没有封闭类的任何文件中定义。另一个例子是firstMethod,它在大多数情况下被定义为顶级。

如何调用顶级函数?

您只需将该功能导入其他文件并调用即可。例如,如果在main(包com.x)中定义firstMethod,则可以在其他Kotlin文件中导入com/x/Example.kt并调用该方法。

对于Java,重要的是要知道顶级函数作为com.x.firstMethod成员编译成一个类。至于上面的例子,你可以从Java调用static