Java Object-oriented note 8

Source: Internet
Author: User

If you put a class inside another class, that class is called an inner class, and the main function of the inner class is as follows:


1, the inner class provides a better encapsulation, you can hide the inner class within the external class, do not allow other classes in the same package to access the class.


2. Internal class members can access private data of the external class directly, because the inner class is treated as its outer class member, and the members of the same class can interact with each other.


Access. However, an external class cannot access the implementation details of an inner class, such as member variables of an inner class.


3. Anonymous inner classes are suitable for creating classes that need to be used only once.


Member inner classes are divided into two types: static inner classes and non-static inner classes, member inner classes that use static adornments, and static inner classes that do not use static


A decorated member inner class is a non-static inner class. For example:

public class Work {public class Teacher{}static class student{}}
The teacher class is a non-static inner class, while the student class is a static inner class.


When a variable is accessed within a method of a non-static inner class, the system takes precedence within the method to find out if a local variable exists for that name, and if so,


Use this variable, or if it does not exist, find out if there is a member variable of that name in the inner class where the method exists, and if so, use that member to change the


If it does not exist, go to the outer class where the inner class is located to find out if there is a member variable of that name, and if so, use that member variable;


If it does not exist, the system will see a compile error indicating that the variable is not found. Therefore, if the outer class member variable, inner class member variable and inner class method


The local variable with the same name can be distinguished by using the this, the external class class name. This is defined as a qualification, for example:

public class Work {string Name= ' work ';p ublic class teacher{string name= "Teacher";p rivate Void Test () {string name= "test"; System.out.println ("Name of The outer class:" +work.this.name); System.out.println ("Member of the Inner class name:" +this.name); SYSTEM.OUT.PRINTLN ("Local variable in inner class method name:" +name);}} Static class Student{}public static void Main (string[] args) {work.teacher teacher=new work (). New Teacher (); teacher.test ();}}
Output Result:

Name:work of external classes
Member of the Inner class Name:teacher
Local variable Name:test in inner class method

The above program obtains the instance variable of the outer class, the instance variable of the non-static inner class respectively.


If you use static to decorate an inner class, the class belongs to the outer class itself, not to an object of the outer class, so the inside of the static decoration is used


A class is referred to as a static intrinsic.


A static inner class can contain static members, or it can contain non-static members. Static inner classes cannot access instance members of external classes, only external classes


The class member. A static inner class is a static member of an external class, so static methods, static initialization blocks in the outer class can use static inner classes to


Define variables, create objects, and so on. External classes still do not have direct access to members of static inner classes, but can use the class masterpieces of static inner classes as callers


To access a class member of a static inner class, you can also use a static inner class object as the caller to access the instance members of the static inner class.


Anonymous inner classes are suitable for creating classes that need to be used only once, an anonymous inner class must inherit a parent class, or implement an interface, but can inherit at most


A parent class or implement an interface. Two rules for anonymous inner classes:


1. Anonymous inner classes cannot be abstract, because when an anonymous inner class is created, an object of an anonymous inner class is created immediately, so anonymous


The inner class is defined as an abstract class.


2. An anonymous inner class cannot define a constructor because the anonymous inner class does not have a class name, so the constructor cannot be defined, but an anonymous inner class can define the initial


Initializes the block, using the instance initialization block to accomplish what the constructor needs to accomplish.


The following are the most commonly used anonymous inner class usages:

Public interface Info {public String name ();p ublic int Age ();}

public class Teacher {public void Test (info info) {System.out.println ("name=" +info.name () + "\nage=" +info.age ());} public static void Main (string[] args) {Teacher teacher=new Teacher (); Teacher.test (new Info () {@Overridepublic String nam E () {return "Bill";} @Overridepublic int Age () {return 25;}});}}

Output Result:

Name=bill
Age=25


A closure (Closure) is an object that can be called, which holds the scope information to create it, and for a static inner class, it not only records its external


class, it retains a reference to creating a non-static inner class object, and can invoke the private member of the external class directly, so you can


A non-static inner class is considered a closure of an object-oriented domain. This kind of non-static inner class of the pseudo-closure can implement the callback function conveniently, and the callback is


Once a method has obtained a reference to an inner class object, it can then, in turn, invoke the method of the external class instance in the appropriate way. For example, the following program:

Public interface Info {public void work ();}

public class Teacher {private string name;public Teacher (String name) {this.name=name;} public void Test () {System.out.println (name+ "at Work");} Private class work implements info{@Overridepublic void work () {test ();}} Public Info Getworkinfo () {return new work ();} public static void Main (string[] args) {Teacher teacher=new Teacher ("Bill"), Teacher.getworkinfo (). Work ();}

Output Result:

Bill's at work.


In the above program, callback the teacher object's test method, the inner class can easily callback its external class field, method, which can improve the flexibility of programming.





Reprint Please specify source: http://blog.csdn.net/hai_qing_xu_kong/article/details/43883197 Emotional Control _  











Java Object-oriented note 8

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.