public 就是谁都可以用 private 就是除了自己,谁都不能用,亲儿子也不行 pretected 除了自己,儿子,孙子...还可以用 String 是不能修改的字符串。如果你改了,那时因为生成了一个新的字符串对象 StringBuffer 是一个可以修改的字符串。
谢谢大家了,我可能说的不太清楚:遇到像下面的问题就迷糊了,如下: 1.public StringBufferDemo() { public static void main(String[] args) { StringBuffer a = new StringBuffer("A"); StringBuffer b = new StringBuffer("B"); operate(a, b);
System.out.println(a + "."+b); } static void operate(StringBuffer x, StringBuffer y) { x.append(y); y = x; } —————————— 2. public class cat {
protected static String colour="random"; public cat() {
} public void showcatcolour() { System.out.println("cat:"+colour); } public static void showcolour() { System.out.println("cat:"+colour); } } public class whitecat extends cat {
protected static String colour="white"; public whitecat() { super(); } public void showcatcolour() { System.out.println("white:"+colour); } public static void showcolour() { System.out.println("whitecat:"+colour); }
}public class main { /** * @param args */ public static void main(String[] args) { whitecat w=new whitecat(); cat c=w; w.showcatcolour(); w.showcolour(); c.showcolour(); c.showcatcolour();
在 StringBuffer 上的基本操作是 append 和 insert 方法,它们都被覆盖以接受任意类型的数据。 每个方法都迅速地把所给的数据转换成一个字符串,并且把该字符串的字符添加或插入 到字符串缓冲区中。 append 方法常常把这些字符添加到缓冲区的末端; 而 insert 方法则把这些字符添加到指定的地方
private 就是除了自己,谁都不能用,亲儿子也不行
pretected 除了自己,儿子,孙子...还可以用
String 是不能修改的字符串。如果你改了,那时因为生成了一个新的字符串对象
StringBuffer 是一个可以修改的字符串。
1.public StringBufferDemo() {
public static void main(String[] args) {
StringBuffer a = new StringBuffer("A");
StringBuffer b = new StringBuffer("B"); operate(a, b);
System.out.println(a + "."+b); } static void operate(StringBuffer x, StringBuffer y) {
x.append(y);
y = x;
}
——————————
2. public class cat {
protected static String colour="random";
public cat()
{
}
public void showcatcolour()
{
System.out.println("cat:"+colour);
}
public static void showcolour()
{
System.out.println("cat:"+colour);
}
}
public class whitecat extends cat {
protected static String colour="white";
public whitecat()
{
super();
}
public void showcatcolour()
{
System.out.println("white:"+colour);
}
public static void showcolour()
{
System.out.println("whitecat:"+colour);
}
}public class main { /**
* @param args
*/
public static void main(String[] args) {
whitecat w=new whitecat();
cat c=w;
w.showcatcolour();
w.showcolour();
c.showcolour();
c.showcatcolour();
System.out.println("***************************");
//cat c2=new cat();
//whitecat w2=(whitecat)c2;
//c2.showcolour();
//c2.showcatcolour();
System.out.println("**************************");
cat c3=new whitecat();
whitecat w3=(whitecat)c3;
c3.showcolour();
c3.showcatcolour();
}应该怎么学才能真正了解啊
StringBuffer a = new StringBuffer("A");
StringBuffer b = new StringBuffer("B");
operate(a, b);
System.out.println(a + "." + b);
}// 重点在这里
// x,y 是2个局部变量
// x.append 会修改x指向的对象,所以此时那个对象的内容变成了 AB
// y=x, 只是把局部变量x的引用指向了x,并不会影响main里面的b的引用
// 所以调用完毕,main里面的a和这里的x 都指向了 AB,
// main里面的 b 继续指向了B,这里的y已经改成指向了 AB
// OVER
static void operate(StringBuffer x, StringBuffer y) {
x.append(y); //
y = x;
}
希望你区分对象和对象的引用的区别。
对象引用类似[一张纸上]
对象类似 房子一张纸上写着房子的地址
当然,可以有多张纸指向了同一个房子,比如那个x,y
修改某纸上的地址,并不会影响其它纸上的内容。
其余的我不解释了,请自己理解吧!