在java中使用字符串查找类?
我创建了一个名为Entity的类,并具有以下代码:在java中使用字符串查找类?,java,string,class,Java,String,Class,我创建了一个名为Entity的类,并具有以下代码: Entity zombie1 = new Entity(); 我从扫描仪中获取输入“zombie”,然后根据末尾的级别连接一个数字,将“zombie 1”保留为字符串。。。我希望能够使用该字符串并调用 zombie1.shoot("shotgun"); 但我似乎找不到解决办法。我只想做一个if语句,但我希望能够创建尽可能多的僵尸,而不必每次都输入更多的if语句 我读过使用反射和forString的文章,但这似乎不是我想要的 任何帮助都很好。
Entity zombie1 = new Entity();
我从扫描仪中获取输入“zombie”,然后根据末尾的级别连接一个数字,将“zombie 1”保留为字符串。。。我希望能够使用该字符串并调用
zombie1.shoot("shotgun");
但我似乎找不到解决办法。我只想做一个if语句,但我希望能够创建尽可能多的僵尸,而不必每次都输入更多的if语句
我读过使用反射和forString的文章,但这似乎不是我想要的
任何帮助都很好。可能的解决方案是使用
映射
来存储和检索基于特定字符串的实体。如果实体的子类型(如僵尸、吸血鬼、受害者等)数量有限,则可以使用映射
,将字符串映射到特定类型的实体,然后按编号获取该类型
e、 g
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Foo002 {
private static final String ZOMBIE = "zombie";
public static void main(String[] args) {
Map<String, List<Entity>> entityMap = new HashMap<String, List<Entity>>();
entityMap.put(ZOMBIE, new ArrayList<Entity>());
entityMap.get(ZOMBIE).add(new Entity(ZOMBIE, "John"));
entityMap.get(ZOMBIE).add(new Entity(ZOMBIE, "Fred"));
entityMap.get(ZOMBIE).add(new Entity(ZOMBIE, "Bill"));
for (Entity entity : entityMap.get(ZOMBIE)) {
System.out.println(entity);
}
}
}
class Entity {
private String type;
private String name;
public Entity(String type, String name) {
this.type = type;
this.name = name;
}
public String getType() {
return type;
}
public String getName() {
return name;
}
@Override
public String toString() {
return type + ": " + name;
}
}
import java.util.ArrayList;
导入java.util.HashMap;
导入java.util.List;
导入java.util.Map;
公开课Foo002{
私有静态最终字符串ZOMBIE=“ZOMBIE”;
公共静态void main(字符串[]args){
Map entityMap=newhashmap();
entityMap.put(ZOMBIE,newarraylist());
获取(ZOMBIE).add(新实体(ZOMBIE,“John”);
获取(ZOMBIE).add(新实体(ZOMBIE,“Fred”);
entityMap.get(ZOMBIE).add(新实体(ZOMBIE,“Bill”);
for(实体:entityMap.get(ZOMBIE)){
系统输出打印项次(实体);
}
}
}
类实体{
私有字符串类型;
私有字符串名称;
公共实体(字符串类型、字符串名称){
this.type=type;
this.name=名称;
}
公共字符串getType(){
返回类型;
}
公共字符串getName(){
返回名称;
}
@凌驾
公共字符串toString(){
返回类型+“:”+名称;
}
}
可能的解决方案是使用映射
来存储和检索基于特定字符串的实体。如果实体的子类型(如僵尸、吸血鬼、受害者等)数量有限,则可以使用映射
,将字符串映射到特定类型的实体,然后按编号获取该类型
e、 g
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Foo002 {
private static final String ZOMBIE = "zombie";
public static void main(String[] args) {
Map<String, List<Entity>> entityMap = new HashMap<String, List<Entity>>();
entityMap.put(ZOMBIE, new ArrayList<Entity>());
entityMap.get(ZOMBIE).add(new Entity(ZOMBIE, "John"));
entityMap.get(ZOMBIE).add(new Entity(ZOMBIE, "Fred"));
entityMap.get(ZOMBIE).add(new Entity(ZOMBIE, "Bill"));
for (Entity entity : entityMap.get(ZOMBIE)) {
System.out.println(entity);
}
}
}
class Entity {
private String type;
private String name;
public Entity(String type, String name) {
this.type = type;
this.name = name;
}
public String getType() {
return type;
}
public String getName() {
return name;
}
@Override
public String toString() {
return type + ": " + name;
}
}
import java.util.ArrayList;
导入java.util.HashMap;
导入java.util.List;
导入java.util.Map;
公开课Foo002{
私有静态最终字符串ZOMBIE=“ZOMBIE”;
公共静态void main(字符串[]args){
Map entityMap=newhashmap();
entityMap.put(ZOMBIE,newarraylist());
获取(ZOMBIE).add(新实体(ZOMBIE,“John”);
获取(ZOMBIE).add(新实体(ZOMBIE,“Fred”);
entityMap.get(ZOMBIE).add(新实体(ZOMBIE,“Bill”);
for(实体:entityMap.get(ZOMBIE)){
系统输出打印项次(实体);
}
}
}
类实体{
私有字符串类型;
私有字符串名称;
公共实体(字符串类型、字符串名称){
this.type=type;
this.name=名称;
}
公共字符串getType(){
返回类型;
}
公共字符串getName(){
返回名称;
}
@凌驾
公共字符串toString(){
返回类型+“:”+名称;
}
}
如果您谈论的是动态调用对象上的方法,那么可以使用反射来获取方法对象并调用它(注意:我可能无意中混淆了此Java中的一些C#语法):
如果您正在谈论动态调用对象上的方法,那么可以使用反射来获取方法对象并调用它(注意:我可能无意中混淆了此Java中的一些C#语法):
这不是你最好的选择。你最好的选择是拥有一张地图
// PLEASE LOOK INTO WHICH MAP WOULD BE BEST FOR YOUR CASE OVERALL
// HASHMAP IS JUST AN EXAMPLE.
Map<String, Entity> zombieHoard = new HashMap<String, Entity>;
String getZombieID( int id )
{
return String.format( "zombie%s", id );
}
String createZombie() {
String zid = getZombieID( Map.size() );
Map.put( zid, new Entity() );
return zid;
}
void sendForthTheHoard() {
createZombie();
createZombie();
String currentZombie = createZombie();
zombieHoard.get( currentZombie ).shoot( "blow-dryer" );
zombieHoard.get( getZombieID( 1 ) ).eatBrains();
}
//请查看哪种地图最适合您的总体情况
//HASHMAP只是一个例子。
Map zombiepald=新HashMap;
字符串getZombieID(int-id)
{
返回String.format(“僵尸%s”,id);
}
字符串createZombie(){
字符串zid=getZombieID(Map.size());
put(zid,new Entity());
返回zid;
}
void sendforth囤积(){
createZombie();
createZombie();
字符串currentZombie=createZombie();
僵尸囤积。获取(当前僵尸)。拍摄(“吹风机”);
zombiecavad.get(getZombieID(1)).eatBrains();
}
这不是你最好的选择。你最好的选择是拥有一张地图
// PLEASE LOOK INTO WHICH MAP WOULD BE BEST FOR YOUR CASE OVERALL
// HASHMAP IS JUST AN EXAMPLE.
Map<String, Entity> zombieHoard = new HashMap<String, Entity>;
String getZombieID( int id )
{
return String.format( "zombie%s", id );
}
String createZombie() {
String zid = getZombieID( Map.size() );
Map.put( zid, new Entity() );
return zid;
}
void sendForthTheHoard() {
createZombie();
createZombie();
String currentZombie = createZombie();
zombieHoard.get( currentZombie ).shoot( "blow-dryer" );
zombieHoard.get( getZombieID( 1 ) ).eatBrains();
}
//请查看哪种地图最适合您的总体情况
//HASHMAP只是一个例子。
Map zombiepald=新HashMap;
字符串getZombieID(int-id)
{
返回String.format(“僵尸%s”,id);
}
字符串createZombie(){
字符串zid=getZombieID(Map.size());
put(zid,new Entity());
返回zid;
}
void sendforth囤积(){
createZombie();
createZombie();
字符串currentZombie=createZombie();
僵尸囤积。获取(当前僵尸)。拍摄(“吹风机”);
zombiecavad.get(getZombieID(1)).eatBrains();
}
将你的僵尸放入ArrayList。例如:
ArrayList<Entity> zombies = new ArrayList<Entity>();
Entity zombie1 = new Entity();
zombies.add(zombie1);
Entity zombie2 = new Entity();
zombies.add(zombie2);
etc...
把你的僵尸放到ArrayList里。例如:
ArrayList<Entity> zombies = new ArrayList<Entity>();
Entity zombie1 = new Entity();
zombies.add(zombie1);
Entity zombie2 = new Entity();
zombies.add(zombie2);
etc...
String是默认创建的类,其中没有这样的.shot方法!但是,您可以尝试按字符串类扩展实体类并向其添加方法!未测试需要更多的专业建议。String是默认生成的类,其中没有这样的。shoot方法!但是,您可以尝试按字符串类扩展实体类并向其添加方法!未经测试的用户需要更多的专业建议。