62,615
社区成员
发帖
与我相关
我的任务
分享
public static void main(String args[]) {
String a = "a";
String b = "b";
String c = "c";
String d = a + b + c;
System.out.println(d);
}
可以自定义一个类如下:
public class Test {
private String d;
Test(String a, String b, String c) {
d = a + b + c;
}
public static void main(String args[]) {
Test test = new Test("a", "b", "c");
Test test1 = new Test("1", "2", "3");
System.out.println(test.d);
System.out.println(test1.d);
}
}
两者的区别是每次上面需要四步,创建四个对象(默认字符串池无上述字符),而定义类之后调用每次只需要创建一个对象,执行一步,效率大大提高,复用性好