3 回答

TA貢獻(xiàn)1780條經(jīng)驗(yàn) 獲得超4個(gè)贊
我試過為該屬性使用吸氣劑,但得到了相同的結(jié)果。
你確定嗎?以下應(yīng)該正是您所追求的:
class SuperClass {
private String a = "Super";
public void superMethod() {
System.out.println("SuperMethod: " + getA());
}
public String getA() {
return this.a;
}
}
class ChildClass extends SuperClass {
private String a = "Child";
@Override
public String getA() {
return this.a;
}
}
public class Main {
public static void main(String[] args) {
SuperClass s = new SuperClass();
ChildClass c = new ChildClass();
s.superMethod();
c.superMethod();
}
}
請注意,getter 不能是私有的(否則無法從類外部訪問它們),并且它們不能是靜態(tài)的(否則它們是類的一部分,而不是該類的任何實(shí)例。)

TA貢獻(xiàn)1871條經(jīng)驗(yàn) 獲得超8個(gè)贊
目前尚不清楚你在做什么,但你的String a成員是private static班級的成員,而不是個(gè)別對象的成員。
如果您創(chuàng)建了String a對象的成員,而不是類的成員,則可以在創(chuàng)建子類期間覆蓋該值:
U:\>jshell
| Welcome to JShell -- Version 12
| For an introduction type: /help intro
jshell> class SuperClass {
...> protected final String a;
...>
...> protected SuperClass(String _a) {
...> a = _a;
...> }
...>
...> public SuperClass() {
...> this("Super");
...> }
...>
...> public void superMethod() {
...> System.out.println("SuperMethod: "+a);
...> }
...> }
| created class SuperClass
jshell> class ChildClass extends SuperClass {
...> public ChildClass() {
...> super("Child");
...> }
...> }
| created class ChildClass
jshell> var s = new SuperClass();
s ==> SuperClass@4566e5bd
jshell> var c = new ChildClass();
c ==> ChildClass@ff5b51f
jshell> s.superMethod();
SuperMethod: Super
jshell> c.superMethod();
SuperMethod: Child
更新
現(xiàn)在我們知道了您的實(shí)際用例(來自下面的評論),您想要實(shí)現(xiàn)的內(nèi)容非常簡單:
class SuperClass {
private final static Logger LOG = Logger.getLogger(SuperClass.class);
protected Logger getLogger() { return LOG; }
public void superMethod(){
getLogger().info("superMethod() called.");
}
}
class ChildClass extends SuperClass {
private final static Logger LOG = Logger.getLogger(ChildClass.class);
@Override
protected Logger getLogger() { return LOG; }
}
public class Main {
public static void main(String[] args) {
SuperClass s = new SuperClass();
ChildClass c = new ChildClass();
s.superMethod(); // Message logged to SuperClass.LOG
c.superMethod(); // Message logged to ChildClass.LOG
}
}

TA貢獻(xiàn)1847條經(jīng)驗(yàn) 獲得超11個(gè)贊
簡短回答:Java 無法按照您想要的方式執(zhí)行此操作,因?yàn)榫幾g器會將 String 文字與最終值合并,因此"SuperMethod: " + a將在生成的字節(jié)碼中進(jìn)行轉(zhuǎn)換"SuperMethod: Super"。
唯一的解決方案是使用反射(如果必須的話):
import java.lang.reflect.Field;
public class Main {
public static void main(String[] args) {
SuperClass s = new SuperClass();
ChildClass c = new ChildClass();
s.superMethod();
c.superMethod();
}
}
class SuperClass {
private static final String a = "Super";
public void superMethod(){
try{
final Class<?> clazz = this.getClass();
final Field fieldA = clazz.getDeclaredField("a");
fieldA.setAccessible(true);
final String value = (String)fieldA.get(null);
System.out.println("SuperMethod: " + value);
} catch (final NoSuchFieldException | IllegalAccessException ex){
// Because reflection
ex.printStackTrace();
}
}
}
class ChildClass extends SuperClass {
private static final String a = "Child";
}
輸出是:
SuperMethod: Super
SuperMethod: Child
但是,老實(shí)說,我仍然喜歡使用經(jīng)典覆蓋:
public class Main {
public static void main(String[] args) {
SuperClass s = new SuperClass();
ChildClass c = new ChildClass();
s.superMethod();
c.superMethod();
}
}
class SuperClass {
private static final String a = "Super";
public void superMethod(){
System.out.println("SuperMethod: " + getA());
}
public String getA() {
return a;
}
}
class ChildClass extends SuperClass {
private static final String a = "Child";
@Override
public String getA() {
return a;
}
}
添加回答
舉報(bào)