java - 通过引用访问私有字符串
问题描述
我需要在私有字符串中访问消息,尽管可以通过使用引用轻松完成,但是它会迫使我插入一条新消息,该消息将替换向后的消息。
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."));
}
}
解决方案
您可以在类中编写一个 getter 方法来获取私有字段。
String getS(String a) {
return s;
}
推荐阅读
- python - Python(如果有的话)如何学习列表中的哪个元素
- html - 样式表中未知媒体查询的后备包括
- delphi - 替换存储文本表单(收据)中的文本集宽度、对齐方式、小数
- ruby-on-rails - 在 ROR 中,如果发生任何错误,如何关闭打开的数据库连接?
- mysql - 如何在 SQL 中的事件中嵌入聚合函数
- javascript - 在javascript中制作一个魔方
- nrules - 为什么 IContext.RetractLinked 需要两个参数?
- awk - 使用 AWK,如何将十进制数转换为十六进制
- android - 在 Jetpack Compose 中的 API 响应后更新 LazyColumn
- amazon-web-services - 云平台和普通网站的真正区别是什么?