在 .NET C# 中是否可以在运行时更改对象的所有者?
例如:
class abc {
MyClass ClassInstance = new MyClass();
AnotherClass AnotherClassInstance = new AnotherClass();
// Some how set the owner of "AnotherClassInstance" to "ClassInstance"
}
Run Code Online (Sandbox Code Playgroud)
谢谢!
我正在写一个包含a的程序JButton.每次单击该按钮时,JTextField都会向a添加一个新按钮JPanel.
我的问题是,在用户创建了所有内容JTextFields并用信息填充后,我需要获取每个字段的文本.我怎样才能访问JTextFields它们动态生成的时间,因为它们没有实例名称?有没有更好的方法来获取每个文本,而不知道他们的实例名称.
这是...... actionPerformed事件的代码JButton
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
JTextField x = new JTextField();
x.setColumns(12);
p.add(x);
validate();
}
Run Code Online (Sandbox Code Playgroud) 我正在用Python编写一个空间交易游戏,并且我决定将地图分成较小的块,以减少在任何给定时间在屏幕上绘制时需要考虑的对象数量。
这通过具有一个Sector对象来处理,该对象的定义如下:
class Sector:
x=0 #Position of the sector. The galactic (absolute) position of any object is its in-sector position
y=0 #plus the galactic position offset times the size of a sector.
stardict=dict()
Run Code Online (Sandbox Code Playgroud)
然后,生成器代码将75个星标填充到每个扇区(此刻目前为100个扇区),并保留在stardict中。
thesector=Sector()
size=size/2
#Generate stars
for t in range(stars):
#name="Star "+str(t)
name=generate_name().capitalize()
thesector.stardict[name]=Star( name, (random.randint(-size,size), random.randint(-size,size)), (random.randint(0,255), random.randint(0,255), random.randint(0,255)), random.randint(3,7))
if math.floor((t/stars)*100)==(t/stars)*100: print("Generating stars: "+str((t/stars)*100)+"% done.")
Run Code Online (Sandbox Code Playgroud)
但是,在尝试实际运行程序时会出现一些奇怪的错误,并且使用调试器打开它可以说明原因:每个扇区的stardict属性是相同的,它们每个都包含完全相同的星号(不是重复的,它们具有相同的内存地址) 。据我所知,每个Sector.stardict实际上都引用同一个dict对象。
我不知道为什么会这样。谁能对此有所启发?
我使用延迟初始化方法创建了一个Singleton类.这就是getInstance方法是同步的.但是,如果不改变设计模式,有一种方法可以创建Singleton类的多个实例.因为更改Singleton模式将需要进行大量的体系结构更改.请建议一种创建多个实例的方法.请帮助Java.
我是python的新手,所以我现在很困惑.我只想在循环中创建一些MyClass类的实例.
我的代码:
for i in range(1, 10):
my_class = MyClass()
print "i = %d, items = %d" % (i, my_class.getItemsCount());
my_class.addItem(i)
Run Code Online (Sandbox Code Playgroud)
MyClass类
class MyClass:
__items = []
def addItem(self, item):
self.__items.append(item)
def getItemsCount(self):
return self.__items.__len__();
Run Code Online (Sandbox Code Playgroud)
输出是:
i = 0, items = 0
i = 1, items = 1
i = 2, items = 2
and so on...
Run Code Online (Sandbox Code Playgroud)
但是我期望在每次迭代时变量my_class中都有新的MyClass空实例.所以预期产量是:
i = 0, items = 0
i = 1, items = 0
i = 2, items = 0
Run Code Online (Sandbox Code Playgroud)
你能帮我理解吗?谢谢.
我知道实例变量的意思是状态,而常量意味着不变.是否有任何理由(除了惯例)使用常量而不是实例变量?使用常量是否有内存/速度优势?
我的程序需要监视多个位置,但是为每个位置触发相同的代码.由于单个FileSystemWatcher无法监控多个位置,但是是否可以创建多个实例并为每个位置传递一个文件夹路径?
我不能对每个硬编码进行硬编码,FileSystemWatcher因为需要及时添加越来越多的位置,这需要由最终用户完成,因为FileSystemWatcher每次手动硬编码都是非常不切实际的.所以我的计划是将文件夹路径保存到文件中,程序只FileSystemWatcher为列表中的每个路径创建一个.但我不知道这是否有可能是最轻微的.
在这里进行工厂方法模式建议尝试:我得到错误:"'List'不包含'add'的定义
public void StartWatchers()
{
string[] ArrayPaths = new string[2];
List<FileSystemWatcher> watchers = new List<FileSystemWatcher>();
ArrayPaths[0] = @"K:\Daily Record Checker\Test\Test1";
ArrayPaths[1] = @"K:\Daily Record Checker\Test\Test2";
int i = 0;
foreach (String String in ArrayPaths)
{
watcher.add(MyWatcherFatory(ArrayPaths[i]));
i++;
}
//Do other stuff....
//....
//Start my watchers...
foreach (FileSystemWatcher watcher in watchers)
{
Watcher.EnableRaisingEvents = true;
i++;
}
}
FileSystemWatcher MyWatcherFatory(string path)
{
FileSystemWatcher watcher = new FileSystemWatcher(path);
watcher.Changed += Watcher_Created;
watcher.Path …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用实例化渲染 ( ANGLE_instanced_arrays)在 webgl 中绘制大量立方体。
但是,我似乎无法理解如何设置除数。我有以下缓冲区;
36 个顶点(6 个面由 2 个三角形组成,每个三角形使用 3 个顶点)。每个立方体 6 种颜色(每个面 1 种颜色)。每个立方体翻译 1 次。
重用每个立方体的顶点;我将它的除数设置为 0。对于颜色,我将除数设置为 2(即对两个三角形使用相同的颜色 - 一张脸))。对于平移,我将除数设置为 12(即 6 个面 * 每个面 2 个三角形的相同平移)。
为了渲染我打电话
ext_angle.drawArraysInstancedANGLE(gl.TRIANGLES, 0, 36, num_cubes);
然而,这似乎并没有渲染我的立方体。
使用平移除数 1 确实如此,但那时颜色相差甚远,立方体是单一纯色。
我想这是因为我的实例现在是完整的立方体,但是如果我限制count(即每个实例的顶点),我似乎并没有完全通过顶点缓冲区,实际上我只是为每个立方体渲染一个三角形然后。
我将如何渲染大量这样的立方体;脸色各异?
例如,考虑来自 Google I/O '17“Android Animations Spring to Life”的幻灯片:
SpringForce force = new SpringForce(0)
.setDampingRation(0.4f)
.setStiffness(500f);
for (int i = 0; i < heads.getChildCount(); i++) {
View child = heads.getChildAt(i);
SpringAnimation anim;
anim = new SpringAnimation(child, DynamicAnimation.ROTATION);
anim.setSpring(force).setStartValue(-25).start();
}
Run Code Online (Sandbox Code Playgroud)
在那里我们可以看到变量anim在一行上定义,变量的实例在下一行创建。有时我也在一些开源项目中看到这种方法。
使用这种方法是否有真正的好处,或者只是风格或可读性的问题?或者,在幻灯片的情况下,这是适合幻灯片宽度的问题?但如果是这样,他们可能会写这样的东西:
SpringAnimation anim = new SpringAnimation(
child, DynamicAnimation.ROTATION);
Run Code Online (Sandbox Code Playgroud) 已经为您定义了一个名为“Pair”的类类型。您需要编写一个名为 pairFactory 的函数,用于在堆上创建 Pair 的实例。不要在堆栈上创建对象。然后,您的函数需要返回一个指向该创建对象的指针。
我已经为pairFactory 编写了代码。它似乎可以运行,但我收到 InfraError。请帮我找出我的错误。另外,我需要在堆内存中创建对象。
#include <iostream>
// This class Pair has already been defined for you.
// (You may not change this definition.)
class Pair {
public:
int first, second;
void check() {
first = 5;
std::cout << "Congratulations! The check() method of the Pair class \n has executed. (But, this isn't enough to guarantee \n that your code is correct.)" << std::endl;
}
};
Pair *pairFactory() {
//
Pair P;
P.check();
// (You can use …Run Code Online (Sandbox Code Playgroud)