Java internal Class A summary of the use of Beijing PK10 Platform Rental

Source: Internet
Author: User
Tags class definition static class

Why Beijing PK10 Platform Rental (www.1159880099.com) QQ1159880099 use internal classes:
The most appealing reason for using inner classes is that each inner class can inherit one (interface) implementation independently, so no matter whether the perimeter class has inherited an implementation of (an interface), there is no effect on the inner class.
Inner class: As the name implies, put the class in a certain range
Class can be placed in a package
Class can be placed in a folder
Class can be placed in a class//inner class
Class can be placed in a method//inner class

First, members of the internal class
Class inside classes, classes in class, classes in class
To use an inner class, you need to instantiate an object of an inner class in an external class, and then invoke the method in the inner class through the object dot to manipulate the internal class data through the method.
The inner class can directly use the properties and methods of the outer class, just as the Monkey King enters the iron fan Princess Belly.

Copy Code
1 public class Outerclass {
2///External class Properties
3 private int outer_1=100;//Private attribute
4 public int outer_2=10; Public Property
5
6//External class method outer_method1
7 publicly void Outer_method1 () {
8 System.out.println ("Outer_method1");
9 Innerclass ic=new innerclass ();
10//Call method in inner class
one by one ic.inner_method1 ();
12//Call private and public properties in the inner class
System.out.println (ic.inner_1+ "" +ic.inner_2);
14}

16//Known internal class Innerclass
+ class innerclass{
18//Properties in the inner class
private int inner_1=123;//private Property
20 public int inner_2=321; Public Property
21//Inner class method Inner_method1
Inner_method1 () {
23//Call method in external class,
Outer_method1 ();
25//Call private and public properties in the external class
System.out.println (outer_1+ "" +outer_2);
(+}
}
)
Copy Code
Second, local inner class
method to place inner class
to use an inner class in a method, you must instantiate the inner class's object below the inner class definition in the method, and then call the method of the inner class through the inner class object. The scope of the
inner class is limited to the current method, and the scope disappears when the inner class definition begins to the end of the method. The
local variables in the current method in the outer class can be used directly in the inner class, and the inner classes in the method cannot be used outside the method.

Copy Code
1/*
2
method Place Inner class
3 */
4 public class Outerclass {
5//External class properties
6 private int outer_1=100 ; Private Property
7 public int outer_2=10;//Common attribute
8//method in external class
9 publicly void Outer_method1 () {
System.out.println (" Outer_method1 () ");
System.out.println ("outer_1" + "outer_2");

13//The inner class in the method
innerclass{
15//Properties in the Inner class
+ private int inner_1=123;//private property
+ public int Inn er_2=321; Public Property
18//The public method in the inner class
, Inner_method1 () {
System.out.println ("method1");
System.out.println (outer_1);

23//Private method in inner class
Inner_method2 () {
System.out.println ("Private method2");
System.out.println (outer_1+ "" +outer_2);
+}}
28//Instantiate internal class
Innerclass ic=new innerclass ();
Ic.inner_method1 ();
Ic.inner_method2 ();
System.out.println (ic.inner_1+ "" +ic.inner_2);
33//scope ends here
{{}
}
Copy Code
Third, anonymous inner class

    1. The anonymous inner class is not an access modifier.
      2. Sometimes I prefer to use an anonymous inner class in order to avoid naming the inner class, because it has no name.
      3. Anonymous inner classes are not constructed. Because it doesn't even have a name to construct the method.
      4. The method implemented in the current anonymous inner class can only be implemented once, and if you want to implement another implementation, you must re-implement it with the anonymous inner class.

Copy Code
1 public class Demo {
2 public void TetMethod1 () {
3//Anonymous inner class new is the child implementation of the Commonperson, but there is no class name, the implementation of the object assigned to the elder reference
4 Commonperson cp=new Commonperson () {
5 @Override
6 public void Study () {
7}
8 @Override
9 public void Eat () {
10}
11};
12}
public static void Main (string[] args) {
14//Famous inner class
Class Student implements iperson{
+ public void Study () {}
+ public void Eat () {}
18}
Student stu1=new Student ();
IPerson IP=STU1;
21st
22//Anonymous inner class
IPerson ip1=new IPerson () {
public void Study () {}
public void Eat () {}
26};
IPerson ip2=new IPerson () {
-public void Study () {}
public void Eat () {}
30};
31}
32}
33
34
Interface IPerson {///defined interface IPerson
36
PNS public void Study ();
A. public void eat ();
39}
Copy Code
Iv. nested INNER classes
Nested inner classes are divided into static inner classes and non-static inner classes
1. Static Inner class
1, its creation does not need to rely on the external class.
2. It cannot use non-static member variables and methods of any perimeter class.
2. Non-static inner class
1. It needs to depend on the external class for its creation.
2. A reference is implicitly saved after compilation, which points to the outer class that created it, and can be arbitrarily invoked on members of the outer class, whether static or non-static, rather overbearing.

Copy Code
1 public class Outerclass {
2 private String sex;
3 public static String name = "Ma";
4/
5 static Inner class
6
/
7 Static Class innerclass1{
8/Static members can exist in static internal classes
9 public static String name1 = "1_static";
Ten public void display () {
11//Static inner class can only access static member variables and methods of the perimeter class
System.out.println ("Outerclass name:" + name);
13}}
/

15non-static inner class
-
/
Class innerclass2{
18/static members cannot exist in non-static inner classes/
Public String name2 = "2_inner";
20/any member of the enclosing class can be called in a non-static inner class, whether static or non-static, which is a bit overbearing./
public void display () {
System.out.println ("Outerclass name:" + name);
23}
24}
public void display () {
26//External class access static inner class
System.out.println (INNERCLASS1.NAME1);
New InnerClass1 (). display ();
29/non-static internal creation needs to be dependent on the external class/
Outerclass.innerclass2 inner2=new Outerclass (). New InnerClass2 ();
System.out.println (inner2.name2);
Inner2.display ();
33}
34}

Java internal Class A summary of the use of Beijing PK10 Platform Rental

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.