繁体   English   中英

Java-如何调用带字符串的类?

[英]Java-How do I call a class with a string?

我是一个初学者程序员,这是我在这个论坛上的第一个问题。

我正在使用BlueJ作为编译器编写一个简单的文本冒险游戏,并且在Mac上。 我遇到的问题是我想使我的代码更加自动化,但是我不能用字符串调用类。 我想要调用该类而不在if函数中全部包含它的原因是为了使我可以合并更多方法。

当前运行方式如下:

public class textadventure {
public method(String room){
if(room==street){street.enterRoom();}
}
}
public class street{
public enterRoom(){
//do stuff and call other methods
}
}

if语句对我创建的每个班级/教室进行测试。 我希望代码执行的操作是自动将字符串空间设置为可以调用的类名称。 因此它可能像这样:

Public method(string room){
Class Room = room;
Room.enterRoom();
}

我已经研究过使用Class.forName,但是所有示例对于我来说太笼统,以至于无法理解如何使用该函数。 任何帮助将不胜感激,如果有其他必要的信息(例如更多示例代码),我很乐意提供。

-塞巴斯蒂安

这是完整的代码:

import java.awt.*;
import javax.swing.*;

public class Player extends JApplet{
public String textOnScreen;

public void start(){
room("street1");
}

public void room(String room){
    if(room=="street1"){
    textOnScreen=street1.enterRoom();
    repaint();
    }
    if(room=="street2"){
    textOnScreen=street2.enterRoom();
    repaint();
    }
}
public void paint(Graphics g){
    g.drawString(textOnScreen,5,15);
}
}

public abstract class street1
{
private static String textToScreen;
public static String enterRoom(){
    textToScreen = "You are on a street running from North to South.";
    return textToScreen;
}
}

public abstract class street2
{
private static String textToScreen;
public static String enterRoom(){
    textToScreen = "You are on another street.";
    return textToScreen;
}
}

鉴于您是编程的新手,我建议您从一些比成熟的冒险游戏更简单的程序开始。 您仍然没有完全掌握Java语法的一些基础知识。 以HelloWorld程序为例:

public class HelloWorld {
  public static void main(String[] args) {
    String output = "Hello World!"
    System.out.println(output);
  }   
}

注意public是小写的。 具有大写P Publicpublic

还要注意, String类的大小写为S *,大小写也很重要,因此stringString

另外,请注意,我不必使用String string = new String("string") 您可以使用String string = "string" 该语法运行速度更快,更易于阅读。

测试字符串相等性时,您需要使用String.equals而不是== 这是因为a == b为对象相等检查(即ab占用在存储器中的相同的点)和stringOne.equals(stringTwo)检查是否stringOne具有相同的字符以相同的次序作为stringTwo无论他们在哪里在记忆中。

现在,关于您的问题,我建议您使用EnumMap来跟踪要使用的对象。

例如:

public class Tester {

  public enum Location {

    ROOM_A("Room A", "You are going into Room A"),
    ROOM_B("Room B", "You are going into Room B"),
    OUTSIDE("Outside", "You are going outside");
    private final String name;
    private final String actionText;

    private Location(String name, String actionText) {
      this.name = name;
      this.actionText = actionText;
    }

    public String getActionText() {
      return this.actionText;
    }   

    public String getName() {
      return this.name;
    }   

    public static Location findByName(String name) {
      name = name.toUpperCase().replaceAll("\\s+", "_");
      try {
        return Enum.valueOf(Location.class, name);
      } catch (IllegalArgumentException e) {
        return null;
      }   
    }   
  }


  private Location currentLocation;

  public void changeLocation(String locationName) {
    Location location = Location.findByName(locationName);
    if (location == null) {
      System.out.println("Unknown room: " + locationName);
    } else if (currentLocation != null && currentLocation.equals(location)) {
      System.out.println("Already in room " + location.getName());
    } else {
      System.out.println(location.getActionText());
      currentLocation = location;
    }
  }

  public static void main(String[] args) {
    Tester tester = new Tester();
    tester.changeLocation("room a");
    tester.changeLocation("room b");
    tester.changeLocation("room c");
    tester.changeLocation("room b");
    tester.changeLocation("outside");
  }
}

*这是格式化Java代码的标准方式。 类名是PascalCased ,变量名是camelCased

String className=getClassName();//Get class name from user here
String fnName=getMethodName();//Get function name from user here

Class params[] = {};
Object paramsObj[] = {};

Class thisClass = Class.forName(className);// get the Class

Object inst = thisClass.newInstance();// get an instance
// get the method
Method fn = thisClass.getDeclaredMethod(fnName, params);
// call the method
fn.invoke(inst, paramsObj);

问题下方的注释是正确的-您的代码很粗糙。

无论如何,如果您有类似的方法

public void doSomething(String str) {
  if (str.equals("whatever")) {
    // do something
  }
}

然后像这样称呼它

doSomething("whatever");

在Java中,许多类都具有属性,并且您可以而且经常会拥有同一类的多个实例。

您将如何识别哪个名字呢?

例如

class Room {
    List<Monster> monsters = new ArrayList <Monster> ();
    public Room (int monstercount) {
         for (int i = 0; i < monstercount; ++i)
            monsters.add (new Monster ());
    }
    // ...
}

怪物可以具有属性,如果其中之一已消失,则如果不处理字符串中的所有内容,则可以更轻松地识别它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM