没有演员的不同阶级的arraylist?

Yar*_*sef 1 java arraylist

让我先说,我DID搜索几个小时,但没有找到我需要的确切内容.

我需要运行类(应用程序)数组并使用当前应用程序的变量和方法.

public class App {
   public final Integer number = 0;
}

public class Player extends App {
   public final Integer number = 1;
}

public class Navigation extends App {
   public final Integer number = 2;
}

public class Phone extends App {
   public final Integer number = 3;
}


private ArrayList<App> apps = new ArrayList<App>();

apps.add(new Player());
apps.add(new Navigation());
apps.add(new Phone());
Run Code Online (Sandbox Code Playgroud)

如果我做

apps.get(0).number //  = 0
Run Code Online (Sandbox Code Playgroud)

我得到父母(app)的号码

如果我把它投给玩家

(Player) apps.get(0).number //  = 1
Run Code Online (Sandbox Code Playgroud)

我得到了理想的结果

现在,我想在应用程序的多个位置运行所有N个应用程序(可能超过3个),但我不想这样做:

if (gun instanceof Player)
{
   Player p = (Player) apps.get(0);

}else if .... N times... in N places
Run Code Online (Sandbox Code Playgroud)

我也不想让应用程序成为界面,因为我需要应用程序默认值.

如果你有更好的主意,没有必要使用arraylist.

提前致谢.

EdH*_*EdH 5

使用方法而不是最终变量.App.getNumber(),然后每个类都可以覆盖该方法以返回适当的值.每个类都可以返回相同的常量,但将常量定义为private,并确保使用getter方法