我经常看到代理调用的代码示例如下所示:
`
public delegate void DelegateThreadActivity<T, U> (T sender, U e);
public event DelegateThreadActivity<Thread, System.EventArgs> OnStart = null;
public event DelegateThreadActivity<Thread, System.EventArgs> OnStop = null;
// Helper method for invocation.
public void RaiseOnStart ()
{
DelegateThreadActivity<Thread, System.EventArgs> instance = null;
try
{
instance = this.OnStart;
// OR
instance = (DelegateThreadActivity) (object) this.OnStart;
if (instance != null)
{
instance(this, System.EventArgs.Empty);
}
}
catch
{
}
}
Run Code Online (Sandbox Code Playgroud)
`
为什么要使用这个[instance]
对象?起初我认为这是公司惯例,但看到经验丰富的开发人员也这样做.有什么好处?
希望有人可以帮我打破Crockford JS Good Parts的一小段代码:
Function.method('new', function ( ) {
// Create a new object that inherits from the
// constructor's prototype.
var that = Object.create(this.prototype);
// Invoke the constructor, binding –this- to
// the new object.
var other = this.apply(that, arguments);
// If its return value isn't an object,
// substitute the new object.
return (typeof other === 'object' && other) || that;
});
Run Code Online (Sandbox Code Playgroud)
我不理解的部分是他使用apply调用模式创建一个对象:
var other = this.apply(that, arguments);
Run Code Online (Sandbox Code Playgroud)
如何执行此函数将创建新对象?
如果该功能将是:
var f = function (name) { …
Run Code Online (Sandbox Code Playgroud) 代码看起来如何创建类的对象:
string myClass = "MyClass";
Run Code Online (Sandbox Code Playgroud)
以上类型,然后调用
string myMethod = "MyMethod";
Run Code Online (Sandbox Code Playgroud)
那个对象?
目前我正试图像这样调用它:
class Test {
public static void test() {
System.out.println("hi");
}
public static void main(String[] args) {
Test t = null;
t.test();
}
}
Run Code Online (Sandbox Code Playgroud)
代码的输出是hi
不知道很多关于积木的事情.你会如何去模仿重复NSTimer
使用dispatch_after()
?我的问题是我想在应用程序移动到后台时"暂停"一个计时器,但是子类NSTimer
似乎不起作用.
我尝试过似乎有用的东西.我无法判断其性能影响或是否可以大大优化.欢迎任何输入.
#import "TimerWithPause.h"
@implementation TimerWithPause
@synthesize timeInterval;
@synthesize userInfo;
@synthesize invalid;
@synthesize invocation;
+ (TimerWithPause *)scheduledTimerWithTimeInterval:(NSTimeInterval)aTimeInterval target:(id)aTarget selector:(SEL)aSelector userInfo:(id)aUserInfo repeats:(BOOL)aTimerRepeats {
TimerWithPause *timer = [[[TimerWithPause alloc] init] autorelease];
timer.timeInterval = aTimeInterval;
NSMethodSignature *signature = [[aTarget class] instanceMethodSignatureForSelector:aSelector];
NSInvocation *aInvocation = [NSInvocation invocationWithMethodSignature:signature];
[aInvocation setSelector:aSelector];
[aInvocation setTarget:aTarget];
[aInvocation setArgument:&timer atIndex:2];
timer.invocation = aInvocation;
timer.userInfo = aUserInfo;
if (!aTimerRepeats) {
timer.invalid = YES;
}
[timer fireAfterDelay];
return timer;
}
- (void)fireAfterDelay {
dispatch_time_t delay = …
Run Code Online (Sandbox Code Playgroud) objective-c invocation nstimer grand-central-dispatch objective-c-blocks
在我的应用程序中,我的代码看起来像 -
request.setParameter("a",false);//line1
request.setParameter("b",someObject);//line2
request.setParameter("c",someObject);//line3
request.setParameter("d",someObject);//line4
Run Code Online (Sandbox Code Playgroud)
我已经模拟了请求对象,现在我想验证是否调用了line3.在我试过的测试类中,我不关心其他行
Mockito.verify(request).setParameter("c",someObject)
,但是我得到了错误 -
Argument(s) are different! Wanted:
request.setParameter("c",com.test.MyObject@fec107);
-> at XXX
Actual invocation has different arguments:
request.setParameter("a", false);
Run Code Online (Sandbox Code Playgroud)
出于某种原因,Mockito比较了第一次调用和抛出错误
我最近改变了我的项目名称,然后当我运行时,我得到了Apple Mach-O Linker错误.
ld: file not found: /Users/Ajay/Library/Developer/Xcode/DerivedData/TapAway-dzdhwbcfttcdcceccpophqtqgmvl/Build/Products/Debug-iphonesimulator/Speedy.app/Speedy
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Run Code Online (Sandbox Code Playgroud)
我不知道为什么会这样,任何人都可以帮助我,我真的很新.
感谢所有帮助过的人.请具体一点我不是那么好.
再次感谢 :)
我刚刚阅读了Marko Rodriguez 关于不同类型数据库的优秀博客文章.虽然阅读我发现了一些语法......
// put data
db.put('marko');
db.put(31);
db.put(true);
// get data
Iterator results = db.get();
Iterator filteredResults = db.get{it.startsWith('ma')};
Run Code Online (Sandbox Code Playgroud)
...我认为它是Java的片段,但我从来没有见过像这样使用花括号调用的方法 - db.get{it.startsWith('ma')}
.
任何细节/想法将不胜感激!
有一些invokeMethod
接受(java.lang.String method_name, Object[] params, Class[] params_classes)
你会如何调用签名的方法void MyMethod()
?
Scala允许在没有括号的情况下调用没有参数列表的函数:
scala> def theAnswer() = 42
theAnswer: ()Int
scala> theAnswer
res5: Int = 42
Run Code Online (Sandbox Code Playgroud)
我将如何构造一个scala表达式来计算函数theAnswer
本身,而不是结果theAnswer
?或者换句话说,我将如何修改表达式theAnswer
,以便结果是类型() => Int
,而不是类型Int
?
invocation ×10
java ×3
.net ×2
c# ×2
methods ×2
apply ×1
arguments ×1
clr ×1
convention ×1
delegates ×1
dynamic ×1
file ×1
javascript ×1
linker ×1
mach-o ×1
mockito ×1
nstimer ×1
objective-c ×1
parameters ×1
scala ×1
static ×1
types ×1
verify ×1