JavaSE 第十六讲 方法重载剖析 10.4

1.方法重载

public class Test
{
    public void change(Person person, Person person2)
    {
        person.name = "lisi";
        person2.name = "wangwu";
    }
    
    public static void main(String[] args)
    {
        Person person = new Person();
        person.name = "zhangsan";
        
        Person person2 = person;
        
        Test test = new Test();
        
        test.change(person, person2);
        
        System.out.println(person.name);//结果:wangwu
    }

}

class Person
{
    String name;
}

易错!!!

public class People
{
      int age;

      public void change(int i)
      {
          i++;
      }
        
      public void change2(People p)
      {
          p = new People();
          p.age++;
      }

      public void change3(People p, int i)
      {
          p.age++;
          i++;
      }

      public static void main(String[] args)
      {
          /* 
        People people = new People();

        int a = 0;

        people.change(people.age);

        System.out.println(people.age);// 0  原生数据类型值不变
        */

          /*
        People people = new People();

        people.change2(people);

        System.out.println(people.age);//0  指向新引用
          */

      People people = new People();

      people.change3(people, people.age);

      System.out.println(people.age);// 1

  }
}

你可能感兴趣的:(JavaSE 第十六讲 方法重载剖析 10.4)