您的位置:首页 > 其它

封装,static,String类

2017-04-06 20:51 246 查看
一:

1.1

封装:(案例演示,创建学生类年龄可以随意被设置成不符合要求的参数)

是指隐藏对象的属性和实现细节,仅对外提供公共访问方式。

好处:
A:提高了代码的复用性
B:提高安全性。

体现:
将不需要对外提供的内容都隐藏起来。
做法:
把属性隐藏,提供公共方法对其访问。

现在我们可以给age赋值,但是赋予负数值居然也通过了。这是不满足实际需求的。
我们应该对不满足需求的数据给与判断。
而判断不能直接针对成员变量做,因为要判断,就必须写在方法中。
所以,我们应该用一个方法给年龄赋值并判断。

虽然我们已经通过一个方法做出了数据不正常的判断,但是我们还是可以通过对象直接访问成员变量。
那么,为了让使用者使用方法来给成员变量赋值,就不能允许它们直接去访问成员变量。
怎么办呢?
为了解决这种情况,java就提供了一个修饰符关键字:private

private:
私有的意思。

可以修饰成员变量和成员方法。

特点:
被private修饰的内容,只能在本类中访问。

1.2
封装加入private后的标准代码:
A:把成员变量private修饰
B:针对每一个成员变量给出
getXxx()和setXxx()(注意首字母大写)
注意:这里的xxx其实是成员变量名称。


二:this关键字详解(演示修改局部变量的变量名和成员变量相同的话的效果)

常说:

见名知意。

局部变量如果有一个变量和成员变量名称一致,那么优先使用的是局部变量。
就近原则。

这样的话,就造成了局部变量隐藏了成员变量。
如何解决呢?
为了解决这种情况,java就提供了一个代表本类对象的关键字:this


三:构造方法

3.1

构造方法:

作用:给对象的数据进行初始化

格式特点:

A:方法名和类名相同。

public void Student() {}

B:没有返回值类型。

修饰符 返回值类型 方法名(…) {…}

C:没有返回值。

没有用return带明确的值回来。

return;

3.2
构造方法注意事项
A:如果你不提供构造方法,系统会给出默认无参构造方法
B:如果你提供了构造方法,系统将不再提供默认无参构造方法
这个时候怎么办呢?
a:使用自己给的带参构造。
b:要么自己再提供一个无参构造

建议:建议永远自己给出无参构造方法。
C:构造方法也是可以重载的。

3.3
给成员变量赋值:
A:通过setXxx()方法
B:通过带参构造方法


四:

成员方法:

去掉static的方法。

根据返回值类型:
void类型
非void类型

根据参数列表:
无参
带参


五:

标准代码的写法:

练习:

手机类:

成员变量:

brand,price,color

构造方法:

无参,带参

成员方法:

getXxx()/setXxx()

show(),call()

练习:

学生类:

成员变量:

name,age,address

构造方法:无参,带参

成员方法:getXxx()/setXxx()
show(),study()


六:

6.1 测试标准手机代码(两种创建对象方式,分别进行演示) package com.edu_04;

/**

* 五:

标准代码的写法:

练习:

手机类:

成员变量:

brand,price,color

构造方法:

无参,带参

成员方法:

getXxx()/setXxx()

show(),call()

一个标准的java描述类:
1.私有化成员变量
2.为私有化的成员变量提供set/get方法
3.提供有参数和无参数的构造方法
4.还需要写一个功能性的方法


*

*/

public class Phone {

//提供私有化的成员变量

private String brand;

private int price;

private String color;

//为私有化的成员变量提供set/get方法
public void setBrand(String brand){
this.brand = brand;
}
public String getBrand(){
return brand;
}

public void setPrice(int price){
this.price = price;
}
public int getPrice(){
return price;
}

public void setColor(String color){
this.color = color;
}
public String getColor(){
return color;
}

//提供有参数和无参数的构造方法
public Phone(){}

public Phone(String brand,int price,String color){
this.brand = brand;
this.price = price;
this.color = color;
}

//提供功能性的方法
public void call(){
System.out.println("手机可以打电话");
}
public void show(){
//show方法打印三个成员变量
System.out.println(brand+"  "+price+"  "+color);
}


}

package com.edu_04;

