Java Internal class

Source: Internet
Author: User

1: Problems with formal parameters and return values (understanding)
(1) Formal parameters:
Class Name: An object that requires this class
Abstract class Name: A subclass object that requires this class
Interface Name: An implementation class object that requires this interface
(2) Return value type:
Class Name: Returns the object of the class
Abstract class Name: Returns the subclass object of the class
Interface Name: An object that returns an implementation class for the interface
(3) Chain-type programming
Object. Method 1 (). Method 2 () .... Method N ();

This usage: In fact, after the invocation of Method 1 (), an object is returned;
Method 2 () returns an object after the call is complete.

After the method n () call is complete, the object may or might not be an object.

2: Inner Class (understanding)
(1) The class is defined inside another class, which is called an inner class.
Example: Class B is defined in Class A, and Class B is called an inner class.
(2) Access rules for internal classes
A: Members of external classes can be accessed directly, including private
B: External class to access internal class members (including private members), you must create an object

Class Outer {private int num = 10;class Inner {public void Show () {System.out.println (num);}} public void Method () {<span style= "white-space:pre" ></span>//external class accesses inner class Inner i = new Inner (); I.show ();}}
(3) Classification of internal classes

Class Outer {private int num = 10;//member position class Inner {}public void method () {//local position class Inner {}}}
A: Member Inner class: in member Position

/* member Inner class: How to access the members of an inner class directly. The external class name. Internal class Name Object name = External Class object. Inner class object; */class Outer {private int num = 10;class Inner {public void Show () {System.out.println (num);} }}class InnerClassDemo3 {public static void main (string[] args) {//requirement: I want to access the show () method of the Inner class//format: external class name. Internal class Name Object name = External class object. Inner class object; Outer. Inner oi = new Outer (). New Inner (); Oi.show ();}}


B: Local inner class: Inside the method
(4)member Inner Class
A:private for the security of the data
B:static for ease of access

the member inner class is not static:
The external class name. Internal class Name Object name = new External class name (). New internal class name ();
The member inner class is static:
The external class name. Internal class Name Object name = new External class name. Internal class name ();

/* Modifier for the inner class of the member: Private to ensure the security of the data static for easy access to data note: External class data accessed by static internal classes must be statically decorated. Because static data is allocated when the class loads the memory space is the case: I have a person who has a body and a heart inside. ) class Body {Private class Heart {public void operator () {System.out.println ("Heart");}} public void Method () {if (...) {//Plus, the qualifying condition can be accessed both heart H = new Heart (); H.operator ();}} As we have just explained, use body.heart BH = new Body (). new Heart (); Bh.operator ();//Add private, you can not be visited, then, how to play it? <span style= " Color: #ff0000; " > Access the private inner class Body B = new body via an external class call method (); B.method (); </span>*/class Outer {private int num = 10;private static int num2 = 100;//Inner class is statically decorated because an inner class can be considered a member of an external class public static class Inner {public void Show () {//non-static//system.out.println (num); System.out.println (num2);} public static void Show2 () {//static//system.out.println (num); System.out.println (num2);}}} Class InnerClassDemo4 {public static void main (string[] args) {////member inner class is accessed by static modification://Format: external class name. Internal class Name Object name = new External class name. Internal class name (); /Because static can be accessed directly through the class name, you only need to create an inner class object to <span style= "color: #ff0000;" >outer.inner oi = new Outer.Inner (); </span>oi.Show (); Oi.show2 ();//show2 () Another way to call Outer.Inner.show2 ();}} 

(5) Questions of inner class of members (fill in the blanks)
Output: 30,20,10class Outer {private int num = 10;class Inner {private int num = 20;public viod Show () {int num  = 30; SYSTEM.OUT.PRINTLN (num);//30system.out.println (this.num);//20<span style= "color: #ff0000;" >system.out.println (New Outer (). num);//10system.out.println (Outer.this.num);//Interview General interview this, 10</span>}}

(6) Local inner class
A:local internal class access local variables must be final decorated.
B: Why?
Because local variables are used to disappear, the objects of the heap memory do not disappear immediately.
The object is still using the variable, and the variable is gone.
In order for the value to exist, add the final modifier to make it a constant
With the anti-compilation tool, we see that, when final is added, the heap memory directly stores the value, not the variable name

/* local inner Class A: Members of the external class can be accessed directly b:<span style= "color: #ff0000;" > In a local location, you can create an inner class object that invokes an inner class method through an object to use local inner class features </span> interview questions: Considerations for local internal classes accessing local variables? A: Local internal class access local variables must be decorated with final B: Why? Local variables are called as the method is called, and disappear as the call finishes. The objects in the heap memory do not disappear immediately. So, we add final retouching. When the final modifier is added, the variable becomes a constant. Since it is a constant. You are gone. I'm storing data 20 in memory, so I still have the data in use. */class Outer {private int num = 10;public void <span style= "color: #ff0000;" >method () </span> {<span style= "color: #ff0000;" >final</span> int num2 = 20;//final type decoration <span style= "color: #ff0000;" >class inner</span> {public void Show () {System.out.println (num);//access local variable num2 from internal class; Need to be declared as the final type System.out.println (num2),//20}}//How to use the local inner class, which is different from the method of using the member inner class, need to create local <span style= "font-family" in the local location: Arial, Helvetica, Sans-serif; > Internal class objects, which call local inner class methods through objects, to use local inner class functions </span><span style= "font-family:arial, Helvetica, Sans-serif;" ></span> 
<span style= "color: #ff0000;"  >inner i = new Inner (); I.show () </span>}}class InnerClassDemo5 {public static void main (string[] args) {Outer o = New Outer (); O.method ();}}

(7) Anonymous inner class (master)
B: Prerequisites
There is a class or interface
C: Format:
New class name or interface name () {
Override method;
}
D: Nature:
is actually an anonymous object that inherits the class or implements the subclass of the interface
(8) Use of anonymous internal classes in development
When we develop, we see abstract classes, or interfaces as parameters.
And this time, we know that what is actually needed is a subclass object.
If the method is called only once, we can use the format of the anonymous inner class to simplify.
<span style= "White-space:pre" ></span>interface person {public abstract void study ();} Class Persondemo {public void method (person p) {P.study ()}} Class Persontest {public static void main (string[] args) {persondemo PD = new Persondemo ();pd. Method (New person () {public void study () {System.out.println ("Good study, day Up");}}) <span style= "font-family:arial, Helvetica, Sans-serif; Background-color:rgb (255, 255, 255); " ></span>
(9) Anonymous inner class of face question (completion code)
<span style= "White-space:pre" ></span>interface Inter {void Show (); Class Outer {//Padded code}class outerdemo {public static void main (string[] args) {Outer.method (). Show ();//"HelloWorld"}}
The code for the completion is:

<pre name= "code" class= "Java" >/*
1:outer.method () can see that method () should be a static approach in Outer. 2:outer.method (). Show () can see that the return value of method () is an object. And because there is a show () method in interface Inter, I think the return value type of method () is an interface.
*/

public static <span style= "color: #ff0000;" >Inter</span> method () {<span style= "color: #ff0000;" >return New Inter () {public void Show () {System.out.println ("HelloWorld");}}; </span>}


Java Internal class

Related Article

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.