MyClass e = new MyClass();
List<Object> ok = new ArrayList<Object>();
List<? extends Object> ko = new ArrayList<Object>();
ok.add(e);
ko.add(e); // doesn't compile
Run Code Online (Sandbox Code Playgroud)
为什么不编译?MyClass不管是Object...... 的子类
有关信息,我收到以下消息:
The method add(capture#1-of ? extends Object) in the type List<capture#1-of ? extends Object> is not applicable for the arguments (MyClass)
Run Code Online (Sandbox Code Playgroud) public class MyClass {
private String s = "foo";
}
Run Code Online (Sandbox Code Playgroud)
是否可以"foo"使用反射而不必实例化 new MyClass?
Field field = MyClass.class.getDeclaredField("s");
// -- ideally: --
// Object initializationValue = field.getInitializationValue();
// assert initializationValue.equals("foo");
Run Code Online (Sandbox Code Playgroud) 在c ++中,您可以从任何普通的父类中轻松调用eny oveloaded方法.我是如何在Java中做那样思考的?
例:
class A {
public void f() {
System.out.println("A");
}
};
class B extends A {
@Override
public void f() {
super.f();
System.out.println("B");
}
};
class C extends B {
@Override
public void f() {
super.f(); // how to invoke f() method of A class avoiding B class?
System.out.println("C");
}
};
Run Code Online (Sandbox Code Playgroud)
因此调用cf()将打印"ABC".
如何从A类的C类方法调用避免B类的相同重载方法?
我有一个这样的字符串:
RegexMy RegexGoodregexNo need for Regex
Run Code Online (Sandbox Code Playgroud)
如何拆分它?
Regex, My Regex, Goodregex, No need for Regex
Run Code Online (Sandbox Code Playgroud)
我的尝试没有真正起作用,因为它也在一个空格后分裂:
var tmp = ("RegexMy RegexGoodregexNo need for Regex").split(/(?=[A-Z])/);
Run Code Online (Sandbox Code Playgroud) 关于.trigger()方法,Event对象,which属性,JS字符代码和下面的代码,为什么#example输入不会将char a作为自动写入值?我误解了这个.trigger()方法吗?
<input type="text" name="example" id="example" value="" />
<script>
$(function() {
var e = jQuery.Event("keydown", { which: 65 });
$("#example").focus().trigger(e);
});
</script>
Run Code Online (Sandbox Code Playgroud) 在@Controller搜索引擎内:
@RequestMapping(value = "/search/{query}", method = RequestMethod.GET)
public String search(@PathVariable String query) {}
Run Code Online (Sandbox Code Playgroud)
如果用户想要搜索/search/w?rld(wich应匹配world,warld,whrld等),则变量query等于w,因为问号表示GET var.
我试过了"/search/{query:.+}",但仍然无法正常工作.
知道如何解决这个问题吗?
从GLib参考手册的"正则表达式语法"部分,"原子分组和所有格量词"小节:
\d+foo在应用于字符串时考虑模式123456bar:在匹配所有6位数然后未匹配"foo"之后,匹配器的正常操作是再次尝试仅匹配\ d +项目的5位数,然后使用4,等等在最终失败之前.如果我们使用
(?>\d+)foo(称为原子分组)前一个例子,匹配器会在第一次未能匹配"foo"时立即放弃.当原子组的子模式只是一个重复项时,如上例所示,可以使用更简单的表示法,称为"占有量词":
\d++foo
我的问题是:有没有理由为什么star(*)重复运算符没有等价物?
Java中的示例:
final String in = "123456";
// "plus" (+)
System.out.println(in.matches("\\d+")); // true
System.out.println(in.matches("(?>\\d+)")); // true
System.out.println(in.matches("\\d++")); // true
// "star" (*)
System.out.println(in.matches("\\d*")); // true
System.out.println(in.matches("(?>\\d*)")); // true
System.out.println(in.matches("\\d**")); // exception
Run Code Online (Sandbox Code Playgroud)
异常堆栈跟踪是:
Exception in thread "main" java.util.regex.PatternSyntaxException: Dangling meta character '*' near index 3
\d**
^
at java.util.regex.Pattern.error(Pattern.java:1713)
at java.util.regex.Pattern.sequence(Pattern.java:1878)
at java.util.regex.Pattern.expr(Pattern.java:1752)
at java.util.regex.Pattern.compile(Pattern.java:1460)
at java.util.regex.Pattern.<init>(Pattern.java:1133)
at java.util.regex.Pattern.compile(Pattern.java:823)
at java.util.regex.Pattern.matches(Pattern.java:928)
at java.lang.String.matches(String.java:2090)
Run Code Online (Sandbox Code Playgroud) 我有一个接口,我希望枚举实现它,但有些方法是相同的,它会导致代码重复.我会使用抽象类而不是接口,但java不允许枚举扩展任何东西.有没有解决的办法?我包含一些代码来帮助理解问题.
public interface CommandI {
boolean isCommand(String command);
void execute(Drawer drawer, Creature creature);
String getDescription();
}
public enum Command2 implements CommandI {
FORWARD{
private String description = "qwersadd";
private String[] aliases = {"fd", "forward"};
@Override
public boolean isCommand(String command) {
for (String s: aliases){
if (s.equals(command)){
return true;
}
}
return false;
}
@Override
public void execute(Drawer drawer, Creature creature) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public String getDescription() {
return description;
}
},
PENUP{
private String description …Run Code Online (Sandbox Code Playgroud) 鉴于以下文件锁定请求:
FileLock lock = null;
try {
lock = randomAccessFile.getChannel().lock(0, Long.MAX_VALUE, mode.shared);
// work with file
} finally {
if (lock != null) {
lock.release();
}
}
Run Code Online (Sandbox Code Playgroud)
目标操作系统是 MS Windows,是否有可能finally永远不会执行该块,从而永远不会释放锁?例如,如果 JVM 崩溃了怎么办?如何处理这种无所有者锁定?
我的所有控制器都扩展了以下抽象类:
public abstract class AbstractController {
public HttpServletRequest request;
public HttpServletResponse response;
public ModelMap model;
}
Run Code Online (Sandbox Code Playgroud)
而且,我实现了以下拦截器:
public class HttpRequestInterceptor implements HandlerInterceptor {
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws ServletException {
if (handler instanceof AbstractController) {
AbstractController controller = (AbstractController) handler;
controller.request = request;
controller.response = response;
controller.model = new ModelMap();
}
return true;
}
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
if (handler instanceof AbstractController && modelAndView != null) {
AbstractController controller …Run Code Online (Sandbox Code Playgroud) 当使用switch语句时,有可用项目的详尽列表(例如a enum),并且如果每个项目都有自己的条件代码,我应该使用default标签吗?例如:
public class MyClass {
public enum Type {
TYPE1, TYPE2
}
private Type type;
public void withDefault() {
switch (type) {
case TYPE1:
// some conditional code for TYPE1
break;
default:
// some conditional code for TYPE2
break;
}
}
public void withoutDefault() {
switch (type) {
case TYPE1:
// some conditional code for TYPE1
break;
case TYPE2:
// some conditional code for TYPE2
break;
}
}
}
Run Code Online (Sandbox Code Playgroud)
在那种情况下,我应该使用什么:withDefault()方法还是方法withoutDefault()?或者也许只是品味问题?
我正在编写一个必须通过命令行运行的java服务器.我想传递给服务器不同的端口号来听.但是,我不确定如何在运行服务器时将端口号作为命令行参数传递给ServerSocket类.
public class Server {
public static void main(String[] args) {
Server server = new Server();
}
public Server() {
ServerSocket socket = new ServerSocket(5000);
// I want to specify the port number above as an argument when running the server.
}
}
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激!
java ×10
jquery ×2
regex ×2
spring-mvc ×2
enums ×1
extends ×1
field ×1
generics ×1
httpresponse ×1
inheritance ×1
interceptor ×1
ioexception ×1
javascript ×1
jvm-crash ×1
keydown ×1
locking ×1
oop ×1
quantifiers ×1
reflection ×1
release ×1
servlets ×1
triggers ×1
which ×1