public class Test {

public static void main(String[] args) {
10c38

//创建一个手机对象,使用set方法给对象赋值(无参构造+set方法)

Phone p = new Phone();

p.setBrand(“华为”);

p.setColor(“黑色”);

p.setPrice(2000);

System.out.println(p.getBrand()+” “+p.getColor()+” “+p.getPrice());

System.out.println("-----------------------------");
//使用有参构造创建对象并且给对象赋值
Phone p2 = new Phone("苹果", 5000, "土豪金");
System.out.println(p2.getBrand()+"  "+p2.getColor()+"  "+p2.getPrice());

System.out.println("-----------------------------");
p2.call();
p2. show();

}


}

七:

面向对象练习:

7.1 定义一个类MyMath,提供基本的加减乘除功能,然后进行测试。

add(int a,int b)

sub(int a,int b)

mul(int a,int b)

div(int a,int b)

package com.edu_06;

public class MyMath {

//加法
public int add(int a,int b){
return a+b;
}

//减法
public int sub(int a,int b){
return a-b;
}

//乘法
public int mul(int a,int b){
return a*b;
}

//除法
public int div(int a,int b){
return a/b;
}


}

package com.edu_06;

public class MyMathTest {

public static void main(String[] args) {

//创建MyMath对象

MyMath my = new MyMath();

//调用这个对象的加减乘除的功能
System.out.println(my.add(10, 20));
System.out.println(my.sub(20, 10));
System.out.println(my.mul(10, 20));
System.out.println(my.div(20, 10));

}


}

7.2

定义一个长方形类,定义求周长和面积的方法,然后定义一个测试了Test2,进行测试。

周长:2*(长+宽)

面积:长*宽

变量的定义:

范围越小越好。

但是,如果这个变量和类有描述关系,就应该定义为成员变量。

package com.edu_07;

/**

* 定义一个长方形类,定义求周长和面积的方法,然后定义一个测试了Test2,进行测试。

周长:2*(长+宽)

面积:长*宽

*/

public class Rectangle {

//提供一个求周长的方法
public int zhouChang(int width,int height){
return 2*(width+height);
}

//求面积的方法
public int getArear(int width,int height){
return width*height;
}


}

package com.edu_07;

public class Test {

public static void main(String[] args) {

//创建对象

Rectangle r = new Rectangle();

int zhouChang = r.zhouChang(10, 20);

System.out.println(zhouChang);

int arear = r.getArear(10, 20);

System.out.println(arear);

}

}

八:

static关键字

8.1

为了体现共用的数据,java就提供了一个关键字:static。

static:

作用:可以修饰成员变量和成员方法

特点:
A:随着类的加载而加载
B:优先于对象存在
C:被类的所有对象共享
也是我们判断该不该使用静态修饰一个数据的依据。
举例:
饮水机:static
水杯:特有的内容。
D:可以通过类名调用
静态变量:类变量
非静态变量:实例变量,对象变量

非静态的:创建对象访问
静态的:可以通过类名,也可以通过对象访问。


8.2(写一个静态成员变量和静态成员方法进行演示)

static关键字注意事项

A:在静态方法中是没有this关键字的

原因:静态的内容随着类的加载而加载,this是随着对象的创建而存在,所以,static中不能有this。

B:静态方法只能访问静态的成员变量和静态的成员方法

静态方法只能访问静态的成员。

九:

API帮助文档使用简单讲解+java中的常用的一些包的讲解

十:

String类讲解 char[] chs = {‘a’,’b’,’c’};

(1) 是由多个字符组成的一串数据。(字符序列)

其实字符串可以看成是一个字符数组。

(2)构造方法:
public String():无参构造方法
public String(byte[] bytes):把字节数组转换为字符串
public String(char[] value):把字符数组转换为字符串
public String(char[] value,int offset,int count):把字符数组的一部分转换为字符串
public String(String original):把一个字符串转换为字符串

(需要利用到的一个成员方法)成员方法:
public int length():返回此字符串的长度

(3)String的特点及面试题
A:
String类的数据特点:
字符串是常量;它们的值在创建之后不能更改
面试题:根据以上结论请问输出的s的值是多少
String s = "hello";
s += "world";
System.out.println(s);

结论:
字符串的内容不能发生改变,但是字符串引用的指向是可以改变的。

B:String s = new String("hello")和String s = "hello"的区别(画图讲解)
前者创建了1个或者2个对象
后缀创建了0个或者1个对象
C:面试题(看程序写结果)
A:new和直接赋值的区别
String s1 = new String("hello");
String s2 = new String("hello");
System.out.println(s1==s2);
System.out.println(s1.equals(s2));

