Package com.study.oop.day01;/** * If a method is static, its behavior is not polymorphic * @author Luhonggang * @date June 5, 2017 * @time pm 4:19:21 * @sinc E 1.0 * Constructors are not polymorphic, they are actually static methods, * except that the static declaration is implicit. Therefore, the constructor cannot be override. */public class Staticdemo {public static void Main (string[] args) {Staticsuper ss = new Staticsub ();Ss. Staticmethod (); The static method in the parent class is called a subclass that cannot both override the static method in the parent classSs. Notstaticmethod ();Ss. Thismethodisnotexistsinsuper (); Program compilation does not pass, the object after initialization can only call methods and properties of the parent class type/*** Styling up in Java: References to parent class types to objects of subclasses* Staticsuper SS = new Staticsub ();*/Staticsuper SS2 = new Staticsuper (); Down stylingStaticsub SS3 = (staticsub) SS;SS2. Thismethodisnotexistsinsuper ();Staticsub SS4 = (staticsub) new Staticsuper ();//compile Pass, run exceptionSs3. Thismethodisnotexistsinsuper (); Run-time exceptionSystem.out.println (Ss3.name);So when you look down, you need to use instanceof.if (SS2 instanceof staticsub) {//downward styling when forced to be judged falseStaticsub ss5 = (staticsub) ss2; System.out.println (Ss5.name);}else{System.out.println ("The object on the left is not an instance of the right class");}if (ss instanceof Staticsub) {//TrueStaticsub ss6 = (staticsub) SS;System.out.println ("The object on the left is an instance of the right class");SS6. Thismethodisnotexistsinsuper ();}}}class staticsuper{public static void Staticmethod () {System.out.println ("I am the static method of the parent class");}public void Notstaticmethod () {System.out.println ("I am a non-static method of the parent class");}}class Staticsub extends staticsuper{String name = "10010";public static void Staticmethod () {System.out.println ("I am a static method of a subclass");}public void Notstaticmethod () {System.out.println ("I am a non-static method of a subclass");}/*** This method does not exist in the parent class and is unique in subclasses only**/public void Thismethodisnotexistsinsuper () {System.out.println ("Just a subclass-unique method, which is an extension of this class");}}
Up and down styling in Java