Java-in-house classes

Source: Internet
Author: User

Inner class concept

What is an internal class

    Writing classes inside other classes can be written in the member and local locations of other classes, where classes written inside other classes are called inner classes . Other classes are also known as external classes .

When to use internal classes

In describing things, if a thing contains other things that might be contained inside, such as when describing a car, the engine is included in the car, and the engine can be described using an inner class.

Class Car { //External classes

Class Engine { //Internal classes

}

}  

Classification of inner classes

Inner classes are divided into members inner classes and local inner classes .

When we define an inner class, it is a normal process of defining a class, and it also contains various modifiers, inheritance, and implementation relationships. All members of an external class can be accessed directly in the inner class .

member Inner class

A member inner class that defines the position of a member in an external class. Similar to member variables in a class, accessible through an external class object

L DEFINE the format

Class External Classes {

Modifier class Inner Class {

Other code

}

}

Access mode

    The external class name. Internal class name Variable name = new external class name (). New internal class name ();

member internal Class code demo

Defining classes

Class Body {//external classes, body

Private Boolean life= true; State of Life

public class Heart {//inner class, Heart

public void jump () {

System.out.println ("The heart POPs through the jump")

System.out.println ("Life State" + living); Accessing external class member variables

}

}

}

accessing internal classes

public static void Main (string[] args) {

//Create inner class object

body.heart bh = new Body (). new Heart ();

Calling methods in an inner class

Bh.jump ();

}

Local inner class

A local inner class that defines a local location within an external class method. similar to local variables in Access methods, accessible by calling methods

Defining formats

Class External Classes {

Modifier return value type method name (parameter) {

Class Inner Classes {

Other code

}

}

}

Access mode

In the external class method, create an inner class object to access

L Local inner Class code demo

Defining classes

Class Party {//external class, Parties

public void Puffball () {//Balloon blowing method

Class Ball {//inner class, balloon

public void Puff () {

SYSTEM.OUT.PRINTLN ("Balloon swells up");

}

}

  Create an inner class object, call the Puff method

New ball (). Puff ();

}

}

accessing internal classes

public static void Main (string[] args) {

Creating an external class object

party p = new Party ();

Calling the Puffball method in an external class

P.puffball ();

}

The actual use of inner classes-anonymous inner classes

Anonymous Inner class concept

Inner classes are designed to deal with more complex inter-class relationships. View the source code will be involved, and in the daily business is difficult to meet, here do not repeat.

The most commonly used inner class is the anonymous inner class, which is one of the local inner classes.

The anonymous inner class defined has two meanings:

N temporarily defines a subclass of a specified type

create an object of the subclass you just defined immediately after the definition

Define the function and format of anonymous inner classes

function: An anonymous inner class is a shortcut to create a subclass object of a type.

Format:

New parent class or interface () {

To do a method rewrite

};

Code Demo

The parent class that already exists:

Public abstract class person{

public abstract void Eat ();

}

Defines and creates a subclass object of the parent class, and assigns a value to the parent class reference variable in a polymorphic way

Person p = new person () {

public void Eat () {

SYSTEM.OUT.PRINTLN ("I ate");

}

};

Call Eat square p.eat ();

Method

Using an anonymous object, the subclass is defined with two steps to create a subclass object by one format at a time. Although it is a two-step process, the two steps are done together.

Anonymous inner class is also an anonymous object if you do not define a variable reference. The code is as follows:

New Person () {

public void Eat () {

SYSTEM.OUT.PRINTLN ("I ate");

}

}.eat ();

Java-in-house 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.