18 new关键字

package com.wys.javaoop;

public class CatTest {
    public static void main(String[] args) {
        Cat one = new Cat();
        Cat two = one;  //此时 two 和 one 都指向同一个实例化的对象

        one.name = "mcgonagall";
        one.month = 2;
        one.weight = 1000;
        one.species = "英国短毛猫";

        two.name = "cat2";
        two.month = 3;
        two.weight = 3000;
        two.species = "美国短毛猫";

        System.out.println("名字:" + one.name);
        System.out.println("年龄:" + one.month);
        System.out.println("体重:" + one.weight);
        System.out.println("品种:" + one.species);
        System.out.println("-----------------------------");
        System.out.println("名字:" + two.name);
        System.out.println("年龄:" + two.month);
        System.out.println("体重:" + two.weight);
        System.out.println("品种:" + two.species);
    }
}

结果:

猜你喜欢

转载自www.cnblogs.com/CPU-Easy/p/12145201.html