Internal class topic (self-study)

1. The method can not be defined inside the class?


It may be an internal class method.


2. Please describe the manner defined inside the class and explain what applies?


(1) Internal class members: defined inside (external type) of another class, and the members of the same level methods and properties, belonging to a member of the class, use can be private, default, protected, public access modifiers four modifications.
External class can not directly access the methods and properties of members of the inner class, we need to be accessed through an instance of the inner class.
Called: External Internal class name class name outside the class instance name = name .new inner class instance constructor (parameter).
(2) static inner classes: the modified static member inner class called static inner classes.
Called: External Internal class name name = new instance of class name class name inside the outer class name (parameter).
(3) Internal partial class: class definition of class called internal local code block or in vivo methods.
Only in a partial inner class code blocks and in vivo methods (e.g., using the class object to create the object and the like).
(4) anonymous inner classes: internal local class specific.
Must inherit a class (abstract, abstract can) or implement an interface. If the parent (or parent interface) is an abstract class, the anonymous inner classes must implement all abstract methods.
After only once, create an instance of the class definition will immediately disappear. Created: parent (Interface) instance name = new parent () {
// anonymous inner class needs to implement functionality of.
}
Anonymous inner class can not be abstract because anonymous inner classes after the definition creates an instance immediately.
Anonymous inner classes can not define constructors, anonymous inner class has no class name, constructor can not be defined, however, an anonymous inner class has a parent class all the same construction method.
Code block can be defined, for example to initialize.

 

3. Ordinary members of the inner class can not directly access ordinary members outside of class?


Ordinary members can directly access the internal members of the class outer class, as for example: internal class innerMethod () methods:
public void innerMethod () {
int NUM = 40;
System.out.println (OuterClass.this.num); // outer class member variables;
System.out.println (this.num); 30 //
System.out.println (NUM); 40 //
System.out.println (name);
}

 

4. [machine] write: typical way ordinary members of the inner class called.


Create instance of members within the class: class name External Internal External class name class instance name = name .new inner class instance constructor (parameter).

External type (1) to create within the class comprising:
public class OuterClass was {
// member variable
Private NUM = 20 is int;
Private String name;
// Constructors
public OuterClass was () {
Super ();
}
public OuterClass was (String name) {
Super ();
this.name = name;
}
// member methods
public void execInnerClass () {
this.name = "ABC";
InnerClass InnerClass new new IC = ();
//ic.num=30;
ic.innerMethod () ;
}
// inner class
public class InnerClass {
Private int NUM = 30;
public InnerClass () {
Super ();
}
public InnerClass (int NUM) {
Super ();
this.num NUM =;
}
public void innerMethod(){
int num = 40;
System.out.println(OuterClass.this.num);//20
System.out.println(this.num);//30
System.out.println(num);//40
System.out.println(name);
}
}
}

(2) call the internal class members: two common methods;
1) create external object, from the outside to create an internal class object class object
format: class name inside the external name class object name = internal external object class object;..
OuterClass was = new new OuterClass was OC2 ();
OuterClass.InnerClass oc2.new InnerClass IC = ();

2) Create external object, the method of operation of the internal outer class class
OuterClass was OuterClass was new new OC = ();
oc.execInnerClass ();

 

5. Static member inner class can not directly access the outer class ordinary members? Can not be accessed outside the class static members?


Static inner class
1 can not access the non-static member (Method or variable) of the outer class.
2. Have access to static members outside of class.

 


6. [machine] write: typical ways static inner class called.


(1) Create a class static external inner class:
public class OuterClass was {
// member variable
Private NUM = 20 is static int;
Private static String name;
// Constructors
public OuterClass was () {
Super ();
}
public OuterClass was (String name) {
Super ();
this.name = name;
}
// member methods
public void execInnerClass () {
this.name = "ABC";
InnerClass InnerClass new new IC = ();
//ic.num=30;
IC. innerMethod ();
}
// inner class
public static class InnerClass {
Private int NUM = 30;
public InnerClass () {
Super ();
}
public InnerClass (int NUM) {
Super ();
this.num = NUM;
}
Public void innerMethod () {
int NUM = 40;
//System.out.println(OuterClass.this.num);//20
System.out.println (OuterClass.num);
System.out.println (this.num ); // 30
// 40; (NUM) System.out.println
System.out.println (name);
}
}
}
(2) internal static class instance object can be created directly, not necessarily attached to the outer class instance:
OuterClass was. InnerClass ic = new OuterClass.InnerClass ();
format: external internal class name class name object name = new class name inside the outer class name ();

7. When will the use of anonymous inner classes?


In some cases, certain types of objects created once and then not used. In this case it may not be provided with the name of the class, but the use of anonymous inner classes.

Guess you like

Origin www.cnblogs.com/ren549047861/p/11294224.html