String s3 = new String("hello");
String s4 = "hello";
System.out.println(s3==s4);
System.out.println(s3.equals(s4));

String s5 = "hello";
String s6 = "hello";
System.out.println(s5==s6);
System.out.println(s5.equals(s6));

D:面试题:
String s1 = "hello";
String s2 = "world";
String s3 = "helloworld";
System.out.println(s3 == s1 + s2);
System.out.println(s3.equals(s1 + s2));

System.out.println(s3 == "hello" + "world");
System.out.println(s3.equals("hello" + "world"));

结论:
看程序写结果
变量相加,先开空间,在加。
常量相加,先加,找是否有这样的数据空间,如果没有才开空间。

(4)String类的成员方法
A:判断功能
boolean equals(Object obj):比较两个字符串的内容是否相同,严格区分大小写。(用户名,密码)
boolean equalsIgnoreCase(String str):比较两个字符串的内容是否相同,忽略大小写。(验证码)
boolean contains(String str):判断字符串中是否包含一个子串。
boolean startsWith(String str):判断是否以指定的字符串开头
boolean endsWith(String str):判断是否以指定的字符串结尾
boolean isEmpty():判断字符串的内容是否为空
问题:内容为空和对象为空是一个意思吗?
答:不是

B:获取功能
String类的获取功能:
int length():返回字符串的长度。其实就是字符的个数。
char charAt(int index):返回字符串中指定索引处的字符。
int indexOf(int ch):返回指定的字符在字符串中第一次出现的索引。
明明说的是字符,为什么是int呢?
原因是int类型还可以接收char类型。
97,'a'是一样的效果。
但是如果参数是char类型,你就不能写97了。
int indexOf(String str):返回指定的字符串在字符串中第一次出现的索引。
String substring(int start):截取从start开始到末尾的字符串。
String substring(int start,int end):截取从start开始到end结束的字符串。

C:转换功能
byte[] getBytes():把字符串转换为字节数组
char[] toCharArray():把字符串转换为字符数组
static String valueOf(char[] chs):把字符数组转换为字符串
static String valueOf(int i):把int类型的数据转换为字符串
valueOf():可以把任意类型的数据转换为字符串。
String toLowerCase():把字符串转成小写
String toUpperCase():把字符串转成大写
String concat(String str):拼接字符串,前面我们使用过+进行字符串的拼接,不够专业。

D:其他功能
A:替换功能
String replace(char old,char new)
String replace(String old,String new)
B:去除字符串两端空格
String trim()
package com.edu_10;


/**

* (2)构造方法:

public String():无参构造方法

public String(byte[] bytes):把字节数组转换为字符串

public String(char[] value):把字符数组转换为字符串

public String(char[] value,int offset,int count):把字符数组的一部分转换为字符串

public String(String original):把一个字符串转换为字符串

*

*/

public class StringDemo {

public static void main(String[] args) {

/*//public String():无参构造方法

String s = new String();//创建一个一个空字符串:””

s = “hello”;//引用的指向本质上已经发生了变化

System.out.println(s);*/

System.out.println("-------------------");
/*//public String(byte[] bytes):把字节数组转换为字符串
String s = "hello";
byte[] bys = s.getBytes();
String s2 = new String(bys);
System.out.println(s2);*/

System.out.println("--------------------");
//public String(char[] value):把字符数组转换为字符串
/*char[] chs = {'a','b','c','d'};
String s = new String(chs);
System.out.println(s);*/

System.out.println("--------------------");
//public String(char[] value,int offset,int count):把字符数组的一部分转换为字符串
/*char[] chs = {'a','b','c','d'};
String s = new String(chs, 2,2);//offect:开始的索引  count:从开始索引开始向后获取多少个字符
System.out.println(s);*/

System.out.println("--------------------");
//public String(String original):把一个字符串转换为字符串
String s = new String("hello");
System.out.println(s);

/**
*  (需要利用到的一个成员方法)成员方法:
public int length():返回此字符串的长度
*/

System.out.println(s.length());

}


}

重点二

package com.edu_10;

/**

* A:

String类的数据特点:

字符串是常量;它们的值在创建之后不能更改

面试题:根据以上结论请问输出的s的值是多少

String s = “hello”;

s += “world”;

System.out.println(s);

注意:字符串被创建之后是不能被改变的,但是他的引用的指向可以发生改变


*/

