Now there are two classes with the same name but different packages.
The structure is shown in:
The code for two class A is as follows:
Package Ka; <br/> Public Class A {<br/> Public void printa () {<br/> system. out. println ("Print: Ka. A "); <br/>}< br/>}
Package PKB; <br/> Public Class A {<br/> Public void printa () {<br/> system. out. println ("Print: PKB. A "); <br/>}< br/>}
In addition, Main. Java is a test class:
Case 1:
Main
The main code is as follows:
Package Ka; <br/> public class main {<br/> Public static void main (string [] ARGs) {<br/> A = new (); <br/>. printa (); <br/>}< br/>}
Test results:
Case 2:
After we add the import to PKB. A, the situation is different.
The main code is as follows:
Package Ka; <br/> Import PKB. a; <br/> public class main {<br/> Public static void main (string [] ARGs) {</P> <p> A = new (); <br/>. printa (); <br/>}< br/>}
Test results:
In this case, a A = new A () is used to create not A in this package, but a in the PKB imported in.
In this case, you must use the following method to create the object A in the ka:
Package Ka; <br/> Import PKB. a; <br/> public class main {<br/> Public static void main (string [] ARGs) {<br/> ka. a A = New KA. A (); <br/>. printa (); // The result is print: Ka. A <br/>}< br/>}