java 重写的 几大注意点
Single Dispatchclass Parent { void print(String a) { log.info("Parent - String"); } void print(Object a) { log.info("Parent - Object"); } } class Child extends Parent { void print(String a) { log.info("Child - String"); } void print(Object a) { log.info("Child - Object"); } }
String string = ""; Object stringObject = string; // What gets printed? Child child = new Child(); child.print(string); child.print(stringObject); Parent parent = new Child(); parent.print(string); parent.print(stringObject);
child.print(string); // Prints: "Child - String" child.print(stringObject); // Prints: "Child - Object" parent.print(string); // Prints: "Child - String" parent.print(stringObject); // Prints: "Child - Object"
Hidden Overrideclass Parent { void print(Object a) { log.info("Parent - Object"); } } class Child extends Parent { void print(String a) { log.info("Child - String"); } }
String string = ""; Parent parent = new Child(); parent.print(string);
parent.print(string); // Prints: "Parent - Object"
Exposed Overrideclass Parent { void print(Object a) { log.info("Parent - Object!"); } void print(String a) { throw new RuntimeException(); } } class Child extends Parent { void print(String a) { log.info("Child - String!"); } }
String string = ""; Parent parent = new Child(); parent.print(string);
parent.print(string); // Prints: "Child - String!"
Ambiguous Parameterclass Foo { void print(Cloneable a) { log.info("I am cloneable!"); } void print(Map a) { log.info("I am Map!"); } }
HashMap cloneableMap = new HashMap(); Cloneable cloneable = cloneableMap; Map map = cloneableMap; // What gets printed? Foo foo = new Foo(); foo.print(map); foo.print(cloneable); foo.print(cloneableMap);
foo.print(map); // Prints: "I am Map!" foo.print(cloneable); // Prints: "I am cloneable!" foo.print(cloneableMap); // Does not compile
Multiple Inheritance – Interfacesinterface Father { default void print() { log.info("I am Father!"); } } interface Mother { default void print() { log.info("I am Mother!"); } } class Child implements Father,Mother {}
new Child().print();
Multiple Inheritance – Class and Interfaceclass ParentClass { void print() { log.info("I am a class!"); } } interface ParentInterface { default void print() { log.info("I am an interface!"); } } class Child extends ParentClass implements ParentInterface {}
new Child().print(); // Prints: "I am a class!"
Transitive Overrideclass Parent { void print() { foo(); } void foo() { log.info("I am Parent!"); } } class Child extends Parent { void foo() { log.info("I am Child!"); } }
new Child().print(); // Prints: "I am Child!"
Private Overrideclass Parent { void print() { foo(); } private void foo() { log.info("I am Parent!"); } } class Child extends Parent { void foo() { log.info("I am Child!"); } }
new Child().print(); // Prints: "I am Parent!"
Static Overridesclass Parent { static void print() { log.info("I am Parent!"); } } class Child extends Parent { static void print() { log.info("I am Child!"); } }
Child child = new Child(); Parent parent = child; parent.print(); // Prints: "I am Parent!" child.print(); // Prints: "I am Child!"
Static Linkingclass Parent { void print() { staticMethod(); instanceMethod(); } static void staticMethod() { log.info("Parent::staticMethod"); } void instanceMethod() { log.info("Parent::instanceMethod"); } } class Child extends Parent { static void staticMethod() { log.info("Child::staticMethod"); } void instanceMethod() { log.info("Child::instanceMethod"); } }
Child child = new Child(); child.print();
Parent::staticMethod
Child::instanceMethod
Wrapping up
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |