Java Foundation inner Class (Bi Xiangdong teacher)

Source: Internet
Author: User

Inner class
Features: The inner class can access the members of the external class directly,
External classes to access the members of the inner class must create an object of the inner class.
Why do you define an inner class?
A class is used to describe something, and if there is something specific in the transaction, and the inner thing is accessing the object
, this internal thing, also need to use class to describe. This class is the inner class.
Why do internal classes have direct access to members of external classes?
Because the inner class holds a reference to an external class. The external class name. This


Static Class inner{
int num=5;
void Method () {
{
int num=6;
System.out.println ("Method run.." +num);
}
public void Show () {
System.out.println ("num+" num);
Inner in=new Inner ();
In.method ();
System.out.println ("num=" +outer.this.num);
}
}
Class haha{
Class inner{

}
}
Class innerclassdemo{
public static void Main (string[] args)
{
New Outer (). Show ();

Accesses the inner class of a class directly in another class.
Because inner is a member, you can create an object first.
Outer.Inner in= New Outer (). New Inner ();
In.method ();
The direct new inner class object. Calling a non-static member in an inner class method
Outer.Inner in= New Outer.Inner ();
In.method ();
Access static members in static inner classes, no objects are required.
Outer.Inter.function ();

}
}
}

Inner classes can also be defined at the local location of an external class.
The inner class is defined at a local location and does not allow access to variables in the local.
Only local constants that are final decorated can be accessed. Only the final decorated local constants can be accessed ...
Class Outer
{
private int num=4;
Private Object obj;
public void Show (final int y) {
final int x=5;
Class inner{
void Method () {
System.out.println ("num=" +num);
System.out.println ("x=" +x);
}
}
Obj=new Inner ();

Class InnerClassDemo3
{
public static void Main (string[] args)
{
System.out.println ("Hello World:");
}
}

method is called by the object, and X is only valid in the Show method.


Anonymous inner class
All anonymous is a shorthand format.
To define an anonymous inner class, there must be a precondition for an inner class to inherit or implement an external class or an excuse
Only then can Jane be written in the form of an anonymous inner class.

Anonymous inner class. is actually an object of the demo. This object defines the member internally with the {} end
That is, an object with member content, which is a bit fatter.

Abstract class Demo
{
abstract void Show ();
}
Class Outer
{
private int num=5;
Class Inner (What if I don't have a name?) Extends demo//more of this one, can be abbreviated
{
void Show ()
{
System.out.println ("Show Run ..." +num);

}
}
public void Method () {
Inner in=new Inner ();
In.show ();

It is described in the process of creating an object.
Format: New parent class name or interface name (). Member.


Anonymous inner class: is actually an anonymous subclass object of the demo. Demo type, polymorphic.
New Demo ()
{
void Show () {
System.out.println ("Show Run ..." +num);
}
}.show ();(object Call method. //Description on top, Mr. Bi said a little fat.

}
}
Class Noinnerclassdemo
{
public static void Main (string[] args) {
Outer out=new Outer ();
Out.method ();
}

}
//interface Inter
{
void Show ();
void Show ();
}
Class outer{
class Inner implements Inter
{
public void Show1 ()
{}
public void Show2 ()
{}
}
public void Method () {
New Inter ()
{public void Show1 ()
{}
public void Show2 ()
{}
Inner in= New Inter ();
{
private int a=3;
public void Show1 () {}
public void Show2 () {}
public void Show3 () {}
};
In.show1 ();
In.show2 ();
In.show3 ();//Not available. Because this subclass object has been upgraded for the Inter interface type, only the method of Interzhong
can be used, and the unique method of the subclass cannot be used.
}
}
}
class NoInnerClassDemo2
{
public static void Main (string[] args)
{
New Object ()// Object Obj=new Object ()
{
public void Show () {
System.out.println ("Object show Run ...");
}
Public String ToString ()
{
Return ' object toString ';
}
};
System.out.println (obj.tostring ());
Obj.show ();

New Inner ();
}
Class Inner
{
}
public void function ()
{
New Inner ();
}
}
The main function can be directly accessed by the static. A new object can be accessed with the load of the class.

Java Foundation inner Class (Bi Xiangdong teacher)

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.