Java-encapsulation and java Encapsulation
Note: by default, java automatically imports classes in the same package.
Two classes with the same name as Telephone are defined as follows:
Package com. test; public class Telephone {private double screen; public double getScreen () {return screen;} public void setScreen (double newScreen) {screen = newScreen;} public Telephone () {System. out. println ("com. test. telephone construction method without Parameters ");}}
package com.test.second;public class Telephone { public Telephone() { System.out.println("com.test.secont.Telephone"); }}
Demo. java
package com.test;import com.test.Telephone;public class Demo { public static void main(String[] args) { Telephone phone = new Telephone(); }}
Running result:
Com. test. Telephone construction method without Parameters
package com.test;import com.test.second.Telephone;public class Demo { public static void main(String[] args) { Telephone phone = new Telephone(); }}
Running result:
Com. test. secont. Telephone
| Access Modifier |
This class |
Same package |
Subclass |
Others |
| Private |
√ |
|
|
|
| Default |
√ |
√ |
|
|
| Protected |
√ |
√ |
√ |
|
| Public |
√ |
√ |
√ |
√ |
This indicates the current object. this. Attribute operates on the attributes of the current object; this. method calls the method of the current object
An internal class is a class defined in another class. The class that contains the internal class is called an external class.
Role of internal classes:
Note:
The internal class of the method is the method in which the internal class defines the external class. The internal class of the method is only visible inside the method.
Note: because the internal class of the method cannot be used outside the external class, the internal class of the method cannot use the access controller and static modifier.
Package com. test; public class Demo12 {public void show () {final int a = 12; int B = 13; class MInner {int c = 2; public void print () {System. out. println ("variable a =" + a in the external class method); System. out. println ("variable B =" + B in the external class method); System. out. println ("variable c =" + c) ;}} MInner mi = new MInner (); mi. print ();} public static void main (String [] args) {Demo12 demo = new Demo12 (); demo. show ();}}
Running result:
Variable a = 12 in the external Class Method
Variable B = 13 in the external Class Method
Variable c = 2 in the internal class