我想知道是否SwingWorker必须是我的主GUI中的嵌套类.我宁愿把它作为一个外部类来保持GUI与我的任何程序逻辑清晰.
我试图使SwingWorker类外部,这适用于该过程,遗憾的是我无法从SwingWorker类访问我的任何GUI字段.每当我尝试从SwingWorker的done()方法中访问属性(如标签或其他内容)时,我都会收到nullPointer异常.
任何建议将不胜感激!
首先非常感谢Jeff!到目前为止工作正常,即使我无法关注你提出的第二个选项.我的一个后台任务计算一定的大小(长值),所以从我的GUI中获取该值会很好.
你建议使用getter和setter但不幸的是我不知道如何在SwingWorker类中实现它们.
我试过这样的:
public void setSize(long totalSize) {
this.totalSize = totalSize;
}
public long getTotalSize() {
return totalSize;
}
Run Code Online (Sandbox Code Playgroud)
在doInBackground()方法结束时调用setter .不幸的是我无法使用get()GUI中的方法.
final MySwingWorker w = new MySwingWorker();
Runnable r = new Runnable() {
public void run() {
// do something with w.get()
}
};
w.setRunnable(r);
w.execute();
Run Code Online (Sandbox Code Playgroud)
在我的情况下,"w"的对象创建不起作用,因为构造函数需要Runnable的对象.我错过了什么吗?请放轻松,这是我第一次使用SwingWorker.:)再次,非常感谢你的帮助!
经过一段时间的调试后,我使用enable_if跟踪了出现问题的原因以及一些意外的模板专业化结果:
以下代码使Visual Studio 2010(和2008)在DoTest()中的声明失败,而在g ++ 3.4.5中则没有。但是,当我从SomeClass中删除模板或将my_condition移出SomeClass的范围时,它也可以在MSVC中使用。
这段代码是否有问题(至少部分地)解释了此行为,或者这是MSVC编译器中的错误?
(使用此示例代码,对于boost和c ++ 0x stl版本是相同的)
#include <cassert>
#include <boost\utility\enable_if.hpp>
template <class X>
class SomeClass {
public:
template <class T>
struct my_condition {
static const bool value = true;
};
template <class T, class Enable = void>
struct enable_if_tester {
bool operator()() { return false; }
};
template <class T>
struct enable_if_tester<T, typename boost::enable_if< my_condition<T> >::type> {
bool operator()() { return true; }
};
template <class T>
void DoTest() …Run Code Online (Sandbox Code Playgroud) 我一直在玩Paperclip来建立一个照片库/商店.图库有很多照片,照片属于图库,用户可以有很多图库.回形针默认值类似于/:class/:style/:basename.:extension.但是,通过图库设置,我更喜欢像/:class/:user_name/:gallery_name/:styles/:basename.:extension.我还没有找到一种方法来访问对象中的变量,以便动态创建这些存储位置.
有没有办法做到这一点?
我尝试在路径中使用#{variable},但这不起作用.这些照片对象是使用@ gallery.photos.build创建的,因此gallery_id应该已经具有可访问的值.
LinqPad中定义的类是嵌套的UserQuery内部类.有没有办法声明作为根类的类?
来自维基百科关于Lambda函数和表达式的文章:
用户通常希望在他们进行算法函数调用的地方附近定义谓词函数.该语言只有一种机制:在函数内部定义类的能力.... 函数中定义的类不允许在模板中使用它们
这是否意味着在C++ 0x lambda到位后,在函数内部使用嵌套结构会被默默地弃用?
另外,上段最后一行的含义是什么?我知道嵌套类不能template; 但那条线并不意味着.
Local C++,Inner class和Nested类在C++中是否意味着相同的东西?
我有Class1和class2,它在class1,VB.NET代码中:
Public Class class1
Public varisbleX As Integer = 1
Public Class class2
Public Sub New()
'Here GET the value of VariableX
End Sub
End Class
Public Sub New()
Dim cls2 As New class2
End Sub
End Class
Run Code Online (Sandbox Code Playgroud)
我想从class2访问varisbleX,在VB.net或C#中的代码表示赞赏,谢谢.
我正在尝试建立一个类似于c#属性概念的语法糖.
我读过这篇文章:原生c ++中的C#属性?.这很有帮助,但缺乏我想要的设计.我还恭敬地不同意那里的几个断言,即属性概念是不好的形式,因为我没有看到一组标题为get_id()和set_id()的方法与暴露相同概念的运算符重载之间的区别,允许代码在使用类时更干净,并且通常不鼓励公共访问私有字段,也将公共实现与私有设计分离.
但是,我提出的代码(受该链接的启发)真的很糟糕,并且很难维护.我想知道是否有人有任何建议来清理它,或者更重要的是,知道更好的方法来做到这一点.
class someClass
{
public:
struct someProperty_struct{
virtual someProperty_struct& operator=(const int&){return *this;}
virtual operator int(){return 0;}
}someProperty;
};
class derivedClass : someClass
{
int i;
public:
struct intProperty: someClass::someProperty_struct
{
protected:
friend class derivedClass;
derivedClass *outer;
public:
virtual someProperty_struct& operator=(const int& value){
outer->i = value;
return *this;}
virtual operator int(){return outer->i;}
};
derivedClass()
{
intProperty p = intProperty();
p.outer = this;
someProperty = p;
}
};
class consumerClass
{
public:
someClass* data;
consumerClass() …Run Code Online (Sandbox Code Playgroud) 当我正想通过这个文章,下部分私人会员超类中,我看到了这条线
嵌套类可以访问其封闭类的所有私有成员,包括字段和方法。因此,子类继承的公共或受保护的嵌套类可以间接访问超类的所有私有成员。
我的问题是我们如何才能直接访问Nested类Base中Derived(就像我们可以访问任何public,protected字段)?
和
如果有一种方法,怎么能Derived访问p它的私有字段Base通过Nested?
public class Base {
protected int f;
private int p;
public class Nested {
public int getP() {
return p;
}
}
}
class Derived extends Base {
public void newMethod() {
System.out.println(f); // i understand inheriting protected field
// how to access the inherited Nested class here? and if accessed how to …Run Code Online (Sandbox Code Playgroud) 我正在尝试在学习编程时第一次创建命名空间.我希望有一种解决我遇到的问题的方法并不是特别凌乱,但实质上我有一个类对象,它保存两个嵌套类对象的两个字典.我需要能够将NestedClassA的Dictionary传递给NestedClassB,或者允许NestedClassB以某种方式访问它......这是我正在尝试做的一个例子:
namespace MyFirstNamespace
{
public class BossClass
{
public Dictionary<int, NestedClassA> DictionaryA = new Dictionary<int, NestedClassA>();
public Dictionary<int, NestedClassB> DictionaryB = new Dictionary<int, NestedClassB>();
public class NestedClassA { ...arbitrary class definition... }
public class NestedClassB
{
public Dictionary<int, NestedClassA> PassedDictionary;
public NestedClassB() { }
public NestedClassB(Dictionary<int, NestedClassA> tempDic))
{
PassedDictionary = tempDic;
}
}
public BossClass() { ... arbitrary constructor ... }
...arbitrary dictionary population methods...
function void CreateAClassBInstance()
{
DictionaryB[n] = new NestedClassB(n, DictionaryA);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题似乎是我不能在"NestedClassB"中对"NestedClassA"进行类型转换,因为它无法识别该类型.是否可以访问B中的"NestedClassA"类型?我尝试过的任何东西都没有效果.我是否必须传递"BossClass"的实例,以便我可以通过" Dictionary<int, …
nested-class ×10
c++ ×4
inheritance ×2
java ×2
.net ×1
c# ×1
c++11 ×1
enable-if ×1
lambda ×1
linqpad ×1
local-class ×1
paperclip ×1
path ×1
properties ×1
pure-virtual ×1
root ×1
swingworker ×1
templates ×1
vb.net ×1