我已经看到了调用可怕的C函数和获取必须迭代的结构的示例(特别是这里),充满了大量的underbars.
为什么我不能这样做(伪跟随):
Money *cost = [[Money alloc] init];
for (Property *property in [[cost class] properties]){
..
}
for (Method *method in [[cost class] methods]){
..
}
Run Code Online (Sandbox Code Playgroud)
?
说我有一个豆子
public class SomeBean{
List<String> messages;
List<Integer> scores;
String id;
int number;
Run Code Online (Sandbox Code Playgroud)
....}
我使用以下代码来处理或转储propreties
BeanInfo beanInfo = Introspector.getBeanInfo(beanClass, Object.class);
PropertyDescriptor descriptors[] = beanInfo.getPropertyDescriptors();
int stop = descriptors.length;
for (int i = 0; i < stop; ++i) {
PropertyDescriptor descriptor = descriptors[i];
logger.info(descriptor.getName() + " : " + descriptor.getPropertyType().getName() + ", writemethod :" + descriptor.getWriteMethod());
}
Run Code Online (Sandbox Code Playgroud)
我希望能够获得"分数"和"消息"的参数化类型.当我破坏代码时,"descriptor.getPropertyType().getName()"的值对于消息和分数都是"java.util.List".
如何判断"消息"的属性描述符是否指的List<String>是"分数"是指List<Integer>?
这可能看起来是一个愚蠢的问题(因为它可能是特定于项目的),但在这种情况下它足够普遍:
我正在运行一段代码,它给出了ClassCastException我写的标题错误信息.这里所提到的转换是从类型的对象java.lang.reflect.Type来Class<?>.
除了原始类型,Java中哪种类型不是类?
谢谢你的回答
我有一个单元测试:
- (void)testFetchTrackByTrackIdIsATrack
{
[self addTrackWithSongId:@"123"];
Track *fetchedTrack = [self.library trackByTrackId:@"123"];
assertThat(fetchedTrack, instanceOf([Track class]));
}
Run Code Online (Sandbox Code Playgroud)
哪个失败了:
file:///Users/elliot/Development/Hark/HarkTests/TestLibrary.m: test failure:
-[TestLibrary testFetchTrackByTrackIdIsATrack] failed: Expected an instance
of Track, but was Track instance <Track: 0x6180001077d0>
Run Code Online (Sandbox Code Playgroud)
我有几个其他测试使用相同的实例检查不同的类工作 - 但我无法锻炼为什么这不起作用.深入研究:
- (void)testFetchTrackByTrackIdIsATrack
{
[self addTrackWithSongId:@"123"];
Track *fetchedTrack = [self.library trackByTrackId:@"123"];
Class c1 = [fetchedTrack class];
Class c2 = [Track class];
}
Run Code Online (Sandbox Code Playgroud)
调试器报告:
c1 Class Track 0x0000000100012fe0
c2 Class 0x1000b3eb8 0x00000001000b3eb8
Run Code Online (Sandbox Code Playgroud)
注意它怎么看不到那[Track class]是一类Track?当我将这个相同的逻辑应用于其他传递的单元测试时,它们都会报告正确的类名.
感觉它在运行时没有类元数据,但为什么呢?
还有一些案例:
assertTrue(c1 == c2); // FAIL
assertThat([fetchedTrack …Run Code Online (Sandbox Code Playgroud) 我希望有类似的东西
def x():
print get_def_name()
Run Code Online (Sandbox Code Playgroud)
但不一定知道的名字x.
理想情况下,它将返回'x'x将是函数的名称.
让我们说我有一个命名空间。我想找到所有的命名空间过程。例如,假设我有以下代码:
namespace eval foo {
proc me {} { puts "yey!" }
proc moo {} { puts "ney!" }
}
puts [ getAllNamespaceProcs foo ] ; # getAllNamespaceProcs is what I do not know how to do
Run Code Online (Sandbox Code Playgroud)
这应该产生:
> { me moo }
Run Code Online (Sandbox Code Playgroud)
谢谢。
Suppose I have a class like this:
class A:
def __init__(self):
self.a = "a"
self.b = "b"
Run Code Online (Sandbox Code Playgroud)
How would I get a dictionary like this ?
{"a": "a", "b": "b"}
Run Code Online (Sandbox Code Playgroud)
I read this question and answers, but the dictionary in these answers always contains some "dunder" attributes as well, which I never defined in A.__init__. Will I have to use one of the solutions in the linked question and filter out the dunder attributes, or is there a …
java ×2
objective-c ×2
python ×2
reflection ×2
attributes ×1
class ×1
function ×1
generics ×1
namespaces ×1
ochamcrest ×1
proc ×1
python-2.7 ×1
python-3.x ×1
tcl ×1
unit-testing ×1