问题标签 [dynamic-class-creation]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
2 回答
354 浏览

c# - 调用动态对象的方法

当两个对象都是动态创建时,如何从另一个对象方法调用/调用一个对象的方法?

情况如下:

我有两个动态创建的对象,每个对象对应一个不同的类

我想打电话给objA's method1A()from objB's method1B()

我怎样才能做到这一点/你推荐什么方法?

0 投票
2 回答
93 浏览

python - 在类型创建的子类中调用 super

更新:取得了一些进展并简化了示例,尽管我目前被最新的错误难住了。我不知道为什么该super().__init__方法不受约束。

输出:

必须有某种方法可以从按类型创建的子类中调用超类方法,但是如果我能看到怎么做,我会感到震惊。

0 投票
1 回答
219 浏览

python - 如何在 Python 中的模块级 initialize() 方法中动态创建类

我正在编写一个库来使用 SQLAlchemy 与数据库通信。我真的很喜欢 SQLAlchemy 的autoload_with=engine特性,它可以传递给Table构造函数来获取表的所有列,而程序员不必显式定义它们。

这是名为“某物”的表的基本方法:

但是,我们有多个版本的数据库(在不同的主机上),所以我需要在运行时将我的引擎作为参数传入。我有点讨厌在我的模块中写这样的东西的想法,但我正在寻找更好的方法:

然后在使用任何 SQLAlchemy 模型之前,客户端代码必须做一些像这样令人讨厌的事情:

有没有更好的方法来处理外部调用者的这种模块初始化?

0 投票
8 回答
5576 浏览

java - 如何即时修改 java.lang 类?

我正在寻找一种通过重写字节码并重新加载类来动态地将字段添加到线程的方法,但不确定它是否可能。欢迎任何指点。我找到了一些关于修改和加载类的信息,并且我知道 JRebel 可以无缝地热交换您的代码,但不确定相同的方法/工具是否适用于此。

这里的动机是探索一种理论上更好的替代线程本地对象的方法。如果该方法有效,我应该能够用注释替换线程本地,并且结果应该优于当前的 JDK 实现。

PS:请救救我“万恶之源”

澄清用例:

想象一下,我有一个带有 ThreadLocal 的类:

我想用注释替换它:

但不是生成上面的代码,我想改变 Thread 以便 Thread 现在有一个 Acounter 字段,实际代码将是:

0 投票
2 回答
1611 浏览

class - C#中的动态创建类

我想知道是否有办法在运行时动态生成类。我想先在实体框架代码中使用它。

考虑如果我有 100 个表(或连接到未知数据库),我将不得不在 EF Code First 中为每个表创建模型/POCO,而不是这个我想在运行时基于连接的数据库生成 POCO 类及其所有属性.

0 投票
1 回答
93 浏览

python - python use different parent class for child class

Using python and django we are creating a framework to run a number of scientific models. Python runs the models, and django is used to keep track of status and to create output to webpages. In operational mode this works fine, as the entire platform (database, django executable) is available. However for the offline creation and testing of new models, my colleages often work on stand-alone code, with no connection to the environment. Then the django part is often an burden (environment, database need to be set up, computational overhead). So we are looking for a way to toggle the Django-functions on and off.

Currently we have a general class iModel, which has all functionality onboard to run any model in the framework, also some django calls. Childclasses inherit from this class, and contain the model-specific functionality.

I was thinking of splitting the class iModel into two classes. an extra class iClassWithDjango(), that would inherit from iModel and that adds the Django functionality. As a wrapper around the class iModel.

first class

and second class

Depending on the situation (operational or offline) DOGmodel and CATmodel would inherit resp. from iModelwithDjango() or iModel(). So change the parentclass dynamically at runtime.

In fact my biggest problem is how to handle the import of the django modules. How can I prevent this to happen in offline mode (as it will fail because the django environment is not set up). My idea was to put iModelWithDjango() in a separate module file (as done above), but maybe there are better ways to do this.

Maybe with a lot of messy coding I could pull this off, but I was hoping some of you could steer me to some clean coding practice. Any advice welcome

0 投票
2 回答
376 浏览

java - 在运行时更改类结构或将其放入匿名类

我有

现在我拥有ArrayList<? extends Object> resultDataA 类和其他一些类的对象。基本上我需要这个列表'resultData'来在其他一些API中生成一个Json文件。

现在我的问题是,在将 A 类对象添加到列表中并且某些条件(X)为真时,我需要修改后的 A 类对象(或任何其他匿名类对象),例如:

那是 A 类的一个特定对象不应该有字段 String b(在批评之前,我这样做是因为我需要针对我的特定 JSon 格式进行修改的对象并且我不想定义使用的新类定义只有一次)

我的最后一个选择是像这样制作匿名类:(然后将其添加到我的列表中)

也请。建议任何其他创建具有所需结构的匿名类的方法。

0 投票
0 回答
137 浏览

java - 在运行时加载类时出现 ClassNotFoundException

使用 Bukkit-API 我目前正在尝试创建一个插件,该插件可以在运行时编译给定 pastebin 链接的 java 代码并执行该代码。到目前为止一切正常,除了一件事:当我尝试访问一个已加载的插件时,我得到了 ClassNotFoundException。(当我使用 Bukkit-API 方法时,我没有遇到异常!)所有插件 jar 都已添加到类路径中;它目前看起来像这样:

我尝试动态加载的类:

MutliCube 的 ClassNotFoundException

在职的

RuntimeCompiler.java 的源代码:http://paste.multicu.be/axigofekip.avrasm 有点长,这就是我使用 pastebin 链接的原因)

我还注意到,当我删除 MultiCube.jar 的类路径时出现编译错误,这意味着类路径必须正确,因为可以编译源代码。

编辑:我能够通过在我的 URLClassLoader 的构造函数中添加 MultiCube.class.getClassLoader() 作为参数来修复它

0 投票
1 回答
200 浏览

python - Python __new__ 没有调用另一个类的 __new__ 版本?

什么是在 Python 中使用的示例,__new__这样在__new__您的主体内不会返回调用其他(基、元或其他)类的版本的结果__new__,但您确实返回了预期类的已分配实例,这样__init__将被调用?

我猜这是(并且应该是)非常罕见的,但我很好奇是否有这个用例的例子。我尝试四处搜索tuple' 的实现__new__和' 的实现的 Python 源代码type,但在网上找到这似乎并不容易。

0 投票
2 回答
1515 浏览

java - URLClassLoader ClassNotFoundException

I'm trying to write a code that loads classes dynamically at run time

the problem is that this keeps giving me classNotFound exception , tho i'm pretty sure files .class exist in that directory , another thing I've tried loading already loaded classes and it worked , so it doesn't work only if my code doesn't have the class file !!