StringBuffer的insert()方法和append()方法

//StringBuffer的insert()方法和append()方法
class
aa
{
public static void main (String[]
args)
{
StringBuffer str = new
StringBuffer("wlf");
System.out.println(str); //调用insert方法前结果:wlf
str.insert(1,"ang");
System.out.println(str); //调用insert方法后结果:wanglf
System.out.println("---------------------------");
Student
stu = new Student(); //生成自定义类的对象
System.out.println(stu); //我们已经知道,打印对象会自动调用其toString()方法 haha0
//用StringBuffer的append方法添加object,实质为:将object的toString返回值(即String信息)加入StringBuffer尾部
str.append(stu);
System.out.println(str); //wanglfhaha0
}
}
//自定义类
class Student
{
int age; //int型属性默认值为0
public String
toString()
{
return "haha"+age;
}
}

原文地址:https://www.cnblogs.com/lingyi1111/p/4421280.html