public class StringDemo2 {

public static void main(String[] args) {

String s = “hello”;

s += “world”;

System.out.println(s);

}

}

重点三

package com.edu_10;

public class StringDemo4 {

public static void main(String[] args) {

String s1 = “hello”;

String s2 = “world”;

String s3 = “helloworld”;

System.out.println(s3 == s1 + s2); //fasle

System.out.println(s3.equals(s1 + s2)); //true

System.out.println(s3 == "hello" + "world"); //true
System.out.println(s3.equals("hello" + "world")); //true
/**
* 变量相加和常量相加的区别?
* 变量相加:先在内存中开辟空间,再去做加法
* 常量相加:先加,找是否有这样的数据空间,如果没有才开空间。
*/
}


}

重点四

package com.edu_10;

/**

A:判断功能

boolean equals(Object obj):比较两个字符串的内容是否相同,严格区分大小写。(用户名,密码)

boolean equalsIgnoreCase(String str):比较两个字符串的内容是否相同,忽略大小写。(验证码)

boolean contains(String str):判断字符串中是否包含一个子串。

boolean startsWith(String str):判断是否以指定的字符串开头

boolean endsWith(String str):判断是否以指定的字符串结尾

boolean isEmpty():判断字符串的内容是否为空

问题:内容为空和对象为空是一个意思吗?

答:不是

*

*/

public class StringDemo5 {

public static void main(String[] args) {

// boolean equals(Object obj):比较两个字符串的内容是否相同,严格区分大小写。(用户名,密码)

String s = “hello”;

String s2 = “Hello”;

System.out.println(s.equals(s2));

// boolean equalsIgnoreCase(String str):比较两个字符串的内容是否相同,忽略大小写。(验证码)

System.out.println(s.equalsIgnoreCase(s2));

// boolean contains(String str):判断字符串中是否包含一个子串。
System.out.println(s.contains("hel"));

// boolean startsWith(String str):判断是否以指定的字符串开头
System.out.println(s.startsWith("h"));

// boolean endsWith(String str):判断是否以指定的字符串结尾
System.out.println(s.endsWith("lo"));

//  boolean isEmpty():判断字符串的内容是否为空
//String s3 = null;
String s3 = "";
/**
* java.lang.NullPointerException:空指针异常
*
* 对象为空:null,说明这个引用没有指向
* 字符串为空:就是一个空字符串,字符串中什么也没有
*/
System.out.println(s3.isEmpty());

}


}

重点五

package com.edu_10;

/**

B:获取功能

String类的获取功能:

int length():返回字符串的长度。其实就是字符的个数。

char charAt(int index):返回字符串中指定索引处的字符。

int indexOf(int ch):返回指定的字符在字符串中第一次出现的索引。

明明说的是字符,为什么是int呢?

原因是int类型还可以接收char类型。

97,’a’是一样的效果。

但是如果参数是char类型,你就不能写97了。

int indexOf(String str):返回指定的字符串在字符串中第一次出现的索引。

String substring(int start):截取从start开始到末尾的字符串。

String substring(int start,int end):截取从start开始到end结束的字符串。

*

*/

public class StringDemo6 {

public static void main(String[] args) {

// int length():返回字符串的长度。其实就是字符的个数。

String s = “hello”;

System.out.println(s.length());

// char charAt(int index):返回字符串中指定索引处的字符。
System.out.println(s.charAt(0));

//int indexOf(int ch):返回指定的字符在字符串中第一次出现的索引。
System.out.println(s.indexOf('l'));

//String substring(int start):截取从start开始到末尾的字符串。
String s2 = s.substring(2);
System.out.println(s2);

//String substring(int start,int end):截取从start开始到end结束的字符串。
System.out.println(s.substring(0, 3));
//这里的开始和结束的索引值,全部都是包前不包后
}


}

重点六

package com.edu_10;

/**

C:转换功能

byte[] getBytes():把字符串转换为字节数组

char[] toCharArray():把字符串转换为字符数组

static String valueOf(char[] chs):把字符数组转换为字符串

static String valueOf(int i):把int类型的数据转换为字符串

valueOf():可以把任意类型的数据转换为字符串。

String toLowerCase():把字符串转成小写

String toUpperCase():把字符串转成大写

String concat(String str):拼接字符串,前面我们使用过+进行字符串的拼接,不够专业。

*

D:其他功能

A:替换功能

String replace(char old,char new)

String replace(String old,String new)

B:去除字符串两端空格

String trim()

*

*/

