可能重复:
为什么enum的构造函数不能访问静态字段?
enum Test {
e1,e2;
int i=0;
static int j=5;
Test(){
System.out.println(i+" "+j);
}
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,构造函数可以访问实例变量但不能访问静态变量J.
我已经阅读了与其他作者有关的答案,所有人都说在初始化J(静态字段)之前初始化了e1和e2,但是根据java规范,所有的静态字段都是在类加载到内存时初始化的,也就是在运行之前构造函数.因此,在运行Test()构造函数之前,必须初始化静态变量j.我无法理解限制,任何机构都可以让我理解.我已经阅读了问题的答案为什么enum的构造函数不能访问静态字段?但我对以下答案感到不满: - 在静态字段全部初始化之前调用构造函数.
假设是用另一个像enum这样的简单类的例子
class Test{
public static final Test t=new Test();
static int a=5;
Test(){
System.out.println(a);
}
public static void main(String[] args) {
}
}
Run Code Online (Sandbox Code Playgroud)
根据那里的参数,构造函数将在静态字段的初始化之前运行,并且它也在运行时因为它的打印0(因为JVM进行了初始化).但没有编译错误或没有运行时问题.那么为什么枚举不会发生同样的事情.
如果您定义如下所示的界面
interface I1{
}
Run Code Online (Sandbox Code Playgroud)
您可以在任何代码部分中编写
I1 i1;
i1.equals(null);
Run Code Online (Sandbox Code Playgroud)
然后从equals方法到来的地方,接口是否也扩展了超类Object ?,如果那样,接口可以如何扩展类?
假设让接口扩展超类Object,那么如果你看到为什么像Set thave这样的集合接口定义了equals()和hashCode()方法?所有类都扩展了Object类,因此如果在Object类中定义的接口中定义任何抽象方法,那么实现接口的人就不需要实现这些方法.如下面的代码
interface I1{
String toString();
}
class A implements I1{
}
Run Code Online (Sandbox Code Playgroud)
这里的类A不需要实现方法toString(),因为它存在于Object类中.那么在集合接口中定义那些方法的目的是什么,因为它们不能强制实现类来实现那些方法.
以下是我的代码
class NumberComparator<Number> implements Comparator<Number> {
public int compare(Number o1, Number o2) {
return 1;
}
}
public class Ex28 {
public static void main(String[] args) {
TreeSet set = new TreeSet(new NumberComparator<Number>());
set.add(1);
set.add(1.4f);
set.add(1L);
set.add("1a");
System.out.println(set);
}
}
Run Code Online (Sandbox Code Playgroud)
因为我已经定义了我自己的Number类型的比较器,但是当我添加任何其他字符串的东西时,它并没有给我任何异常.它只是工作正常.我得到了输出
[1, 1.4, 1, 1a]
Run Code Online (Sandbox Code Playgroud)
任何人都可以解释它为什么会发生.
大家好我使用Spring简单的JDBC模板来调用oracle程序,下面是我的代码.
程序,流程
create or replace
PROCEDURE get_all_system_users(
pi_client_code IN VARCHAR2,
po_system_users OUT T_SYSTEM_USER_TAB,
po_error_code OUT NUMBER,
po_error_description OUT VARCHAR2)
IS
ctr NUMBER;
sysUser SYSTEM_USER_OBJ;
BEGIN
ctr:=0;
po_system_users:= t_system_user_tab();
end
Run Code Online (Sandbox Code Playgroud)
春道课
public class ManualSaleStoredProcedureDao {
private SimpleJdbcCall getAllSytemUsers;
public List<SystemUser> getAllSytemUsers(String clientCode) {
MapSqlParameterSource in = new MapSqlParameterSource();
in.addValue("pi_client_code", clientCode);
in.addValue("po_system_users", null,
OracleTypes.ARRAY, "T_SYSTEM_USER_TAB");
Map<String, Object> result = getAllSytemUsers.execute(in);
return null;
}
public void setDataSource(DataSource dataSource) {
getAllSytemUsers = new SimpleJdbcCall(dataSource)
.withSchemaName("SChemaName")
.withProcedureName("get_all_system_users")
.declareParameters(
new SqlParameter(
"pi_client_code",
OracleTypes.VARCHAR,
"pi_client_code"));
}
Run Code Online (Sandbox Code Playgroud)
当我打电话给 …
我有一个登录页面,用户需要在其中输入以下信息VIN号码,电子邮件,邮政编码和accessCode,他们将从不同的应用程序中获取.
因此,要验证用户,我需要自定义UserDetailsService类中的所有信息,然后将调用一个过程来验证用户.
但是当我实现UserDetailsService下面这样的时候,我看到了
@Component
public class LoginService implements UserDetailsService {
@Autowired
LoginStoredProcedureDao loginStoredProcedureDao;
public Map<String, Object> verifyLogin(LoginDetails details) {
return loginStoredProcedureDao.verifyLogin(details);
}
@Override
public UserDetails loadUserByUsername(String username)
throws UsernameNotFoundException {
// TODO Auto-generated method stub
//verifyLogin();
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
loginDetails对象如下所示
public class LoginDetails {
String vin;
String email;
String zipcode;
String accessCode;
}
Run Code Online (Sandbox Code Playgroud)
在上述情况下如何使用弹簧安全.在这里,用户需要提供所有信息以验证他自己.
我正在将多个文件上传到Amazon S3.通过使用以下代码.
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
MultiValueMap < String,
MultipartFile > map = multipartRequest.getMultiFileMap();
try {
if (map != null) {
for (String filename: map.keySet()) {
List < MultipartFile > fileList = map.get(filename);
incrPercentge = 100 / fileList.size();
request.getSession().setAttribute("incrPercentge", incrPercentge);
for (MultipartFile mpf: fileList) {
/*
* custom input stream wrap to original input stream to get
* the progress
*/
ProgressInputStream inputStream = new ProgressInputStream("test", mpf.getInputStream(), mpf.getBytes().length);
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentType(mpf.getContentType());
String key = Util.getLoginUserName() + …Run Code Online (Sandbox Code Playgroud) 我有以下弹簧配置:
<context:property-placeholder location="classpath:commonSql.properties" />
Run Code Online (Sandbox Code Playgroud)
现在在我的课堂上,当我使用@value("#{someproperty}")它时,它不起作用.然后,我改变了
@value("${someproperty}")它并且它起作用了.
根据这个问题的答案@value("#{someproperty}") 是SpEL语法,它的功能和复杂程度要高得多.它还可以处理属性占位符,还有更多,但在我的情况下,为什么它不起作用?虽然简单的是如何使用$和#来评估价值.
最重要的是@value("#{someproperty}")在工作时@value("${someproperty}")不工作.
if(someCondition)
int a=10;//Compilation Error
else if(SomeOtherCondition){
int b=10;//no compilation Error
}
Run Code Online (Sandbox Code Playgroud)
为什么会发生这种情况.为什么在第一种情况下出现编译错误.如果我放括号,那么没有编译错误,但if语句括号是可选的,如果它是一个语句.
我正在将 war 文件部署到 JBoss 作为 7.1 ,相同的 war 文件与 tomcat 一起正常运行,但使用 jboss 时,它给出了流动异常。
16:20:50,906 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-1) MSC00001: Failed to start service jboss.module.service."deployment.VSCAS.war".main: org.jboss.msc.service.StartException in service jboss.module.service."deployment.VSCAS.war".main: Failed to load module: deployment.VSCAS.war:main
at org.jboss.as.server.moduleservice.ModuleLoadService.start(ModuleLoadService.java:91) [jboss-as-server-7.1.1.Final.jar:7.1.1.Final]
at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811) [jboss-msc-1.0.2.GA.jar:1.0.2.GA]
at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746) [jboss-msc-1.0.2.GA.jar:1.0.2.GA]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) [rt.jar:1.7.0]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603) [rt.jar:1.7.0]
at java.lang.Thread.run(Thread.java:722) [rt.jar:1.7.0]
Caused by: org.jboss.modules.ModuleNotFoundException: Module com.oracle:main is not found in local module loader @40dd550c (roots: E:\server\jboss-as-7.1.1.vsc\jboss-as-7.1.1.Final\modules)
at org.jboss.modules.LocalModuleLoader.findModule(LocalModuleLoader.java:126)
at org.jboss.modules.ModuleLoader.loadModuleLocal(ModuleLoader.java:275)
at org.jboss.modules.ModuleLoader.preloadModule(ModuleLoader.java:222)
at org.jboss.modules.LocalModuleLoader.preloadModule(LocalModuleLoader.java:94)
at org.jboss.modules.Module.addPaths(Module.java:841)
at org.jboss.modules.Module.link(Module.java:1181) …Run Code Online (Sandbox Code Playgroud) 我的微服务将创建一些我需要访问的文件,这些文件主要是存档或错误数据。
我从这里找到了这篇文章 Microsoft azure app service by default mount across the container 持久共享存储
来自微软文档:
“您可以使用
/home应用程序文件系统中的目录在重新启动时保留文件并在实例之间共享它们。应用/home程序中的 是为了使容器应用程序能够访问持久存储而提供的。”
现在我想访问该/home目录以检查该服务创建的文件。
我尝试了很多方法,例如使用部署凭据使用 FTPS,但我无法看到该/home目录
如何访问挂载到 docker 容器的持久共享存储?
我无法为应用程序服务配置单独的存储,因为它处于预览模式,并且根据本文档不支持生产场景:
https://learn.microsoft.com/en-us/azure/app-service/containers/how-to-serve-content-from-azure-storage
我有以下代码
public class Test {
public static void main(String[] args) {
Integer i1=null;
String s1=null;
String s2=String.valueOf(i1);
System.out.println(s1==null+" "+s2==null);//Compilation Error
System.out.println(s1==null+" ");//No compilation Error
System.out.println(s2==null+" ");//No compilation error
}
}
Run Code Online (Sandbox Code Playgroud)
如果将两个布尔值与String组合,为什么会出现编译错误
编辑:编译错误是操作符==未定义参数类型boolean,null
I have a binary string like "11100011" and I want to convert it into a byte. I have a working example in Java like below:
byte b1 = (byte)Integer.parseInt("11100011", 2);
System.out.println(b1);
Run Code Online (Sandbox Code Playgroud)
Here the output will be -29. But if I write some similar code in JavaScript like below:
parseInt('11100011', 2);
Run Code Online (Sandbox Code Playgroud)
I get an output of 227.
What JavaScript code I should write to get the same output as Java?
我想结合两个通用列表并希望生成新列表,我以简单的格式提供我的代码.
public class Example{
public static <E> List<E> union(List<? extends E> a,List<? extends E> b){
List<Object> es= new ArrayList<Object>();
for( E e:a){
es.add(e);
}
for( E e:b){
es.add(e);
}
return (List<E>) es;
}
public static void main(String[] args) {
List<Long> a=new ArrayList<Long>();
a.add(1L);
List<Integer> b=new ArrayList<Integer>();
b.add(2);
List<Number> list3=union(a, b);//Compilation Error due to mismatch of return type
List<String> a1=new ArrayList<String>();
a1.add("H");
List<String> a2=new ArrayList<String>();
a1.add("=E");
List<String> a3=union(a1, a2);//no compilation error
}
}
Run Code Online (Sandbox Code Playgroud)
我的要求是我应该能够组合两个整数和长列表来生成数字列表,我也应该能够组合其他类型.这里的问题是关于我尝试组合整数和长列表时的返回类型.我需要做些什么改变才能使我的代码工作.
java ×11
spring ×3
collections ×2
spring-mvc ×2
amazon-s3 ×1
azure ×1
boolean ×1
enums ×1
file-upload ×1
generics ×1
if-statement ×1
javascript ×1
jboss7.x ×1
jdbctemplate ×1
oracle ×1
oracle11g ×1
progress-bar ×1
spring-el ×1
string ×1