gpt4 book ai didi

java - 无法使用 Class.forName() 实例化类

转载 作者:行者123 更新时间:2023-12-01 10:45:01 25 4
gpt4 key购买 nike

我在使用 Class.forName() 实例化类时遇到问题。起初,我尝试仅使用类名来创建类,但它不断抛出“ClassNotFoundError”。因此,我添加了包名称,但我的格式是 package$className。例如:

`Class<?> cls = Class.forName("GreenhouseControls$ThermostatNight"); //` This Works.

我读取了需要从文件中实例化的类名。创建类后,我使用 Controller 类中的 addEvent() 函数添加事件。

不幸的是,当我尝试让构造函数实例化该类时,我收到“NoSuchMethodError”。我不知道为什么?我想知道是否是抽象类不允许我获取子类构造函数?

GreenhouseControls.java

import java.io.*;
import java.lang.reflect.*;
import java.lang.Long;
import java.util.*;
import java.util.regex.*;
import tme3.*;

public class GreenhouseControls extends Controller{


public class ThermostatNight extends Event {

public ThermostatNight(long delayTime) {
super(delayTime);
}
public void action() {
// Put hardware control code here.
thermostat = "Night";
}
public String toString() {
return "Thermostat on night setting";
}
}



public class Restart extends Event {
public Restart(long delayTime, String filename) {
super(delayTime);
eventsFile = filename;
}

public void action() {
File f = new File(eventsFile);
try{
Scanner scan = new Scanner(f);
Pattern class_name = Pattern.compile("(?<==)(.*)(?=,time)");
Pattern time1 = Pattern.compile("(?<=time=)(.*)(?=,*)");
Pattern rings_time = Pattern.compile("(?<=time=)(.*)(?=,)");
Pattern rings = Pattern.compile("(?<=rings=)(.*)");



while(scan.hasNextLine()){
String event_name;
long l;
int r = 0;
Class<?> cls;
Constructor<?> clsCon;
event_name = scan.findInLine(class_name)

try{
String check = scan.findInLine(rings_time);
check.getClass();
l = Long.valueOf(check).longValue();
r = Integer.valueOf(scan.findInLine(rings)).intValue();

}catch(NullPointerException e){ l = Long.valueOf(scan.findInLine(time1)).longValue(); }

System.out.println("event_name: " + event_name + " Time: " + l + " Rings: " + r);

cls = Class.forName("GreenhouseControls$ThermostatNight");

clsCon = cls.getDeclaredConstructor(); // This Code throws and error because apparently there is no constructor

if (scan.hasNextLine())
scan.nextLine();


}
}catch(Exception e) { System.out.println(e); }
//addEvent(new ThermostatNight(0));
}
}




public static void main(String[] args) {
try {
String option = args[0];
String filename = args[1];

if ( !(option.equals("-f")) && !(option.equals("-d")) ) {
System.out.println("Invalid option");
printUsage();
}

GreenhouseControls gc = new GreenhouseControls();

if (option.equals("-f")) {
gc.addEvent(gc.new Restart(0,filename));
}

gc.run();
}
catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Invalid number of parameters");
printUsage();
}
}
}

Event.java

package tme3;

import java.io.*;

public abstract class Event {
private long eventTime;
protected final long delayTime;

public Event(){ delayTime = 0;}
public Event(long delayTime) {
this.delayTime = delayTime;
start();
}
public void start() { // Allows restarting
eventTime = System.currentTimeMillis() + delayTime;
}
public boolean ready() {
return System.currentTimeMillis() >= eventTime;
}
public abstract void action();
}
}

Controller .java

public class Controller {
// A class from java.util to hold Event objects:
private List<Event> eventList = new ArrayList<Event>();
public void addEvent(Event c) { eventList.add(c); }

public void run() {
while(eventList.size() > 0)
// Make a copy so you're not modifying the list
// while you're selecting the elements in it:
for(Event e : new ArrayList<Event>(eventList))
if(e.ready()) {
System.out.println(e);
e.action();
eventList.remove(e);
}
}
}

最佳答案

您不能像您尝试那样直接创建内部类。您只能创建 GreenhouseControls 类型的对象。

关于java - 无法使用 Class.forName() 实例化类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34232616/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com