我有以下代码片段:
public class Example {
private Integer threshold;
private Map<String, Progress> history;
protected void activate(ComponentContext ctx) {
this.history = Collections.synchronizedMap(new LinkedHashMap<String, Progress>() {
@Override
protected boolean removeEldestEntry(Map.Entry<String, Progress> entry) {
return size() > threshold;
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
Theres是匿名LinkedHashMap类和Example类之间的循环依赖.这样可以吗?为什么不?它会被垃圾收集器很好地收回吗?
我知道它可以用Java完成,因为我过去曾广泛使用过这种技术.Java中的一个例子如下所示.(附加问题.这种技术被称为什么?如果没有名称,很难找到这样的例子.)
public abstract class Example {
public abstract void doStuff();
}
public class StartHere{
public static void main(string[] args){
Example x = new Example(){
public void doStuff(){
System.out.println("Did stuff");
}
};
x.doStuff();
}
}
Run Code Online (Sandbox Code Playgroud)
现在,我的主要问题是,这也可以在C#中完成,如果是这样,怎么样?
是否可以在Objective-C中声明Delegates之类的匿名实现.我想我的术语是正确的,但这是一个java示例:
myClass.addListener(new FancyInterfaceListener({
void onListenerInterestingAction(Action a){
....interesting stuff here
}
});
Run Code Online (Sandbox Code Playgroud)
因此,例如,处理一个UIActionSheet调用我必须声明另一个方法在同一个班,如果我想通过它的数据,这似乎有点傻,因为我必须将这些数据保存为一个全局变量.以下是使用确认对话框删除内容的示例,询问您是否确定:
-(void)deleteItem:(int)indexToDelete{
UIActionSheet *confirm = [[UIActionSheet alloc] initWithTitle:@"Delete Item?" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Delete" otherButtonTitles:nil];
[confirm showInView:self.view];
[confirm release];
}
Run Code Online (Sandbox Code Playgroud)
和同一类中的UIActionSheetDelegate:
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0){
[[Settings sharedSettings] removeItemAtIndex:/*need index variable here*/];
[drinksTable reloadData];
}
}
Run Code Online (Sandbox Code Playgroud)
我想要做的是将其声明为内联,就像我在顶部的java示例中所做的那样.这可能吗?
在那里有一个内部问题,为什么java匿名类不能同时实现和子类?或者只是因为语法?
我在哪里可以使用,我应该使用PHP 7中提供的匿名类?我找不到他们的用例.
$message = (new class() implements Message {
public function getText() { return "Message"; }});
Run Code Online (Sandbox Code Playgroud) 我正在调查Guava图书馆,我遇到了一个空洞的匿名内部课程TypeToken.
TypeToken<List<String>> stringListTok = new TypeToken<List<String>>() {};
Run Code Online (Sandbox Code Playgroud)
什么是空匿名内部类的确切使用以及哪些有用的场景可以提供帮助?
我使用一个实例化另一个匿名类的方法实例化一个匿名类,并且从这个内部匿名类我想调用一个属于外部匿名类的方法.为了说明它,假设我有这个界面:
interface ReturnsANumber {
int getIt();
}
Run Code Online (Sandbox Code Playgroud)
然后,在我的代码中的某个地方,我这样做:
ReturnsANumber v = new ReturnsANumber() {
int theNumber() {
return 119;
}
public int getIt() {
// In a modern version of Java, maybe I could do
// var a = this;
// and then call a.theNumber();
ReturnsANumber w = new ReturnsANumber() {
int theNumber() {
return 1;
}
public int getIt() {
return this.theNumber();
}
};
return w.getIt();
}
};
System.out.println("The number is " + v.getIt());
Run Code Online (Sandbox Code Playgroud)
问题:
在最里面的方法中getIt,我想调用theNumber() …
在ScheduledExecutorService中运行时,是否有一种很好的方法可以阻止任务内部重复任务?
可以说,我有以下任务:
Future<?> f = scheduledExecutor.scheduleAtFixedRate(new Runnable() {
int count = 0;
public void run() {
System.out.println(count++);
if (count == 10) {
// ??? cancel self
}
}
}, 1, 1, TimeUnit.SECONDS);
Run Code Online (Sandbox Code Playgroud)
从外面看,很容易通过f.cancel()取消,但是如何在指定的地方停止重复?(通过AtomicReference传递Future是不安全的,因为当scheduleAtFixedRate返回f迟到且变量设置得太晚时,有一个潜在的窗口,并且任务本身可能已经运行,在引用中看到null.)
java concurrency anonymous-class executorservice variable-initialization
我有一个使用Java 8新流功能的示例代码(获取一系列int值1 .. 20,跳过前9个,然后取剩余10个,每个int值:减1并乘以2).
System.out.println(Arrays.toString(
IntStream.rangeClosed(1, 20).skip(9).limit(10).map((new IntUnaryOperator() {
@Override
public int applyAsInt(int operand) {
return operand - 1;
}
}).andThen(new IntUnaryOperator() {
@Override
public int applyAsInt(int operand) {
return operand * 2;
}
})).toArray()));
Run Code Online (Sandbox Code Playgroud)
输出如下:
[18, 20, 22, 24, 26, 28, 30, 32, 34, 36]
Run Code Online (Sandbox Code Playgroud)
现在我想用Lambda表达式替换匿名类.以下转换工作正常(第二个匿名类替换为i -> i * 2lambda表达式),我得到相同的输出:
System.out.println(Arrays.toString(
IntStream.rangeClosed(1, 20).skip(9).limit(10).map((new IntUnaryOperator() {
@Override
public int applyAsInt(int operand) {
return operand - 1;
}
}).andThen(i -> i * 2)).toArray()));
Run Code Online (Sandbox Code Playgroud)
但是,当我用lambda表达式替换第一个匿名类时:
System.out.println( …Run Code Online (Sandbox Code Playgroud) anonymous-class ×10
java ×6
c# ×2
java-8 ×2
abstract ×1
concurrency ×1
delegates ×1
guava ×1
lambda ×1
methods ×1
objective-c ×1
php ×1
php-7 ×1