如何在另一个类中有一个不同名称的类?

  • 本文关键字:有一个 另一个 java class
  • 更新时间 :
  • 英文 :


我有一个名为Playership的类和另一个名为Game的类。我想Playership班级Game以玩家的名字命名。我不知道该怎么做。

您要做的是这样的:

class Playership {
public Playership() {
System.out.println("This is a playership, please be careful!");
}
}
class Game {
private Playership playership;
public Game() {
System.out.println("Starting the game...");
this.playership = new Playership();
System.out.println("I have a playership now.");
}
}
public class MyClass {
public static void main(String args[]) {
Game game = new Game();
System.out.println("The end of the Game");
}
}

输出将是

Starting the game...
This is a playership, please be careful!
I have a playership now.
The end of the Game

相关内容

最新更新