本人java菜鸟,刚学习的,有段代码没搞懂,请高手解答一下,请帮忙
package basic.day01;public class Example {
String str = new String("good");

char[] ch = { 'a', 'b', 'c' }; public static void main(String args[]) {


Example ex = new Example();

ex.change(ex.str, ex.ch);
System.out.print(ex.str + " and ");
System.out.print(ex.ch);
}

解决方案 »

  1.   

    [Quote=引用楼主 ai380102002 的回复:]
    本人java菜鸟,刚学习的,有段代码没搞懂,请高手解答一下,请帮忙
    package basic.day01;public class Example {
    String str = new String("good");

    char[] ch = { 'a', 'b', 'c' }; public static void main(String args[]) {


    Example ex = new Example();

    ex.change(ex.str, ex.ch);
    System.out.print(ex.str + " and ");
    System.out.print(ex.ch);
    } public void change(String str, char ch[]) {
    str = "test ok";
    ch[0] = 'g';
    }}
      

  2.   

    以前没弄过帖子,这回对了,为什么输出的事 good and gbc  不是 test ok
     and abc呢?
      

  3.   

    public void change(String str, char ch[]) {
     this.str = "test ok";
     this.ch[0] = 'g';
    }
      

  4.   

    为什么我不用this它输出的是good呢?
    ex.change(ex.str, ex.ch);
    当执行完这个语句后,ex.str为什么还是good?
    它存的不是text ok的地址吗?
      

  5.   

    你给函数参数赋值,基本类型的化什么也变不了.
    this.str和str是两个东西.没见过很多constructor都是写的 this.xxx=xxx么.
      

  6.   

    要对对象的属性修改,方法不要传递参数。把change 方法参数去掉。
    public void change() {
    str = "test ok";
    ch[0] = 'g';
    }
    着时str 就是对象的str.调用时也改为:
    ex.change();