我在此代码中遇到了转换问题,但不知道如何纠正它.
public void showFrame(String className, Object controller) throws Exception{
try {
Class c = Class.forName("com." + className);
// "(Object.class)" I want this to be of type held in className var
// this type will be same as one passed into "controller" at runtime
Constructor ctr = c.getConstructor(Object.class);
ctr.newInstance(controller);
} catch (ClassNotFoundException ex) {
JOptionPane.showMessageDialog(null, "Error" );
}
}
Run Code Online (Sandbox Code Playgroud) 为什么通过反射调用方法比创建一个接口然后通过反射调用它要慢得多.第一个版本显示了其他版本显示增强方式的繁琐方式?
// first version
class A
{
public void fn()
{
}
}
void Main(String[]x)
{
Type type = typeof(A);
object obj = Activator.CreateInstance(type);
type.InvokeMember("fn", BindingFlags.Public, null, obj, null);
}
//second verison
interface IA
{
void fn();
}
class A :IA
{
public void fn()
{
}
}
void Main(String []x)
{
Type type = typeof(A);
IA obj =(IA) Activator.CreateInstance(type);
obj.fn();
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用反射将Object转换为其类型.
所以我想做一些事情,比如使用typeOf()方法找到它的类型,然后动态地将对象转换为它的found类型.
我不知道如何使用linq很好地做到这一点?
我使用的是c#4.0.
谢谢.
我正在测试问题的解决方案
我遇到的是,如果我尝试调用诸如房地产游戏之类的应用程序,则此解决方案不起作用.因此,在调用main之后只需添加以下代码即可 Applic2
Frame[] f2 = JFrame.getFrames();
for(Frame fx: f2){
if (fx instanceof JFrame) {
JFrame aFrame = (JFrame)fx;
aFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
}
Run Code Online (Sandbox Code Playgroud)
我创建了一个异步线程,不断将操作JFrame.EXIT_ON_CLOSE更改为JFrame.DISPOSE_ON_CLOSE,如下所示
import java.awt.Frame;
import javax.swing.JFrame;
public class FrameMonitor extends Thread{
@Override
public void run(){
while(true){
Frame[] f2 = JFrame.getFrames();
for(Frame fx : f2){
if(fx instanceof JFrame){
JFrame aframe =(JFrame)fx;
aframe.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
并通过MyApp类中的start方法调用此线程实例.但解决方案无效.我仍然面临关闭一帧时所有帧关闭的相同问题.为什么会发生任何建议以及如何克服这个?
请仔细检查以下问题:
让我更详细地介绍这个问题
将房地产游戏代码添加到工作区
将以下包添加到RealEstate代码中
package MyApplication;
import java.awt.Frame;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
import …Run Code Online (Sandbox Code Playgroud) 我需要解释以下问题:
我在反思中很困惑。我已经阅读了很多文件,但我仍然感到困惑。请给我一个我能完全理解的解释。
如何解决我的代码?
package mypackage;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
public class testReflection {
public class You {
public You(String s) {
}
public void f(String s, int i) {
System.out.println(i + 100);
}
}
public static void main(String[] args) throws NoSuchMethodException {
Constructor constructor =
You.class.getConstructor(testReflection.class, String.class);
try {
You y = (You)constructor.newInstance("xzy");//Exception!!!!
System.out.println("ok");
y.f("xyz",2);
}catch(Exception e){
e.printStackTrace();
}
}
}
Run Code Online (Sandbox Code Playgroud)
异常消息是:
java.lang.IllegalArgumentException: wrong number of arguments
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) …Run Code Online (Sandbox Code Playgroud) I've been through "Questions that may already have your answer" and didn't find the thing I'm looking for. I want to get all members on a type via reflection. When I try
var type = typeof(int);
var members = type.GetMembers();
for(var i = 0; i < members.Length; i++)
Console.WriteLine($"{i, 2} {members[i]}");
Run Code Online (Sandbox Code Playgroud)
I get 19 members.
I found that some members need specific BindingFlags. Since I don't know those flags and different members have different flags I pass all …
我将以下遗留代码迁移到 Java 16,但是由于这个新版本引入了强封装,它不起作用:
try {
Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
method.setAccessible(true);
method.invoke(new URLClassLoader(
new URL[] {}),
new File("C:/external-folder/my.jar").toURI().toURL()
);
} catch (Exception exc) {
exc.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
有办法让它发挥作用吗?
我明明把构造函数的setAccessible设置为true了,可是为什么还是报错!你能告诉我如何修复它吗
public class Person {
// fields...
private Person(String name, int age, String address, int num) {
this.name = name;
this.age = age;
this.address = address;
this.num = num;
System.out.println("private full-constructor be created");
}
}
Run Code Online (Sandbox Code Playgroud)
// 发生错误?!为什么?
public static void operatePrivateConstructorFullParameter(Class<? extends Person> clzz) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
Constructor<? extends Person> constructor = clzz.getConstructor(String.class, int.class, String.class, int.class);
constructor.setAccessible(true);
Person person = constructor.newInstance("Miao", 18, "MOON", 88);
}
public static void main(String[] args) throws Exception {
Class<? extends Person> clzz …Run Code Online (Sandbox Code Playgroud) 我有一个存储大量int成员变量的类,每个变量定义如下:
public final static int int1 = int1value;
public final static int int2 = int2value;
...
public final static int int106 = int106value;
Run Code Online (Sandbox Code Playgroud)
还有第二个类需要根据int第一个类中的s 数进行循环.我如何向第一个类添加一个函数来计算这些成员变量?