Protected modified static properties and methods call Way 1: Direct class name invocation
2: Instantiation of the parent class object for invocation
3: Instantiating a subclass object for invocation
Protected modified non-static properties and Methods call method: 1: Instantiate subclass object for invocation
Parent Class Code
1 PackageCom.baidu.tool;2 3 ImportCom.baidu.test.SonDiffPack;4 5 Public classparentdiffpack{6 7 protectedString name;8 protected Static intAge ;9 Ten protected voidrun () { OneSystem.out.println ("Parent class can run ... is not a static method "); A } - protected Static voideat () { -System.out.println ("Parent class can eat ....") static Method "); the } -}
Sub-class Code
1 Packagecom.baidu.test;2 3 ImportCom.baidu.tool.ParentDiffPack;4 5 Public classSondiffpackextendsParentdiffpack {6 7 Public Static voidMain (string[] args) {8 //the invocation of static properties and methods in different package parents can either instantiate the parent class object or call it directly with the class name9Parentdiffpack parent =NewParentdiffpack ();TenParent.age=12; One parent.eat (); AParentdiffpack.age=12; - parentdiffpack.eat (); - the //Both static and non-static methods in the parent class of different packages can instantiate a subclass object for invocation. -Sondiffpack son =NewSondiffpack (); -Son.name= "Xioaming"; -Son.age=12; + Son.run (); - son.eat (); + } A}
Properties and method invocation methods for protected adornments in different Java packages