2013年9月16日星期一

Asked one about this static inner classes and scoping issues

abstract class OutClass {
int a;

private static class InClass {
public InClass () {
System.out.println (OutClass.this.a);
}
public void test () {
System.out.println (OutClass.this.a);
}
}
}

code above

1 compile error I asked why not visit a?
2 two different places System.out.println (OutClass.this.a); in this represent what is ?
3 external class attribute different access types will produce what kind of internal class effect ?
4 static inner classes and on what impact ?
5 then the internal class the meaning of existence , what is it ?
6 internal class relations and external class is kind of how ?
many problems ...... great God who seek answers ...... only 100 points of all to the ......
------ Solution ------- -------------------------------------
this is not all the procedures , right ? ? ?
this refers to the current object , OutClass.this. this role , do not understand
------ Solution - -------------------------------------------
you this OutClass. this.a What does it mean ? ? ? No sense ah.
------ Solution ---------------------------------------- ----
inner class
1. inner class access rules
(1) inner class can directly access external class members, including private
The reason why you can directly access the external members of the class , because the inner class holds a reference to the outer class format : External class name . this
(2) external class to access the internal class, you must create an internal class object. 2 Visit format when the internal class definition in the external members of the class position, and non-proprietary , other classes can be external , internal class object can be created directly Format: external class . Internal class variable name = external object internal class object ;
When the internal position of the member , it can be modified by members of modifiers such as private, inner class will be encapsulated in an external class static: static inner classes to have the characteristics of an inner class is static when modified , can directly access external class static member Access limitations appeared
other classes in the external , how to directly access a non- static inner class static members do new outer.Inner (). function ();
Note: When the internal class defines a static member of the inner class must be static : when the external static methods of the class access to internal classes, inner classes must be static
inner classes are defined in local time , can not be members of the modifier modifier , you can directly access the external class members because also holds a reference to the outer class . But can not access it in the local variable can only access local variables are final modified .

look red font
------ Solution ------------------------------- -------------
static inner classes are modified , only the outer class static attribute for a visit.
a very simple example , static inner classes modified , you can not instantiate , this time to visit only need to instantiate a non- static external variables , there will be access error variables need to instantiate a class to exist , and the internal static class does not need to instantiate also be able to visit , a contradiction .
conclusion is static inner class can not access the external class non-static variables.
------ Solution ---------------------------------------- ----
1, because you are static inner classes , if the static removed, all right friends !
2, OutClass.this is a reference to the outer class object
3, the inner class has all the elements of its enclosing class access ( non-static inner classes )
4, also known as static inner classes nested class , use this name to give him a more defined image . This means that within the class and outside the class relationship has only hierarchical nested relationship, so just create a class file when the class file name is of the form : outer $ inner.java, complete and in the use of the same general category two .
5, within the meaning of class , see "java programming ideas " in the chapter on why you need an inner class inner class sections , that is very detailed !
6, static inner classes and class relations outside it , just 4 as non-static on complex point , "java programming ideas ," said , when an external class object creates an internal class object , this inner class secretly capture a brother pointing enclosing class object, when you visit enclosing class members is through the reference implementation !

not guaranteed right or wrong, for reference only
------ Solution ---------------------------- ----------------
1 compile error I asked why not visit a?
static inner class can only access static members outside ;
2 two different places System.out.println (OutClass.this.a); in this represent what is ?
are the outer class object.
3 external class attribute different access types will produce what kind of internal class effect ?
ordinary inner classes have external access to all members of the class ;
static inner class can only access static members outside ;
described above are members of the class outside access to internal class does not affect ;
4 static inner classes and on what impact ?
static inner class can only access static members outside ;
create static inner classes do not need to create an external class ( class of ordinary internal needs ) ;
static inner class nested inside but also ordinary inner classes , while the ordinary static inner classes can not be nested inside , it can not contain static members ;

5 then the internal class the meaning of existence , what is it ?
further encapsulation ;
achieve multiple inheritance ;
6 internal class relations and external class is kind of how ?
inner class by default with external references to classes ;
------ Solution ---------------------- ----------------------
first : Because it is a static inner classes can not contain this, there is a static inner class to access external class members , the external class must be static, which is the problem of the life cycle . .
Second: external class . this represents an object instance of the outer class
Third: General category ( except for inner classes ) are only two access. public and the default package access. Therefore, no impact
Fourth: the demand is not clear. . . .
Fifth: internal class members are divided into inner classes and local inner classes . Members of the inner class is an external member of the class attribute , member properties that describe a class's public properties. Local inner class methods exist within
Sixth: inclusion relation , also called hasa
------ For reference only ---------------------------------------
on this short
he were in two (OutClass.this.a); painted red at
follows


So I want to know why is it so ?
------ For reference only -------------------------------------- -
I help you answer a few questions:
1 compile error I asked why not visit a?


4 static inner classes and on what impact ?
I do not know where you get the code , I had concluded an internal class rule consists of :
inner classes can not declare any static members ; Only top-level class can declare static members . Therefore, one needs an inner class static member must come from the top-level members of the class .

5 then the internal class the meaning of existence , what is it ?
1, cite a simple example, if you want to implement an interface, but this interface in a way and your idea of this class a method name , parameters are the same , how should you do ? At this time, you can build an internal class that implements this interface. Due to internal class content of the outer class are accessible to all , so everything you do can be done directly implement this interface functionality.
2, the real reason is this , java classes and interfaces in the interior together, can solve the C + + programmers often complain that there is a problem in java : No multiple inheritance . In fact , C + + multiple inheritance designs are complex , and java classes together through the internal interface, you can achieve very good multiple inheritance effect .

6 internal class relations and external class is kind of how ?
Here is an inner class features:
(1): nested class ( inner class ) can reflect the logical affiliation . While controlling for the other classes can be seen within the class is not visible , etc.
(2): external class member variable scope is the entire outer class , including nested classes. But the external classes can not access private members of nested class
(3): logically related classes can be together, can effectively achieve information hiding.
(4): internal class can directly access the external members of the class . This can be achieved using multiple inheritance !
(5): After compilation, inner classes have been compiled into a separate class , but the name is outclass $ inclass form.

can refer to , the following blog , I want to have something you want .

http://blog.csdn.net/gaopeng0071/article/details/9887001
hope to help to you ~ ~

没有评论:

发表评论