public abstract class Mother {
}
public class Daughter extends Mother {
}
public class Son extends Mother {
}
Run Code Online (Sandbox Code Playgroud)
我需要Map哪些键中的一个Daughter或Son类,并且其值是这两个类中的一个,的对象列表分别.
例如:
/* 1. */ map.put(Daughter.class, new ArrayList<Daughter>()); // should compile
/* 2. */ map.put(Son.class, new ArrayList<Son>()); // should compile
/* 3. */ map.put(Daughter.class, new ArrayList<Son>()); // should not compile
/* 4. */ map.put(Son.class, new ArrayList<Daughter>()); // should not compile
Run Code Online (Sandbox Code Playgroud)
我试过了Map<Class<T extends Mother>, List<T>>,但它没有编译.
Map<Class<? extends Mother>, List<? extends Mother>> …
我理解这个问题听起来很奇怪,但我想知道为什么java.io.Serializable界面已经被精确地作为一个接口实现,而不是作为一个类?
是什么让我想到这一点,是我们正在谈论覆盖的readObject/ writeObject方法,而根据定义,我们并不覆盖他们(即没有超类型我们对Serializable对象已经实现了这些方法).
因此,如果Serializable本来是一个类,它可以实现默认 readObject/writeObject方法,然后执行类可以真正覆盖所述方法.
这是一个不起作用的解决方法,说明了我的话:
public class Serializable implements java.io.Serializable {
private static final long serialVersionUID = 356223041512972356L;
protected void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
stream.defaultReadObject();
}
protected void writeObject(ObjectOutputStream stream) throws IOException {
stream.defaultWriteObject();
}
}
public class MyClass extends Serializable {
private static final long serialVersionUID = -5437634103734137046L;
@Override
protected void readObject(ObjectInputStream stream) throws IOException, …Run Code Online (Sandbox Code Playgroud) (如何)可以简化以下正则表达式:
ab|a|b
Run Code Online (Sandbox Code Playgroud)
?
我正在寻找一个不那么冗余的,即只有一个a和一个b.可能吗?
一些尝试:
a?b? # matches empty string while shouldn't
ab?|b # still two b
Run Code Online (Sandbox Code Playgroud)
另外,实正则表达式具有更复杂a和b部分,即不是单个字符,但内subregexes让我们说.
我正在尝试为Spring MVC实现一个通用的REST控制器:
public abstract class GenericRestController<T extends GenericEntity> {
protected final GenericService<T> service;
public GenericRestController(GenericService<T> service) {
this.service = service;
}
@RequestMapping(method = RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)
public List<T> list() {
return service.list();
}
@RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.CREATED)
public T create(@RequestBody T entity) {
return service.create(entity);
}
// other basic REST methods such as get(), delete(), etc.
}
Run Code Online (Sandbox Code Playgroud)
然后,例如ArticleRestController:
@RestController
@RequestMapping(value = "/rest/articles", produces = MediaType.APPLICATION_JSON_VALUE)
public class ArticleRestController extends GenericRestController<Article> {
@Autowired
public ArticleRestController(ArticleService articleService) { …Run Code Online (Sandbox Code Playgroud) 我正在尝试嵌入Instagram个人资料:
$scope.instagram = "<blockquote class=\"instagram-media\" data-instgrm-version=\"4\" style=\" background:#FFF; border:0; border-radius:3px; box-shadow:0 0 1px 0 rgba(0,0,0,0.5),0 1px 10px 0 rgba(0,0,0,0.15); margin: 1px; max-width:658px; padding:0; width:99.375%; width:-webkit-calc(100% - 2px); width:calc(100% - 2px);\"><div style=\"padding:8px;\"> <div style=\" background:#F8F8F8; line-height:0; margin-top:40px; padding:50% 0; text-align:center; width:100%;\"> <div style=\" background:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACwAAAAsCAMAAAApWqozAAAAGFBMVEUiIiI9PT0eHh4gIB4hIBkcHBwcHBwcHBydr+JQAAAACHRSTlMABA4YHyQsM5jtaMwAAADfSURBVDjL7ZVBEgMhCAQBAf//42xcNbpAqakcM0ftUmFAAIBE81IqBJdS3lS6zs3bIpB9WED3YYXFPmHRfT8sgyrCP1x8uEUxLMzNWElFOYCV6mHWWwMzdPEKHlhLw7NWJqkHc4uIZphavDzA2JPzUDsBZziNae2S6owH8xPmX8G7zzgKEOPUoYHvGz1TBCxMkd3kwNVbU0gKHkx+iZILf77IofhrY1nYFnB/lQPb79drWOyJVa/DAvg9B/rLB4cC+Nqgdz/TvBbBnr6GBReqn/nRmDgaQEej7WhonozjF+Y2I/fZou/qAAAAAElFTkSuQmCC); display:block; height:44px; margin:0 auto -44px; position:relative; top:-22px; width:44px;\"></div></div><p style=\" color:#c9c8cd; font-family:Arial,sans-serif; font-size:14px; line-height:17px; margin-bottom:0; margin-top:8px; overflow:hidden; padding:8px 0 7px; text-align:center; text-overflow:ellipsis; white-space:nowrap;\"><a href=\"https://instagram.com/p/1yaMWyoVZM/\" style=\" color:#c9c8cd; font-family:Arial,sans-serif; font-size:14px; font-style:normal; font-weight:normal; line-height:17px; text-decoration:none;\" target=\"_top\">Une photo publiée par National Geographic …Run Code Online (Sandbox Code Playgroud) 在 Spring Security 的 BCrypt 实现中,我遇到了以下方法(源码):
static boolean equalsNoEarlyReturn(String a, String b) {
char[] caa = a.toCharArray();
char[] cab = b.toCharArray();
if (caa.length != cab.length) {
return false;
}
byte ret = 0;
for (int i = 0; i < caa.length; i++) {
ret |= caa[i] ^ cab[i];
}
return ret == 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么采用这种不提前归还的 equals方法?它有什么好处:
static boolean equals(String a, String b) {
char[] caa = a.toCharArray();
char[] cab = b.toCharArray();
if (caa.length != cab.length) { …Run Code Online (Sandbox Code Playgroud) 这是我尝试用户的jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Add Users using ajax</title>
<script src="/Spring3MVC/js/jquery.js"></script>
<script type="text/javascript">
function doAjaxPost() {
// get the form values
var firstName = $('#firstName').val();
var lastName = $('#lastName').val();
$.ajax({
type: "POST",
url: "/insert",
data: "firstName=" + firstName + "&lastName=" + lastName,
success: function(response) {
// we have the response
$('#info').html(response);
$('#firstName').val('');
$('#lastName').val('');
},
error: function(e) {
alert('Error: ' + …Run Code Online (Sandbox Code Playgroud) 我试图在div上设置一个从红色到透明的线性渐变,但输出有点奇怪:小提琴.
小提琴的CSS:
background: -webkit-linear-gradient(transparent, red);
background: -moz-linear-gradient(transparent, red);
background: -ms-linear-gradient(transparent, red);
background: -o-linear-gradient(transparent, red);
background: linear-gradient(transparent, red);
Run Code Online (Sandbox Code Playgroud)
如您所见,两种颜色之间的过渡是灰色的,而我只期望红色渐变.
有没有人知道如何改善这个输出(没有在小提琴中用"黄色"替换"透明")?
PS:在Chrome 23.0.1271.64中尝试过
在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类的相同重载方法?
java ×6
generics ×2
inheritance ×2
spring ×2
spring-mvc ×2
ajax ×1
angularjs ×1
bcrypt ×1
class ×1
css ×1
equals ×1
etag ×1
html ×1
http ×1
instagram ×1
interface ×1
javascript ×1
jquery ×1
jsp ×1
map ×1
ng-bind-html ×1
oop ×1
overriding ×1
payload ×1
put ×1
redundancy ×1
regex ×1
rest ×1
rfc ×1
simplify ×1
transparency ×1