After this blog, the advanced object-oriented operations are performed. What is advanced? Only on the basis of expansion, so the basic knowledge of the firm can be further improved. In order to complete the questioning task, the rest of the week to review, so that the new knowledge.
Before starting the topic inner class, let's briefly introduce the basic concepts of code blocks.
1. code blockSome of the code in Java can be used to enclose the code directly in {}, mainly including several:
1. Common code blocks: written directly in the method.
Explanation: The above code int is instantiated, first assigning a local variable to x, and then entering the global variable after the base code block.
2. Building blocks: Blocks of code defined in the class. Building blocks take precedence over construction method execution , and after each object is instantiated, construction blocks and construction methods are repeated.
3. Static code block. With a block defined by static, he takes precedence over the construction block execution, and regardless of how many instanced objects he executes only once.
Static blocks defined within the main class take precedence over the main method execution. \
The above procedure provides a message that can be output without using the main () method, and is implemented through blocks of code.
2.Inner class
Use case:
1. Earliest use in graphical interface
2. Open Source Framework
2.1Concept
Concept: Define other operation classes within a class
Format:
Class External Classes
{
Property method
class Inner Classes
{
Property method
}
}
Internal class test Class Outer{private string name1 = "Hello"; class inner{private string name2 = "World";p ublic Void print () {SYSTEM.O Ut.println (name1); <span style= "White-space:pre" ></span>//Call external class properties <span style= "White-space:pre" > </span>
Pros: Private operations within and outside the class can be accessed by each other
Cons: Destroying the structure of a class
Before the operation of the inner class was generated by an external class, the internal class was provided by the method provided by the external class to provide an instantiation of the inner classes, in order to make the internal class of the method call, can you produce an instance of the inner class externally?
Outer$inner.class occurred after compiling the program
$ in the program can be understood as ".", that is, the name of the inner class is: "External class . Inner class ".
Because the access to the inner class requires manipulating the resources and properties of the external class, you want to instantiate the object of the inner class externally, in the format:
The external class . Inner class inner class object name = new External Class (). New Inner class ()
The inner class of the test class outer{private String name1 = "Hello"; class inner{//inner class method public void print () {System.out.println ( Outer.this.name1);}}} public class Test {public static void main (string[] args) {//external class. Inner class Inner class object name = New External Class (). New inner Class () Outer.Inner
2.2 Staticdeclaring an inner class
You can declare an inner class using static, but you cannot declare an external class
For example, static a{} error hint: Error : modifier Static is not allowed here
If an inner class becomes an outer class with the static declaration
Declaration format:
The external class . Inner class inner class object name = new External Class . Inner class ()
The inner class of the test class Outer{private static String name1 = "Hello";//inner class becomes static only access static resource static class inner{//method public void print () {System.out.println (name1);}}} public class Test {public static void main (string[] args) {//static Inner class then he becomes an external class Outer.Inner
2.3defining an inner class in a method
In theory, inner classes can be defined anywhere, mostly in methods. Define an inner class in a method that can directly access the properties of an external class
Note: When you define an inner class in a method, the inner class method is also immediately instantiated. In the lower version of the JDK, the inner class requires a final declaration if the parameters and variables of the method are to be called, but the high version does not require
To this end today, I wish you a healthy health, happy.
12-11java Object-oriented partial operation