Example Description Different in Java overwriting static methods and non-static methods

zhaozj2021-02-16  51

This article will explain the difference in static methods and non-static methods in Java by presentation. And in the article, a specific explanation is given.

First we offer two classes, base classes are Parent, derived as Child. In Parent, we provide two ways, one is static method staticMethod (), one is a non-static method nonstaticMethod (). In the Child class, we cover two ways. class Parent {public void nonStaticMethod () {System.out.println ( "Parent's Non-Static Method is Called");} public static void staticMethod () {System.out.println ( "parent's static method is called");} }

class Child extends Parent {public void nonStaticMethod () {System.out.println ( "child's non-static method is called");} public static void staticMethod () {System.out.println ( "child's static method is called") }} In the Test class, we use Parent P1 = New Parent (), Parent P2 = New Child (), Child C = New Child () to get three instances, and call static methods and non-static methods, we come Look at the program of the program PUBLIC CLASS TEST {public static void main (string args []) {PARENT P1 = new parent (); parent p2 = new child (); child c = new child (); system.out.print "Parent.Static:"); Parent.StaticMethod (); System.out.print ("p1.static:"); p1.staticMethod (); system.out.print ("p2.static:"); p2. StaticMethod (); system.out.print ("p1.nonstatic:"); p1.nonStaticMethod (); system.out.print ("p2.nonstatic:"); p2.nonStaticMethod (); system.out.print "Child.Static:"); child.staticMethod (); system.out.print ("c.static:"); c.staticMethod (); system.out.print ("c.nons Tatic: "); c.nonStaticMethod ();

转载请注明原文地址:https://www.9cbs.com/read-21005.html

New Post(0)