Java Internal classes

Source: Internet
Author: User

Today we have studied three internal classes mainly

respectively:

1. Member internal classes (member inner Class)

Not open to the outside, high cohesion

 PackageMemberinner; Public classTest {//Class B is an inner class in Test.class.    classB { Public voidGo () {System.out.println ("Haha, you don't know the secret!!! "); }    }         Public voidShow () {b b=NewB ();    B.go (); }         Public Static voidMain (string[] args) {NewTest (). Show (); }}

The identity of the internal class file is generated in the bin file of your project after running.

2. Method Inner Class (Methods inner Class)

A. The inner class of the method is the inner class definition in the method of the outer class, the inner class of the method is visible only within the method, that is, it can be used only inside the method.

B. When the method inner class is used, an inner class object is created in the method, and the method in its inner class is called with the object.

Note: Because the method inner class cannot be used outside the method of the outer class, the method inner class cannot use access control to conform to the static modifier

In short, it is a temporary, partial. See chestnuts:

 PackageMethodinner; Public classTest { Public voiddriver () {SYSTEM.OUT.PRINTLN ("I was driving."); //System.out.println ("Hit the Dead");//How to make the car accident that you are responsible for becomes okay//call the Noproblem method in Duanzanshichangzheng.class        classBextendsDuanzanshichangzheng {}NewB (). Noproblem (); System.out.println ("At last, I am not a matter, sick, is so capricious!! "); }         Public Static voidMain (string[] args) {NewTest (). driver (); }    }

3. Anonymous internal classes (anonymous inner class)

Anonymous inner class is also an inner class without a name

Because there is no name, the anonymous inner class can only be used once, and it is often used to simplify code writing

But there is also a precondition for using anonymous inner classes: You must inherit from a parent class or implement an interface

We can get through this chestnut, to be able to understand.

 Package Anonymousinner;  Public Abstract class Implements qinshou{            publicvoid  workinday () {        System.out.println ("impart theoretical knowledge during the day.") ");    }    }
 Package Anonymousinner;  Public Interface Qinshou {        publicvoid  workinnight ();}
 package   Anonymousinner;  public  class  Doctor1 extends   doctor{ public  void   Workinnight () {Syst    Em.out.println ( teach hibernate at night 
 Package Anonymousinner;  Public class extends doctor{    publicvoid  workinnight () {                System.out.println ("The evening, the moon is black and high, We want to learn Android "completely");}    }
 PackageAnonymousinner; Public classTest { Public Static voidMain (string[] args) {Doctor1 D1=NewDoctor1 ();        D1.workinday ();                D1.workinnight (); Doctor2 D2=NewDoctor2 ();        D2.workinday ();                D2.workinnight (); /*** Anonymous inner class syntax three steps: * 1.new an abstract class or interface * 2. Add a curly brace * 3. Adding a method to it as an implementation*/Doctor D3=NewDoctor () { Public voidworkinnight () {System.out.println ("I'm doctor3, we're going to learn oracle at night.");        }        };        D3.workinday ();    D3.workinnight (); }}

Java Internal classes

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.