public class StringDemo7 {

public static void main(String[] args) {

// byte[] getBytes():把字符串转换为字节数组

String s = “java”;

byte[] bys = s.getBytes();

//char[] toCharArray():把字符串转换为字符数组


// char[] chs = s.toCharArray();

// for (int i = 0; i < chs.length; i++) {

// System.out.println(chs[i]);

// }

//static String valueOf(char[] chs):把字符数组转换为字符串
char[] chs = {'a','v','c','d'};
String s3 = String.valueOf(chs);
System.out.println(s3);

String s4 = String.valueOf(12.34);//"12.34"
System.out.println(s4);

//String toLowerCase():把字符串转成小写
String s5 = "HELLoWoErl";
System.out.println(s5.toLowerCase());

//String toUpperCase():把字符串转成大写
System.out.println(s5.toUpperCase());

// String concat(String str):拼接字符串,前面我们使用过+进行字符串的拼接,不够专业。
String s6 = "hello";
String s7 = "world";
System.out.println(s6.concat(s7));

//String replace(char old,char new)
System.out.println(s6.replace('e', 'a'));

//String replace(String old,String new)
System.out.println(s6.replace("hel", "hal"));

//String trim()
String s8 = "   fdv  gdf  gds    ";
System.out.println(s8);
System.out.println(s8.trim());

}


}

重点⑦

package com.edu_10;

public class StringDemo8 {

public static void main(String[] args) {

//做一个练习

//”HElloWorld”

String s = “HElloWorld” ;

String s2 = s.substring(0, 5);

String s3 = s.substring(5);

//转换大小写带拼接
String s4 = s2.toUpperCase().concat(s3.toLowerCase());
System.out.println(s4);
}


}

十一:

StringBuffer和StringBuilder

11.1

* StringBuffer:

* 线程安全的可变字符序列。

*

* String和StringBuffer的区别?

* A:String的内容不可变

* B:StringBuffer的内容可变

*

* StringBuffer和StringBuilder的区别?

* A:StringBuffer 线程安全,效率低

* B:StringBuilder 线程不安全,效率高

*

* 线程安全:(同步),效率低

* 线程不安全:(不同步),效率高

*

* 构造方法:

* StringBuffer():构造一个其中不带字符的字符串缓冲区,其初始容量为 16 个字符。

* StringBuffer(int capacity):构造一个其中不带字符的字符串缓冲区,其初始容量为 capacity个字符。

* StringBuffer(String str):构造一个其中带字符的字符串缓冲区,其初始容量为??? 个字符。

*

* 成员方法:

* public int length():返回长度(字符数)。实际值

* public int capacity():返回当前容量。 理论值

* 添加功能:添加元素,并返回本身的对象。

* A:public StringBuffer append(String str):追加数据,在末尾添加

* B:public StringBuffer insert(int offset,String str):插入数据,在指定位置添加

* 删除功能:

* public StringBuffer deleteCharAt(int index):删除指定索引处的字符

* public StringBuffer delete(int start,int end):删除从start开始到end结束的数据,包左不包右

* 替换功能:

* public StringBuffer replace(int start,int end,String str):用str替换从start到end的数据

*

* 反转功能:

* public StringBuffer reverse()

* 截取功能:返回值类型是String类型,本身没有发生改变

* public String substring(int start)

* public String substring(int start,int end)

* 相互转换:

*

* String – StringBuffer

* String s = “hello”;

// 方式1

StringBuffer sb1 = new StringBuffer(s);

// 方式2

StringBuffer sb2 = new StringBuffer();

sb2.append(s);

*

* StringBuffer – String

* StringBuffer sb = new StringBuffer(“world”);

//方式1

String s1 = sb.substring(0);

//方式2

String s2 = sb.toString();

//方式3

String s3 = new String(sb);

package com.edu_11;


/**

* 构造方法:

* StringBuffer():构造一个其中不带字符的字符串缓冲区,其初始容量为 16 个字符。

* StringBuffer(int capacity):构造一个其中不带字符的字符串缓冲区,其初始容量为 capacity个字符。

* StringBuffer(String str):构造一个其中带字符的字符串缓冲区,其初始容量为??? 个字符。

*

* 成员方法:

* public int length():返回长度(字符数)。实际值

* public int capacity():返回当前容量。 理论值

*

*/

