是否可以反射地覆盖给定类的实例的方法?
前提条件:游戏具有覆盖能力的方法act().
public class Foo {
public Method[] getMethods() {
Class s = Game.class;
return s.getMethods();
}
public void override()
{
Method[] arr = getMethods()
for (int i = 0; i<arr.length; i++)
{
if (arr[i].toGenericString().contains("act()")
{
// code to override method (it can just disable to method for all i care)
}
}
}
}
Run Code Online (Sandbox Code Playgroud) private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int count = jSlider1.getValue();
int delay = jSlider2.getValue();
int valueOfSlider = jSlider2.getValue();
int valueOfSlider2 = jSlider1.getValue();
while (count > 0)
{
count--;
String count2 = ""+count;
jLabel3.setText(count2);
try {Thread.sleep(delay); }
catch (InterruptedException ie) { }
}
Run Code Online (Sandbox Code Playgroud)
它最终将显示jLabel上的最终数字,但它不会逐步更新数字.任何帮助
package puzzle;
import java.awt.Color;
import javax.swing.*;
import java.util.*;
public class Puzzle {
Integer x = 50;
Integer y = 50;
int x2 = 100;
int y2 = 100;
int vnotx = 0;
int vnoty = 0;
float t = 0;
float t2 = 0;
JFrame frame = new JFrame();
Move m = new Move(x, y,Color.GREEN);
Move n = new Move(x,y,Color.ORANGE);
java.util.Timer timer = new java.util.Timer();
java.util.Timer timer2 = new java.util.Timer();
public Puzzle() {
frame.setUndecorated(true);
frame.setSize(400, 400);
frame.add(m);
frame.add(n);
frame.addKeyListener(new java.awt.event.KeyAdapter() …Run Code Online (Sandbox Code Playgroud) 我有一个自定义类MDRect,我正在尝试添加NSMutableArray
数组是属性:
@property (retain) NSMutableArray* array;
Run Code Online (Sandbox Code Playgroud)
它在NSView子类的initMethod中初始化:
-(id)init {
array = [NSMutableArray new];
return [super init];
}
Run Code Online (Sandbox Code Playgroud)
然后我试图在这里添加一个对象:
-(void)mouseUp:(NSEvent *)theEvent
{
NSPoint mouseLoc = [NSEvent mouseLocation];
mouseLoc = [self mouse:mouseLoc inRect:[self frame]];
CGSize temp;
NSLog(@"%f",mouseLoc.y - mouseLocation.y);
NSLog(@"%f",mouseLoc.x - mouseLocation.x);
temp.height = mouseLoc.y - mouseLocation.y;
temp.width = mouseLoc.x - mouseLocation.x;
tempRect.size = temp;
MDRect * rect = [[MDRect alloc] initWithColor:[NSColor orangeColor] andRect:tempRect];
[array addObject:rect];
int i = (int)array.count;
NSLog(@"%i",i);
[self setNeedsDisplay:YES];
}
Run Code Online (Sandbox Code Playgroud)
但是没有将对象添加到数组中.它永远不会返回NSLog函数中除0以外的任何值.我究竟做错了什么?
java ×4
swing ×2
cocoa ×1
concurrency ×1
graphics2d ×1
nsarray ×1
objective-c ×1
overriding ×1
reflection ×1
thread-sleep ×1