Nested class (generally C + +), Inner class (generally Java). The most important difference between a Java inner class and a C + + nested class is whether there is a reference to an external point. Visible http://java.ccidnet.com/art/297/20060325/489133_1.html Note: Static inner class (Inner Class) means that 1 creates an object with a static inner class that does not require an external class object. 2 Cannot access an external class object local class from an object in a static inner class A class defined in a function body is called a local class. The local class can only be contacted using objects and functions in its perimeter scope, because the variables in the perimeter scope are not related to the objects of the Bureau class. When defining a local class, it is important to note that static member functions cannot be described in a local class, and that all member functions must be defined in the class body. In practice, local classes are rarely used. The following is an example of a local class. int A; void Fun () { static int S; Class A { Public void init (int i) {s = i;} }; A m; M.init (10); } Nested classes Classes defined in a class are called nested classes, and classes that define nested classes are called peripheral classes. The purpose of defining a nested class is to hide the class name and reduce the global identifier, thereby limiting the ability of the user to create objects using the class. This improves the abstraction of the class and emphasizes the master-slave relationship between the two classes (the perimeter class and the nested Class). The following is an example of a nested class: Class A { Public Class B { Public ... Private ... }; void f (); Private int A; } Where Class B is a nested class, Class A is a peripheral class, and Class B is defined in Class A's class body. Several descriptions of nested classes: 1. From a scope perspective, nested classes are hidden in the perimeter class, which can only be used in peripheral classes. If the class name is used within the scope of the perimeter class, a name qualifier is required. 2. From the point of view of access rights, the nested class name has the same access rule as the object member name of its perimeter class. You cannot access a private member function in an object of a nested class, nor can you establish an object on a nested class in the private part of the outer class. 3, a member function in a nested class can be defined outside of its class body. 4. The members described in the nested class are not members of objects in the perimeter class, and vice versa. A member function of a nested class has no access to the members of the perimeter class, and vice versa. In this case, nested classes are often treated as not nested classes when analyzing the member access relationships between nested classes and peripheral classes. In this way, the nested class above can be written in the following format: Class A { public:200 void f (); Private int A; }; Class B { Public ... Private ... }; The nested class is only syntactically embedded, as the citation is visible. 5. Friends that are described in nested classes do not have access to members of the perimeter class. 6, if the nested class is more complex, you can only in the perimeter class to describe the nested class, the detailed content of the nesting can be defined in the outer class of the file field outside the body. Reference: Preface: Originally wanted to sum up the use of inner class, but found that these few quite close. Let's pull out together and sneak out. Purpose of writing: With static, final, inner class good relationship, so as to meet in the future, as if, else, as clear. The terms defined in this article are based on the Java language spec.
Just think about what's going on in those places. 1. Grammar. (Code, we eat the guy) 2. Semantic. (In fact, writing code is for this) 3. How to use a simple, pleasant structure to express semantics. (This is what every programmer pursues) Semantic infinity, grammar is limited, we have to understand the semantics of different grammars, this is the focus of this article.
1. Final article Final as an independent existence, but also the performance of the different. It is generally understood that can ' t be changed. 1 Final data: implement constant semantics. This value is not changed at compile time and cannot be changed at run time. In Java, blank final: Allows you to delay the initialization of the action into the constructor. This is the limit, there are compiler guarantees. 2 Final parameter: Ditto semantics. 3 Final method: A) Prevent overriden of subclasses. (Note: This angle, private implied final semantics) b) Efficiency: Allow compiler conversion calls to inline call. 4 Final class: Reject inherited.
2. Static article 1. Definition: Static is modifier. I wanted to find an authoritative definition for everyone, only to find out that there is no specific scope for this object. It also means that there may be a new usage tomorrow, which also shows the infinite extensibility of the language. No, we have to note here: The following usage is the description before Java 1.5:
Gossip: This static is very bad, with whom the whole is spoiled. If you want to advertise your independent personality, please use it. Static one, it shows that "I am the public servant of the people, you just find me, you can let me serve you." It belongs to the definition hierarchy and is unique at the JVM level. From another perspective, it can be used only for member definitions. We cannot define a top-level class as static. public static class Outest {//Compile error ...; }
Loading: Because a static member (include field, method, nested Class) is stored at the class definition level, any action associated with its class will produce its initialization action. (it means that it's ready and you just want to use it.) Classname.staticmember)
All static object/primitive and static blocks, performing initialization operations in the order of arrival. Like other.
Can be used in: 1 static import (static import ...) Single-static-import declaration:import static typename.identifier makes access identification simpler. (Simple, no ambiguity) 2 static initializer: (Static {}): Completes a section of initialization. Common usage: 3 static field: (Static Type field) static field: Alias: Class variable; Non-static Fileds: Alias: Instance variable. 4 static method: (Static Type Method ()) Ditto. 5) static member type. (Member Class & member Interface: Here in the nested class section to explain)
The JVM is the source of implementing the semantics of these Java programs: class variables are stored as part of the type information in the method area (the memory structure that the JVM places the class definition). Before using a class, the JVM must allocate space for these class variables in the method area. So the class variable is shared by all instances of the class, and it can be accessed even if there are no class instances. These variables are only related to the class.
Here are the plays: nested class. 3. Nested class Chapter When it comes to class definitions, let's take a look at what's interesting: In Java, the representation of a class declaration can be divided into two types: 1 Top Level class: 2) Nested class: DEFINE:NESTD class is any class whose declaration occurs within the body of another class or interface. A class is nested class.
And in nested class we need special attention to the use of inner class, local class, anonymous class. When discussing the following two special definitions, let's take a look at an important concept: inner class. 1 A inner class is a nested class, is not explicitly or implicitly declared Static. Inner classes may does declare static initializers or member interfaces. Inner classes may does declare static members unless they are compile-time Constant fields. (Restriction on inner class: cannot declare static initialization operation) Here I want to clarify one point: Nested class!= inner class, inner class is a special Nested class. Uncle Bruce thinking the two concepts in the Java 3rd. (Khan, until I wrote this article, I was only investigating, fortunately I wrote the Java language spec and JVM spec for reference) Look at the inner class form. * * Common SITUATION * * Class hasstatic{ static int j = 100; | Class outer{ Class Inner extends Hasstatic { static final int x = 3; OK-COMPILE-TIME constant static int y = 4; Compile-time error, an inner class } Static Class Nestedbutnotinner { static int z = 5; OK, not a inner class } Interface neverinner{}//interfaces are never inner }
2) Local Class:is a nested class, ' is ' not ' a ' any class and ' has a name. All local classes are inner classes. Local class cannot be declared with public, protected, private, or static (compilation error). As we can see from the definition, its lifetime is block scope. Comparing the form of the local variable, it is not difficult to understand its semantics. Also you can't use modifier (public/static etc.) To define a local variable. E.p Class Test { public static void Main (string[] args) { int i; public class local{}: Compile Error:illegal start of expression Class Local { { for (int i = 0; i < i++) { System.out.println (i); } }
public void P () { SYSTEM.OUT.PRINTLN ("Call local class P ():"); } } Local L = new local (); L.P (); } } 3) anonymous class:is automatically derived from a class instance creation by the expression. : 1.never abstract; 2.inner class; 3.never static; 4.impicitly final. Interface contents{}; public class Parcel6 { Public Contents cont () { return new Contents () { private int i = 11; public int value () {return i;} }; } public static void Main (string[] args) { Parcel6 p = new Parcel6 (); Contents C = P.cont (); } } ///:~
This seemingly mysterious structure allows you to integrate the definition of a class with the construction process. In fact, the semantics it implements are: Class Mycontents implements Contents { private int i = 11; public int value () {return i;} } return new mycontents (); (impicitly final) so that everyone is at a glance.
Look at the above or simple, or complex class definitions. Maybe we all have some headaches. Why does Java provide such a rich way to define classes? Let us ask ourselves, what is the purpose of our defining class? is to make our program have a better logical structure. A class definition is the form of an information organization. The Java language gives us the encapsulation that allows us to do information at each implementation stage, and is really everything is object. These are the things we should be happy about. It is never a burden, just give us more choice space.
Semantic features: (the definition in the following inner class is thinkinginjava3th can be considered as the nested class defined in this article) Each inner class can independently inherit from a implementation. Thus, the inner class is isn't limited by whether the outer class are already inheriting from the implementation. (is not seen a point, more inheritance.) Visitor pattern. ) 1. The inner class can have multiple instances, each with its own state information this is independent to the information in The outer class object. An independent entity, born in a larger entity (an external entity), can use the resources of an external entity (please associate parasite:<) 2. In a single outer class with can have several inner classes, each of which implement the same interface or inherit the Same class in a different way. (can have different kinds of parasites) 3. The point of creation of the inner class object isn't tied to the creation of the outer class object. (Its creation process is not created by external objects) 4. There is no potentially confusing "is-a" relationship with the inner class; It ' s a separate entity. (no inherit relationship to external class)
After the instructions, do you have a clearer idea: Inner Class common usage: Closures & Callbacks 1. In Java, you can use inner class to implement closure (closures) and Callback (callback) Semantics: Definition of closure: (I try to explain this concept from different sides) Martin fowler:essentially A closure is a blocks of code that can be passed as a argument to a function call. Bruce:a closure is A callable object, retains information from the scope in which it was created. So when do you use it? What is the advantage of it? For example: let you look after the child: Do you want a baby or a pupil? I think most people will choose primary school students. Why. Because primary school students can eat their own, the toilet, their own bath. Which of these two objects do you prefer to achieve?
Call Back:with a callback, some, "other object is given a piece of information" ing object at some later point. (To make a controllable interface) |