public class StringBufferDemo {

public static void main(String[] args) {

//StringBuffer():构造一个其中不带字符的字符串缓冲区,其初始容量为 16 个字符。

StringBuffer sb = new StringBuffer();

System.out.println(sb.length());//实际容量

System.out.println(sb.capacity());//理论容量

System.out.println("-----------------------");
//StringBuffer(int capacity):构造一个其中不带字符的字符串缓冲区,其初始容量为 capacity个字符。
StringBuffer sb2 = new StringBuffer(10);
System.out.println(sb2.length());
System.out.println(sb2.capacity());

System.out.println("---------------------");
//StringBuffer(String str):构造一个其中带字符的字符串缓冲区,其初始容量为??? 个字符。
StringBuffer sb3 = new StringBuffer("hello");
System.out.println(sb3);
}


}

二
package com.edu_11;


/**

* 添加功能:添加元素,并返回本身的对象。

* A:public StringBuffer append(String str):追加数据,在末尾添加

* B:public StringBuffer insert(int offset,String str):插入数据,在指定位置添加

*

*/

public class StringBufferDemo2 {

public static void main(String[] args) {

//A:public StringBuffer append(String str):追加数据,在末尾添加

StringBuffer sb =new StringBuffer();

// sb.append(“hello”);

// sb.append(100);

// sb.append(true);

// sb.append(‘a’);

// System.out.println(sb);

sb.append(true).append("hello").append("java").append(100);
System.out.println(sb);

//B:public StringBuffer insert(int offset,String str):插入数据,在指定位置添加
//sb.insert(0, "false");
sb.insert(4, "fasle");
System.out.println(sb);

}


}

三
package com.edu_11;


/**

* 删除功能:

* public StringBuffer deleteCharAt(int index):删除指定索引处的字符

* public StringBuffer delete(int start,int end):删除从start开始到end结束的数据,包左不包右

*/

public class StringBufferDemo3 {

public static void main(String[] args) {

//public StringBuffer deleteCharAt(int index):删除指定索引处的字符

StringBuffer sb =new StringBuffer(“hello”);

//sb.deleteCharAt(1);

//System.out.println(sb);

//public StringBuffer delete(int start,int end):
//删除从start开始到end结束的数据,包左不包右
sb.delete(1, 3);
System.out.println(sb);

}


}



package com.edu_11;

/**

* 替换功能:

* public StringBuffer replace(int start,int end,String str):用str替换从start到end的数据

*

* 反转功能:

* public StringBuffer reverse()

* 截取功能:返回值类型是String类型,本身没有发生改变

* public String substring(int start)

* public String substring(int start,int end)

*

*/

public class StringBufferDemo4 {

public static void main(String[] args) {

//替换功能:public StringBuffer replace(int start,int end,String str):

//用str替换从start到end的数据

/* StringBuffer sb = new StringBuffer(“hello”);

sb.replace(1, 3, “ab”);

System.out.println(sb);*/

System.out.println("-----------------------");
//反转功能:public StringBuffer reverse()
/*StringBuffer sb = new StringBuffer("hello");
sb.reverse();
System.out.println(sb);*/

System.out.println("-------------------");
//public String substring(int start)
StringBuffer sb = new StringBuffer("hello");


/* String s = sb.substring(2);

System.out.println(s);*/

//public String substring(int start,int end)
String s = sb.substring(1, 4);
System.out.println(s);
}


}



package com.edu_11;

/**

* String – StringBuffer

* String s = “hello”;

// 方式1

StringBuffer sb1 = new StringBuffer(s);

// 方式2

StringBuffer sb2 = new StringBuffer();

sb2.append(s);

*

*/

public class StringBufferDemo5 {

public static void main(String[] args) {

//String – StringBuffer

String s = “hello”;

// 方式1

StringBuffer sb1 = new StringBuffer(s);

// 方式2

StringBuffer sb2 = new StringBuffer();

sb2.append(s);

System.out.println("------------------------");
//StringBuffer -->>String的转换
//方式1:
//substring(0)截取方法

//方式2:String(StringBuffer buffer)

//方式3:调用StringBuffer中的toString()方法

}


}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