我对此代码有疑问
public Car {
public static void m1(){
System.out.println("a");
}
public void m2(){
System.out.println("b");
}
}
class Mini extends Car {
public static void m1() {
System.out.println("c");
}
public void m2(){
System.out.println("d");
}
public static void main(String args[]) {
Car c = new Mini();
c.m1();
c.m2();
}
}
Run Code Online (Sandbox Code Playgroud)
我知道多态不适用于静态方法,只适用于实例方法.并且覆盖不适用于静态方法.
因此我认为这个程序应该打印出来:c,d
因为c调用m1方法,但它是静态的,所以它不能覆盖并且它在类Mini而不是Car中调用方法.
它是否正确?
但是,我的教科书说答案应该是:a,d
这是一个错字吗?因为我现在有点困惑.
请清楚这一点,谢谢.
在Java规范(http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.9)中,new具有以下形式:
ClassInstanceCreationExpression ::=
| new TypeArguments_opt TypeDeclSpecifier TypeArgumentsOrDiamond_opt
( ArgumentListopt ) ClassBodyopt
| Primary . new TypeArguments_opt Identifier TypeArgumentsOrDiamond_opt
( ArgumentListopt ) ClassBodyopt
Run Code Online (Sandbox Code Playgroud)
新的后面的第一个可选类型参数列表的目的是什么?我无法从我对15.9节的阅读中找到它(对类型参数列表的所有引用似乎都引用了类型/标识符之后的列表).测试标准Java编译器上的随机位会产生令人困惑的结果:
public class Foo<T> { }
// ...
Foo<Integer> t1 = new <Integer> Foo<Integer>(); // works
Foo<Integer> t2 = new <Integer> Foo(); // works -- unchecked warning missing the type arg after Foo
Foo<Integer> t3 = new <Boolean> Foo<Integer>(); // works
Foo<Integer> t4 = new <Float, Boolean> Foo<Integer>(); // works
Foo<Integer> t5 = new <NotDefined> …Run Code Online (Sandbox Code Playgroud) 请...有人能解释一下使用以下弹簧切入点指示符之间有什么区别吗?
使用"切入点指示符":
<aop:pointcut expression="within(my.app.dao.impl.*)" id="commonDaoOperation"/>
Run Code Online (Sandbox Code Playgroud)
使用"执行切入点指示符":
<aop:pointcut expression="execution(public * my.app.dao.impl.*.*(..))" id="commonDaoOperation"/>
Run Code Online (Sandbox Code Playgroud)
我在我的web项目中使用第二个(我认为它是最常用的),我用这种方法发现的问题是它在堆中消耗了大量内存......
在使用"eclipse内存分析器"分析应用程序服务器的"堆转储"后,我发现我的应用程序消耗了450 MB,并且该类的实例"org.springframework.aop.aspectj.AspectJExpressionPointcut"正在消耗这些450 MB的30%......
每个实例AspectJExpressionPointcut占用6 MB(大约),这是因为每个实例都保留了与java.lang.reflect.Method实例匹配的缓存,并且令人惊讶的是有很多java方法被缓存(我的切入点表达式没有提到的方法).
在阅读Spring Documentation之后,我决定使用第一种方法(在切入点指示符内),现在每个实例AspectJExpressionPointcut占用的内存都少得多.
问题是关于......他们之间的表现有什么不同......
提前谢谢了...
我正在用Gson解析简单的JSON对象.我希望它在重复密钥名称时抛出一些错误.例如
{
a: 2,
a: 3
}
Run Code Online (Sandbox Code Playgroud)
在我的例子中,Gson解析这样的JSON并将a设置为3.我希望它抛出一些异常.
我知道我可以将JSON解析为map,然后Gson会在这种情况下抛出异常,但前提是复制的键没有嵌套在map中.如果我有像这样的JSON:
{
a: 2,
b: {
dup: 1,
dup: 2
}
}
Run Code Online (Sandbox Code Playgroud)
仍然,它被解析没有任何异常,我只有一个值为2的"dup".
我可以以某种方式设置Gson在这种情况下抛出错误吗?或者在JsonObject实例中有重复的条目,以便我自己可以检测到它(但我怀疑它,因为它会无效JsonObject)
可重复的例子
String json = "{\"a\":2, \"a\":3}";
Gson gson = new Gson();
JsonObject jsonObject = gson.fromJson(json, JsonObject.class);
System.out.println(jsonObject);
Run Code Online (Sandbox Code Playgroud)
打印出来
{"a":3}
Run Code Online (Sandbox Code Playgroud) @Autowired 可以与构造函数,setter和类变量一起使用.
如何@Autowired在方法或任何其他范围内使用注释.我尝试了以下,但它产生编译错误.例如
public classs TestSpring {
public void method(String param){
@Autowired
MyCustomObjct obj;
obj.method(param);
}
}
Run Code Online (Sandbox Code Playgroud)
如果这是不可能的,还有其他方法可以实现吗?(我用过Spring 4.)
我需要通常以http:\ somewebsite.com\somepage.asp格式添加URL.当我使用上面的URL创建一个字符串并将其添加到JSON对象json时
运用
json.put("url",urlstring);
Run Code Online (Sandbox Code Playgroud)
它附加了一个额外的"\",当我检查输出时就像是 http:\\\\somewebsite.com\\somepage.asp
当我给出http://somewebsite.com/somepage.asp
json输出的URL时http:\/\/somewebsite.com\/somepage.asp
你能帮我检查一下这个URL吗?
谢谢
可能重复:
如何以正确的方式打印对象内容?
我需要能够在我的数组列表中打印出Student对象(所有变量).这可能吗?当我尝试打印时输出这种东西,例如student.Student@82701e.我认为这是hexadecimal什么的
这是我的代码:
package student;
public class Student {
private String studentName;
private String studentNo;
private String email;
private int year;
public Student() {
this.studentName = null;
this.studentNo = null;
this.email = null;
this.year = -1;
}
public Student(String nName, String nNum, String nEmail, int nYr) {
this.studentName = nName;
this.studentNo = nNum;
this.email = nEmail;
this.year = nYr;
}
public void setStudentName(String newStudentName) {
this.studentName = newStudentName;
}
public void setStudentNo(String newStudentNo) {
this.studentNo …Run Code Online (Sandbox Code Playgroud) 我AbstractMethodError在调用一个方法时会收到一个,我认为该方法应该在目标实例中有一个默认实现.
我在三个参数中创建了一个功能接口,但也派生自java.util.function.Function并提供了一个默认实现Function#apply(..).然后我使用3参数lambda表达式创建我的接口实例.3参数方法都Function#apply(..)可以在创建的实例中正常工作.
当我将创建的实例传递给期望我的接口的方法时,我可以Function#apply(..)从方法中调用它并且它工作正常.
但是当我将实例传递给期望a的方法时Function,我会在AbstractMethodError尝试调用时收到Function#apply(..).
我似乎错过了对默认方法如何以及何时绑定到实例的理解.我做错了什么?
展示:
package spike;
import java.util.function.BiFunction;
import java.util.function.Function;
public class ReductionProblem {
interface F3<T, U, V, R> extends Function<T, BiFunction<U, V, R>> {
default BiFunction<U, V, R> apply(final T t) {
return (U u, V v) -> apply(t, u, v);
}
R apply(T t, U u, V v);
}
private static <T, U, V, R> BiFunction<U, V, R> workingReduce(
F3<T, …Run Code Online (Sandbox Code Playgroud) 使用vlcj组件时,自定义组件将作为AOP代理对象null的结果出现.
public class MediaList {
private libvlc_media_list_t mediaListInstance;
public MediaList(LibVlc libvlc, libvlc_instance_t instance, libvlc_media_list_t mediaListInstance) {
this.libvlc = libvlc;
this.instance = instance;
createInstance(mediaListInstance);
}
private void createInstance(libvlc_media_list_t mediaListInstance) {
logger.debug("createInstance()");
if(mediaListInstance == null) {
mediaListInstance = libvlc.libvlc_media_list_new(instance);
}
else {
libvlc.libvlc_media_list_retain(mediaListInstance);
}
this.mediaListInstance = mediaListInstance; // <- assignment
logger.debug("mediaListInstance={}", mediaListInstance);
mediaListEventManager = libvlc.libvlc_media_list_event_manager(mediaListInstance);
logger.debug("mediaListEventManager={}", mediaListEventManager);
registerEventListener();
}
public final libvlc_media_list_t mediaListInstance() {
return mediaListInstance; // <- proxy object return null, if use aop
}
}
Run Code Online (Sandbox Code Playgroud)
我现在将我CompletableFuture<X>来CompletableFuture<Void>,如下图所示,但我不知道是否有一个更好的办法.
@Override
public CompletableFuture<Void> packetEncrypted(ByteBuffer engineToSocketData) {
return realChannel.write(engineToSocketData).thenApply(c -> empty());
}
public Void empty() {
return null;
}
Run Code Online (Sandbox Code Playgroud)