Packaging
-
Hides some information about a class internally, and implements operations and access to hidden information through the methods provided by the class. Benefits: 1) data can only be accessed through the prescribed method; 2) Hide the instance details of the class for easy modification and implementation. Package of Practice steps: 650) this.width=650; "Src=" http://s3.51cto.com/wyfs02/M01/4D/1C/wKioL1RLnVjz7gt8AAD2ra7zZ6Y799.jpg " Title= "1.png" width= "height=" 347 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" WIDTH:500PX;HEIGHT:347PX; "alt=" Wkiol1rlnvjz7gt8aad2ra7zz6y799.jpg "/>
Public class telphone{ private float screen; private float cpu; private float mem; public Float getscreen () { return screen;} public void setscreen (Float newscreen) { screen = newscreen;} public telphone () { system.out.println (' non-parametric construction method execution ');}} Public class intialtelphone{ public static void main (String[] args) { telphone phone = new telphone (); telphone phone2 = new telphone (5.0f,1.4f,2.0f); phone2.setscreen (6.0f); system.out.println (' Screen: ' +phone2.getscreen());}}
Packages in Java
-
The role of the package: Managing Java files, resolving conflicting names
-
Definition Package: The package name must be placed on the first line of the program
-
A Java source file contains at most one package statement
Access modifiers in Java
-
Access modifier: Private public protected
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/4D/1B/wKiom1RLnr3D561KAAC0QIKu7kU551.jpg "title=" 2.png " Width= "height=" 154 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:500px;height:154px; "alt=" Wkiom1rlnr3d561kaac0qiku7ku551.jpg "/>
The This keyword in Java
-
The This keyword represents the current object, this. Property Operation current object property, this. method call Current Object method
-
When encapsulating object properties, this is often used
Inner classes in Java
-
Inner class: An inner Class (Inner Class) is a class defined in another class. Corresponding to this, the class containing the inner class is called the outer class.
-
Inner class function: 1. The inner class provides a better encapsulation that hides the inner class within the outer class and does not allow other classes in the same package to access the class;2. The internal class method can directly access all data of the external class, including private data;3. Functions implemented by internal classes can also be implemented using external classes , but sometimes it's more convenient to use internal classes
//external class helloworldpublic class helloworld { // Inner class inner, class inner inside the class outer public class Inner { // methods for internal classes public void show () { system.out.println ("welcome to imooc!"); } } public static void main (String[] args) { // Create an external class object helloworld hello = new helloworld (); // Create an inner class object inner i = hello.new inner (); // method to invoke the Inner class object i.show (); }}
3. Inner class Type: member Inner class, static inner class , method inner class, anonymous inner class
4. member Inner class: Once you have defined a member inner class, you must use an external class object to create an inner class object, and you cannot go directly to the new inner class object, that is, the inner class object name = External class object. New inner class ();
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/4D/1B/wKiom1RLnzniZ3XWAAL8c4ODkqg543.jpg "title=" 3.png " Width= "height=" 307 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" WIDTH:500PX;HEIGHT:307PX; "alt=" Wkiom1rlnzniz3xwaal8c4odkqg543.jpg "/>
Note: An external class cannot use the members and methods of an inner class directly, you can first create an object of the inner class, and then access its member variables and methods through the objects of the inner class.
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/4D/1C/wKioL1RLn8eBRp7CAAG2FSX1LC0679.jpg "title=" 4.png " Width= "height=" border= "0" hspace= "0" vspace= "0" style= "width:500px;height:230px;" alt= " Wkiol1rln8ebrp7caag2fsx1lc0679.jpg "/>
5. Static inner classes are internal classes of static adornments. 1) static inner classes cannot directly access non-static members of an external class, but can be accessed through the new external Class (). 2) If the static member of the outer class is the same as the member name of the inner class, the static member of the external class can be accessed through the class name. Static member If the static member of the outer class is not the same as the member name of the inner class, the static member of the outer class can be called directly through the member name; 3) When you create an object of a static inner class, you do not need an object of the outer class, you can directly create an inner class object name = New Inner Class ()
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/4D/1D/wKioL1RLoHDxbrdsAALYSv08Mbc312.jpg "title=" 5.png " Width= "height=" 336 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:500px;height:336px; "alt=" Wkiol1rlohdxbrdsaalysv08mbc312.jpg "/>
6. method Inner class: The inner class is defined in the method of the outer class, and the inner class of the method is visible only inside the method, that is, it is available only within the method.
External class public class helloworld { private string name = "Love Class"; // Show method in external class public void Show () { // define method inner class class MInner { int Score = 83; public int getscore () { return score + 10; } } // create an object for the method inner class minner mi = new minner (); // method of calling inner class int newscore= mi.getscore (); system.out.println ("Name:" + name + "\ n Bonus score:" + newscore); } // test method inner class public static void Main (String[] args) { // creating objects for external classes helloworld mo = new helloworld (); // methods for calling external classes mo.show (); }}
7. Anonymous inner class: An inner class without a name, typically used only once for this type of inner class. An anonymous inner class must be declared as part of the new statement when it is created, and this will take another special form of the new statement.
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/4D/1C/wKiom1RLoSKwLy6hAAJMEWvJtfY667.jpg "title=" 7.png " Width= "height=" 368 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:500px;height:368px; "alt=" Wkiom1rloskwly6haajmewvjtfy667.jpg "/>
Java Learning Note 2--Package