java프로그래밍 사상005 별칭 현상

package wzs.test2;

//             ,                   
//    ,    tank1=tank2,  tank1 tank2       tank2       
//        ,  tank1.level=tank2.level
public class Test_wzs2
{
    public static void main(String[] args)
    {
        Tank tank1 = new Tank();
        Tank tank2 = new Tank();

        tank1.level = 1;
        tank2.level = 2;
        System.out.println("tank1.level=" + tank1.level + ", tank2.level=" + tank2.level);

        tank1 = tank2;
        System.out.println("tank1.level=" + tank1.level + ", tank2.level=" + tank2.level);

        tank1.level = 5;
        System.out.println("tank1.level=" + tank1.level + ", tank2.level=" + tank2.level);

        tank2.level = 7;
        System.out.println("tank1.level=" + tank1.level + ", tank2.level=" + tank2.level);
        // ----------------------------
        Tank tank3 = new Tank();
        Tank tank4 = new Tank();
        tank3.level = 1;
        tank4.level = 2;
        tank3.level = tank4.level;
        System.out.println("
tank3.level=" + tank3.level + ", tank4.level=" + tank4.level); tank3.level = 7; tank4.level = 8; System.out.println("tank3.level=" + tank3.level + ", tank4.level=" + tank4.level); } } class Tank { int level; }

결과 출력:
tank1.level=1, tank2.level=2
tank1.level=2, tank2.level=2
tank1.level=5, tank2.level=5
tank1.level=7, tank2.level=7

tank3.level=2, tank4.level=2
tank3.level=7, tank4.level=8

좋은 웹페이지 즐겨찾기