Java对象赋值是什么意思?
我有以下两个班级:
class Animal { public static void staticMethod(int i) { System.out.println("Animal : static -- " + i); } public void instanceMethod(int i) { System.out.println("Animal : instance -- " + i); } } class Cat extends Animal { public static void staticMethod(int i) { System.out.println("Cat : static -- " + i); } public void instanceMethod(int i) { System.out.println("Cat : instance -- " + i); } public static void main(String[] args) { Cat myCat = new Cat(); myCat.staticMethod(1); // Cat : static -- 1 myCat.instanceMethod(2); // Cat : instance -- 2 System.out.println(""); Animal myAnimal = myCat; Animal.staticMethod(3); // Animal : static -- 3 myAnimal.staticMethod(4); // Animal : static -- 4 [ ? ] System.out.println(""); myAnimal.instanceMethod(5); // Cat : instance -- 5 } } 当我运行Cat时,我得到了以下结果: Cat : static -- 1 Cat : instance -- 2 Animal : static -- 3 Animal : static -- 4 Cat : instance -- 5 我能理解1,2,3和5,但为什么#4不是:“Cat:static – 4”? myAnimal = myCat意味着“myAnimal”现在与“myCat”完全相同,所以任何地方“myAnimal”都会出现,你可以用“myCat”替换它并获得相同的结果,因为myAnimal中的所有内容都与myCat中的所有内容相同,因此“myAnimal.staticMethod(4)”应与“myCat.staticMethod(4)”相同,输出应为:“Cat:static – 4”,类似于上面的“myCat.staticMethod(1)”. 但事实并非如此,为什么呢? 解决方法
从
Oracle docs开始:
class Super { static String greeting() { return "Goodnight"; } String name() { return "Richard"; } } class Sub extends Super { static String greeting() { return "Hello"; } String name() { return "Dick"; } } class Test { public static void main(String[] args) { Super s = new Sub(); System.out.println(s.greeting() + "," + s.name()); } }
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |