小编Sau*_*akh的帖子

派生类如何调用基类的私有方法?

public class PrivateOverride {  

    private void f() {  
        System.out.println("private f()");  
    }
}  

public class Derived extends PrivateOverride {  

    public void f() {                         //this method is never run.
        System.out.println("public f()");     
    }  
}  

public static void main(String[] args) {

    // instantiate Derived and assign it to 
    // object po of type PrivateOverride.
    PrivateOverride po = new Derived();  

    // invoke method f of object po.  It
    // chooses to run the private method of PrivateOveride
    // instead of Derived
    po.f();                         
  }  
}  
Run Code Online (Sandbox Code Playgroud)

所以,这段代码的输出是 …

java oop inheritance

30
推荐指数
2
解决办法
1万
查看次数

如果使用Spring存在,则覆盖外部属性文件中的属性

我在我的一个Spring配置文件中定义了以下属性文件:

<context:property-placeholder location="classpath:project.properties"/> 
Run Code Online (Sandbox Code Playgroud)

现在我想从一些不在类路径中的外部属性文件中覆盖一些属性.

假设我已将项目部署到某处,我需要进行一些动态配置更改.我不想更新容器中的项目代码库(tomcat或任何东西).

1.)所以我需要一种方法,用我在外部属性文件中的最新更新来更新(覆盖)spring的加载属性文件的值.

2.)如果有人也可以共享刷新预加载属性的方法,那将会很棒.

java spring properties

28
推荐指数
1
解决办法
3万
查看次数

是否真的缓存了java.lang.String的哈希码?

String s1 = "String1";      
System.out.println(s1.hashCode()); // return an integer i1 


Field field = String.class.getDeclaredField("value");  
field.setAccessible(true);  
char[] value = (char[])field.get(s1);  
value[0] = 'J';  
value[1] = 'a';  
value[2] = 'v';  
value[3] = 'a';  
value[4] = '1'; 
System.out.println(s1.hashCode()); // return same value of integer i1 
Run Code Online (Sandbox Code Playgroud)

即使我在反射的帮助下更改了字符,也会保留相同的哈希码值.

这里有什么我需要知道的吗?

java string caching immutability

7
推荐指数
1
解决办法
510
查看次数

为什么我们被限制在java中的内部类中声明静态成员变量?

考虑下面的例子为什么在内部类中继承静态变量没有任何限制,我们被限制在内部类中声明静态成员变量?

public class Outer {

    public class Inner {

        public static String notAllowed;
        /* Above line give following compilation error 
         The field notAllowed cannot be declared static in a non-static inner type, unless initialized with a constant expression
         */

    }

}
Run Code Online (Sandbox Code Playgroud)

但是现在如果我的内部类扩展了包含静态变量的其他类,那么这样可以正常工作.考虑以下代码:

public class Outer {

    public class Inner extends InnerBase {
        /* Since it extends InnerBase so we can access Outer.Inner.allowed */ 
        public Inner(){
             Outer.Inner.allowed = null; // Valid statement
        }
    }

}

public class …
Run Code Online (Sandbox Code Playgroud)

java inheritance

6
推荐指数
1
解决办法
1232
查看次数

外部属性文件为Spring MessageSource无法正常工作

考虑以下代码:

<bean id="busmessageSource" 
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basenames">
        <list>
            <value>classpath:bundles/resource</value>
            <value>classpath:bundles/override</value>
            <value>file:/C:/mmt/override</value>
        </list>
    </property>
    <property name="cacheSeconds" value="100" />
</bean>
Run Code Online (Sandbox Code Playgroud)

这里的属性来自bundles/resourcebundles/override我打电话时获取busmessageSource.getMessage("anykey", null, null)

但是当我尝试获取属性的值时失败了 C:/mmt/override

  1. 使用磁盘中的外部文件配置messagesource的正确方法是什么.
  2. 此外,我想file:/C:/mmt/override覆盖值,classpath:bundles/override如果有任何相同的键存在.如何覆盖war文件夹之外的外部文件中的属性?

java spring

3
推荐指数
2
解决办法
1万
查看次数

标签 统计

java ×5

inheritance ×2

spring ×2

caching ×1

immutability ×1

oop ×1

properties ×1

string ×1