首页 > 解决方案 > 通过引用访问私有字符串

问题描述

我需要在私有字符串中访问消息,尽管可以通过使用引用轻松完成,但是它会迫使我插入一条新消息,该消息将替换向后的消息。

class Test {
    private String s = "This is string s";
    String methodS(String a) {
        a = s;
        return s;
    }
}

class name {
    public static void main(String args[]) {
        Test elen = new Test();
        System.out.println(elen.methodS("This is string s in another class."));     
    }
}

标签: java

解决方案


您可以在类中编写一个 getter 方法来获取私有字段。

String getS(String a) {
   return s;
}

推荐阅读