怎么调用另一个类的 string
时间: 2023-05-26 16:06:01 浏览: 53
有几种方法可以调用另一个类的 string:
1. 使用 getter 方法
如果目标类已经定义了一个 getter 方法来获取其字符串属性,您可以通过实例化该类并调用其 getter 方法来获取其字符串值,例如:
```
class TargetClass {
private String stringValue;
public String getStringValue() {
return stringValue;
}
}
// 调用 TargetClass 的字符串值
TargetClass target = new TargetClass();
String value = target.getStringValue();
```
2. 使用静态字符串属性
如果目标类定义了一个静态字符串属性,则您可以直接通过该属性来访问其字符串值,例如:
```
class TargetClass {
public static String STRING_VALUE = "hello world";
}
// 调用 TargetClass 的字符串值
String value = TargetClass.STRING_VALUE;
```
3. 使用静态方法
如果目标类定义了一个静态方法来获取其字符串值,则您可以通过该方法来获得其字符串值,例如:
```
class TargetClass {
public static String getStringValue() {
return "hello world";
}
}
// 调用 TargetClass 的字符串值
String value = TargetClass.getStringValue();
```
无论哪种方法,您都需要确保您可以访问目标类并具有正确的权限。