最后的代码产生编译错误:
NotApplicable.java:7: run() in cannot be applied to (int)
run(42);
^
1 error
Run Code Online (Sandbox Code Playgroud)
问题是为什么?为什么javac认为我调用run(),并且找不到run(int bar)?它正确地称为foo(int bar).为什么我必须使用NotApplicable.this.run(42);?这是一个错误吗?
public class NotApplicable {
public NotApplicable() {
new Runnable() {
public void run() {
foo(42);
run(42);
// uncomment below to fix
//NotApplicable.this.run(42);
}
};
}
private void run(int bar) {
}
public void foo(int bar) {
}
}
Run Code Online (Sandbox Code Playgroud) 在Java中,我喜欢使用诸如的结构
List<String> list = new ArrayList<String>() {{add("foo");}};
Run Code Online (Sandbox Code Playgroud)
有没有办法在C#的1行中做到这一点呢?
我想使用a PriorityQueue在图形上进行拓扑排序.为简洁起见,我想为比较器使用匿名内部类.但是,我需要访问图表g以确定我正在查看的节点的程度.这可能吗?
/**
* topological sort
* @param g must be a dag
*/
public static Queue<String> topoSort(DirectedGraph<String, DefaultEdge> g) {
Queue<String> result = new PriorityQueue<String>(g.vertexSet().size(),
new Comparator<String>() {
DirectedGraph<String, DefaultEdge> g;
@Override
public int compare(String arg0, String arg1) {
if (g.inDegreeOf(arg0) < g.inDegreeOf(arg1)) {
return -1;
}
if (g.inDegreeOf(arg0) > g.inDegreeOf(arg1)) {
return 1;
}
return 0;
}
});
result.addAll(g.vertexSet());
return result;
}
Run Code Online (Sandbox Code Playgroud)
正确的代码
/**
* topological sort
* @param g must be a dag
*/ …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用一些技术来优化我的ASP.NET MVC应用程序,这些技术包括调整URL生成:http://www.chadmoran.com/blog/2009/4/23/optimizing-url-generation-in-aspnet- MVC-部分2.HTML
如果使用RouteValueDictionary代替匿名类之间的速度差异很大,那么在定义html属性时我是否还应该使用Dictionary代替匿名类?
例如,我应该这样做:
Html.ActionLink("LinkName", "Action", "Controller",
new RouteValueDictionary { { "id", Model.Id } },
new { @class = "someCSSClass" })
Run Code Online (Sandbox Code Playgroud)
或者我应该通过这样做进一步优化:
Html.ActionLink("LinkName", "Action", "Controller",
new RouteValueDictionary { { "id", Model.Id } },
new Dictionary<string, object> { { "class", "someCSSClass" } })
Run Code Online (Sandbox Code Playgroud)
我知道使用Url.Action更快,或者更好地使用RouteLink技术,但我只是想知道为了速度,是否应该完全避免使用匿名类.
如何instance variables从匿名类的方法中访问?
class Tester extends JFrame {
private JButton button;
private JLabel label;
//..some more
public Tester() {
function(); // CALL FUNCTION
}
public void function() {
Runnable r = new Runnable() {
@Override
public void run() {
// How do I access button and label from here ?
}
};
new Thread(r).start();
}
}
Run Code Online (Sandbox Code Playgroud) 我正在使用testNg和Mockito编写单元案例。我面临的问题是以下给出的方法:
public void publishRequest(final NotificationRequest request){
MessageCreator messageCreator = new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
if (logger.isDebugEnabled()) {
logger.debug("Start of MessagePublisher publishRequest");
}
ObjectMessage msg = session.createObjectMessage();
msg.setStringProperty(
"SELECTOR",
request.getAvailaibilityTier() + "-"
+ request.getEsbReference());
if (logger.isDebugEnabled()) {
logger.debug("Message Selector=>"+msg.getStringProperty("SELECTOR"));
}
msg.setStringProperty(NotificationConstants.CACHE_KEY.name(),
request.getId());
msg.setObject(request.getData());
if (logger.isDebugEnabled()) {
logger.debug("Publishing request -->" + request);
}
if (logger.isDebugEnabled()) {
logger.debug("End of MessagePublisher publishRequest");
}
return msg;
}
};
producerTemplate.send(messageCreator);
}
Run Code Online (Sandbox Code Playgroud)
在编写单元案例时,即使我的测试案例创建了MessageCreater对象,也不会调用createMessage()方法。我想在单位案例中也包括在内,以获得更好的覆盖范围。任何想法如何测试内部类的方法?
我可以这么说吗?
匿名类看起来像OO样式,而lambda表达式使它成为功能样式.
是否在编译时在运行时或提前创建了匿名Java类?
根据Java文档,They are like local classes except that they do not have a name所以我的猜测是它们是提前创建的.如果你能引用你的消息来源或知道如何测试这样的东西,请告诉我!
我有一个棘手的情况,我想从代码的角度优化.有没有办法通过Lambda/Java8表达式缩短以下方法?
// total amount of audiences
registry.register("metric.persons.total", new CachedGauge<Integer>(1,TimeUnit.MINUTES) {
@Override
protected Integer loadValue() {
return personService.findAll().size();
}
});
Run Code Online (Sandbox Code Playgroud)
CachedGauge类看起来像这样:
public abstract class CachedGauge<T> implements Gauge<T> {
protected CachedGauge(long timeout, TimeUnit timeoutUnit) {
...
}
protected abstract T loadValue();
...
}
}
Run Code Online (Sandbox Code Playgroud)
看看是否有办法真的很棒,这里棘手的部分是有一个默认的构造函数,并且类是参数化的.
最好的,周五
我不是Java程序员,但必须做一些Java工作.从一些谷歌搜索,这似乎是一个匿名的子类.但是后面的对象后跟"[]".但是再次声明变量是一个Object.这是否与允许对象数组的最通用类型(Object)相反?
Object thing = new Object[] {someInt, anotherInt, someInterface, someString};
Run Code Online (Sandbox Code Playgroud) anonymous-class ×10
java ×9
lambda ×2
asp.net-mvc ×1
c# ×1
class ×1
graph ×1
html ×1
java-8 ×1
javac ×1
methods ×1
runtime ×1
unit-testing ×1
url ×1