Map<Integer, String> map = new TreeMap<Integer, String>();
// Add Items to the TreeMap
map.put(new Integer(8), "Eight");
map.put(new Integer(9), "Nine");
map.put(new Integer(1), "One");
map.put(new Integer(4), "Four");
map.put(new Integer(10), "Ten");
map.put(new Integer(5), "Five");
map.put(new Integer(6), "Six");
map.put(new Integer(2), "Two");
map.put(new Integer(3), "Three");
map.put(new Integer(7), "Seven");
keys = map.keySet();
for (Iterator i = keys.iterator(); i.hasNext();) {
Integer key = (Integer) i.next();
String value = (String) map.get(key);
System.out.println(key + " = " + value);
}
Run Code Online (Sandbox Code Playgroud)
我试图完成的是调用特定类的接口.
我使用Enum来填充.class并获取该类的接口.
那么我怎样才能返回界面呢?
如果可能的话,我想避免反思.
提前致谢.
public interface GameInterface {
void start();
void sop();
}
public enum Game{
MINESWEEPER(MineSweeper.class),
MARIO(Mario.class);
private Class c;
public Game(Class c) {
this.c = c;
}
public GameInterface getGameInterface() {
// return Interface of the class
// So I can call for instance MINESWEEPER.getGameInterface().start()
// At this momement I use return:
// ((GamemodeInterface)this.c.getDeclaredMethod("getInstance", new Class[0]).invoke(null, new Object[0]));
// *MineSweeper and Mario are Singleton, thats why getInstance
}
}
Run Code Online (Sandbox Code Playgroud)
澄清:主要目标是在MineSweeper和Mario类中访问Start()和Stop()方法.
用法应该是这样的:MINESWEEPER.getGameInterface().start()但是在这一刻我不知道一个可靠的解决方案让接口知道.class.
情况
我想要实现的是将true/false存储为Integer/Long中的一个位.问题是如果某个位为1或0,我无法解决.
码
public class Test
{
private static long unlocked = 0;
public static void main(String[] args)
{
setUnlocked(1);
setUnlocked(2);
setUnlocked(3);
System.out.println(isUnlocked(2);
}
public static void setUnlocked(int id)
{
unlocked += Math.pow(2, id);
}
public static boolean isUnlocked(int id)
{
// ???
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
与上面的测试用例一样,它将产生以下位序列:1110 = 14.
编辑
第二个问题:
public static void setUnlocked(int id)
{
unlocked |= 1 << id;
}
Run Code Online (Sandbox Code Playgroud)
至
public static void setUnlocked(int id, boolean set)
{
}
Run Code Online (Sandbox Code Playgroud)
这将给出在给定位置将该位设置为0或1的选项.
但是我怎样才能做到这一点?