Java:你能把Class转换成一个特定的接口吗? [英] Java: can you cast Class into a specific interface?

查看:96
本文介绍了Java:你能把Class转换成一个特定的接口吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个我正在开发的项目,在这个项目中,我想包括开发人员的能力,包括自己的插件,而不必改变整个代码。



这是我迄今为止开发的。
这是插件使用的界面。

  
package com.pennion.pennpad;

public interface action {
void doAction();
}

这是加载插件的主要代码。

 


映射menuMap = new HashMap
map actionCommands = new HashMap();
public void load3rdPartyMenu()throws Exception {
String userHome = System.getProperty(user.home);
String sep = File.getSeparator();
String fileString = userHome + sep +pennion+ sep +pennpad+ sep +plugins+ sep +plugins.conf
文件cfgFile = new File(fileString);
BufferedReader in = new BufferedReader(new InputStreamReader(new FileStreamReader(cfgFile)));
String ln =;
boolean menuFound = false;
while((ln = in.readLine())!= null){
if(!menuFound){
if(ln.equals(//!== Menu!== //)){
menuFound = true;
} else {
menuFound = false;
}
} else {
String pluginName =;
String pluginDescription =;
String KeyMask =;
String [] split = ln.split(||);
pluginName = split [0];
KeyMask = split [1];
pluginDescription = split [2];
ClassLoader pluginLoader = ClassLoader.getClassLoader();
Class c = pluginLoader.loadClass(com.pennion.3rdparty。+ pluginName);
Map keyMap = new HashMap();
String [] kmSplit = KeyMask.split(+);
if(kmSplit [0] .equals(CTRL)){
keyMap.put(ActionEvent,ActionEvent.CTRL_MASK);
} else if(kmSplit [0] .equals(SHIFT)){
keyMap.put(ActionEvent,ActionEvent.SHIFT_MASK);
} else if(kmSplit [0] .equals(ALT)){
keyMap.put(ActionEvent,ActionEvent.ALT_MASK);
} else if(kmSplit [0] .equals(ALT_CTRL)|| kmSplit [0] .equals(CTRL_ALT)){
keyMap.put(ActionEvent,ActionEvent.CTRL_MASK + ActionEvent.ALT_MASK);
} else if(kmSplit [0] .equals(SHIFT_CTRL)|| kmSplit [0] .equals(CTRL_SHIFT)){
keyMap.put(ActionEvent,ActionEvent.CTRL_MASK + ActionEvent.SHIFT_MASK);
} else if(kmSplit [0] .equals(ALT_SHIFT)|| kmSplit [0] .equals(SHIFT_ALT)){
keyMap.put(ActionEvent,ActionEvent.SHIFT_MASK + ActionEvent.ALT_MASK);
}
keyMap.put(KeyBind,getKeyBinding(kmSplit [1]));
this.addMenuItem(Plugin,pluginName,keyMap.get(KeyBind),keyMap.get(ActionEvent),keyMap.get(KeyBind),pluginName,c);
}
}
}
public int getKeyBinding(String k){
if(k.equals(A)){
return KeyEvent。 VK_A;
} else if(k.equals(B)){
return KeyEvent.VK_B;
} else if(k.equals(C)){
return KeyEvent.VK_C;
} else if(k.equals(D)){
return KeyEvent.VK_D;
} else if(k.equals(E)){
return KeyEvent.VK_E;
} else if(k.equals(F)){
return KeyEvent.VK_F;
} else if(k.equals(G)){
return KeyEvent.VK_G;
} else if(k.equals(H)){
return KeyEvent.VK_H;
} else if(k.equals(I)){
return KeyEvent.VK_I;
} else if(k.equals(J)){
return KeyEvent.VK_J;
} else if(k.equals(K)){
return KeyEvent.VK_K;
} else if(k.equals(L)){
return KeyEvent.VK_L;
} else if(k.equals(M)){
return KeyEvent.VK_M;
} else if(k.equals(N)){
return KeyEvent.VK_N;
} else if(k.equals(O)){
return KeyEvent.VK_O;
} else if(k.equals(P)){
return KeyEvent.VK_P;
} else if(k.equals(Q)){
return KeyEvent.VK_Q;
} else if(k.equals(R)){
return KeyEvent.VK_R;
} else if(k.equals(S)){
return KeyEvent.VK_S;
} else if(k.equals(T)){
return KeyEvent.VK_T;
} else if(k.equals(U)){
return KeyEvent.VK_U;
} else if(k.equals(V)){
return KeyEvent.VK_V;
} else if(k.equals(W)){
return KeyEvent.VK_W;
} else if(k.equals(X)){
return KeyEvent.VK_X;
} else if(k.equals(Y)){
return KeyEvent.VK_Y;
} else if(k.equals(Z)){
return KeyEvent.VK_Z;
} else if(k.equals(1)){
return KeyEvent.VK_1;
} else if(k.equals(2)){
return KeyEvent.VK_2;
} else if(k.equals(3)){
return KeyEvent.VK_3;
} else if(k.equals(4)){
return KeyEvent.VK_4;
} else If(k.equals(5)){
return KeyEvent.VK_5;
}否则if(k.equals(6)){
return KeyEvent.VK_6;
} else if(k.equals(7)){
return KeyEvent.VK_7;
} else if(k.equals(8)){
return KeyEvent.VK_8;
} else if(k.equals(9)){
return KeyEvent.VK_9;
} else if(k.equals(0)){
return KeyEvent.VK_0;
} else {
return 0;
}
}

我需要一种方法加载类作为一个动作,因为现在它被编译器视为一个类,不能添加到actionCommands hashmap。



并且有一个更简单的方法要处理哪个KeyEvent被加载的String要求?

解决方案

/ p>

您可以创建一个对象,然后将其转换到您的界面。

  Object obj = clazz.newInstance(); 
Action actionObj =(Action)obj;

遵循约定,类名以大写开头。


I've got a project I am working on and in this project I would like to include the ability for developers to include their own plugins without having to change the whole code.

This is what I have developed for it so far. this is the interface that the plugins are using.


package com.pennion.pennpad;

public interface action{
 void doAction();
}

This is the main code that loads the plugins among other things.



 Map menuMap=new HashMap();
 Map actionCommands=new HashMap();
 public void load3rdPartyMenu() throws Exception{
  String userHome=System.getProperty("user.home");
  String sep=File.getSeparator();
  String fileString=userHome+sep+"pennion"+sep+"pennpad"+sep+"plugins"+sep+"plugins.conf";
  File cfgFile=new File(fileString);
  BufferedReader in=new BufferedReader(new InputStreamReader(new FileStreamReader(cfgFile)));
  String ln="";
  boolean menuFound=false;
  while((ln=in.readLine())!=null){
   if(!menuFound){
    if(ln.equals("//!==Menu!==//")){
     menuFound=true;
    } else{
     menuFound=false;
    }
   } else{
    String pluginName="";
    String pluginDescription="";
    String KeyMask="";
    String[] split=ln.split("||");
    pluginName=split[0];
    KeyMask=split[1];
    pluginDescription=split[2];
    ClassLoader pluginLoader=ClassLoader.getClassLoader();
    Class c=pluginLoader.loadClass("com.pennion.3rdparty."+pluginName);
    Map keyMap=new HashMap();
    String[] kmSplit=KeyMask.split("+");
    if(kmSplit[0].equals("CTRL")){
     keyMap.put("ActionEvent",ActionEvent.CTRL_MASK);
    } else if(kmSplit[0].equals("SHIFT")){
     keyMap.put("ActionEvent",ActionEvent.SHIFT_MASK);
    } else if(kmSplit[0].equals("ALT")){
     keyMap.put("ActionEvent",ActionEvent.ALT_MASK);
    } else if(kmSplit[0].equals("ALT_CTRL")||kmSplit[0].equals("CTRL_ALT")){
     keyMap.put("ActionEvent",ActionEvent.CTRL_MASK+ActionEvent.ALT_MASK);
    } else if(kmSplit[0].equals("SHIFT_CTRL")||kmSplit[0].equals("CTRL_SHIFT")){
     keyMap.put("ActionEvent",ActionEvent.CTRL_MASK+ActionEvent.SHIFT_MASK);
    } else if(kmSplit[0].equals("ALT_SHIFT")||kmSplit[0].equals("SHIFT_ALT")){
     keyMap.put("ActionEvent",ActionEvent.SHIFT_MASK+ActionEvent.ALT_MASK);
    }
    keyMap.put("KeyBind",getKeyBinding(kmSplit[1]));
    this.addMenuItem("Plugin",pluginName,keyMap.get("KeyBind"),keyMap.get("ActionEvent"),keyMap.get("KeyBind"),pluginName,c);
   }
  }
 }
 public int getKeyBinding(String k){
  if(k.equals("A")){
   return KeyEvent.VK_A;
  } else if(k.equals("B")){
   return KeyEvent.VK_B;
  } else if(k.equals("C")){
   return KeyEvent.VK_C;
  } else if(k.equals("D")){
   return KeyEvent.VK_D;
  } else if(k.equals("E")){
   return KeyEvent.VK_E;
  } else if(k.equals("F")){
   return KeyEvent.VK_F;
  } else if(k.equals("G")){
   return KeyEvent.VK_G;
  } else if(k.equals("H")){
   return KeyEvent.VK_H;
  } else if(k.equals("I")){
   return KeyEvent.VK_I;
  } else if(k.equals("J")){
   return KeyEvent.VK_J;
  } else if(k.equals("K")){
   return KeyEvent.VK_K;
  } else if(k.equals("L")){
   return KeyEvent.VK_L;
  } else if(k.equals("M")){
   return KeyEvent.VK_M;
  } else if(k.equals("N")){
   return KeyEvent.VK_N;
  } else if(k.equals("O")){
   return KeyEvent.VK_O;
  } else if(k.equals("P")){
   return KeyEvent.VK_P;
  } else if(k.equals("Q")){
   return KeyEvent.VK_Q;
  } else if(k.equals("R")){
   return KeyEvent.VK_R;
  } else if(k.equals("S")){
   return KeyEvent.VK_S;
  } else if(k.equals("T")){
   return KeyEvent.VK_T;
  } else if(k.equals("U")){
   return KeyEvent.VK_U;
  } else if(k.equals("V")){
   return KeyEvent.VK_V;
  } else if(k.equals("W")){
   return KeyEvent.VK_W;
  } else if(k.equals("X")){
   return KeyEvent.VK_X;
  } else if(k.equals("Y")){
   return KeyEvent.VK_Y;
  } else if(k.equals("Z")){
   return KeyEvent.VK_Z;
  } else if(k.equals("1")){
   return KeyEvent.VK_1;
  } else if(k.equals("2")){
   return KeyEvent.VK_2;
  } else if(k.equals("3")){
   return KeyEvent.VK_3;
  } else if(k.equals("4")){
   return KeyEvent.VK_4;
  } else if(k.equals("5")){
   return KeyEvent.VK_5;
  } else if(k.equals("6")){
   return KeyEvent.VK_6;
  } else if(k.equals("7")){
   return KeyEvent.VK_7;
  } else if(k.equals("8")){
   return KeyEvent.VK_8;
  } else if(k.equals("9")){
   return KeyEvent.VK_9;
  } else if(k.equals("0")){
   return KeyEvent.VK_0;
  } else{
   return 0;
  }
 }

I need a way to cast the loaded class as an action because as of now it is being considered a class by the compiler and can't be added to the actionCommands hashmap.

and is there an easier way to process which KeyEvent is being asked for by the loaded String?

解决方案

You can't cast a Class direct to something.

You can create a object of it and then cast it to your interface.

Object obj = clazz.newInstance();
Action actionObj = (Action) obj;

Follow the conventions, class names starts with upper case.

这篇关于Java:你能把Class转换成一个特定的接口吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