java self-study diary no.7

data:2020/2/17

Four access modifiers

One basic idea of ​​object-oriented Java package is disclosed in detail and the interface. java language access control using access control method modifier classes and variables and, thereby exposing the interface to the user, but implementation details are hidden, access specifier into four levels:

public

Public class with modified, and methods of generic variables, the bag and any class (general class and subclass including) the outer package can access

protected

A protected modified classes, generic variables and methods, and any class within the packageOutsource those inherited subclasses can access the classprotected focused inheritance

dafault

If a class, class variables and methods do not belong to any modifiers (ie no use public, protected, any modification in private), its access to default (default access).
The default access type, generic variables and methods, any class (including inherited such a subclass) can access it in the bag, and for outsourcing of any kind can not access it (this heritage including outsourcing subclass),default highlights package

private

With the private modifier classes, variables and generic methods, class can access only this, the bag and outer bag of any type can not access it

Access level Access control modifiers similar Packages with different types (excluding subclasses) Buns with class Different packages of different types (excluding subclasses) Different classes buns
public public
protected protected
default default
private private

summary:
protected focused inheritance, as long as the subclass can access even in different packages
focused default package, available only from access to the same classes in the package
belonging to subclass restriction modifier protected, and limits the packet belongs to the default modifier

Inner classes

Internal class (general class defined inside the abstract class interface collectively) means a structure nested relationship, i.e.,In addition to the interior of a class attributes and methods may also continue to define a class structureStructure is defined, thus making the program more flexible

Internal class features:

1. The inner class can directly access external class members, including private

2. External class needs to access internal class members, you must create an object, you can create a method in the outer class, and then create an internal method in the class object to call the internal class members

3. External class internal object name = class name external object. Inside class object
( Outer.lnner new new Outer OI = (). Lnner new new () )

4. static inner classes , as said before, you canDirect access with the class name inside a static class methodTherefore: External Internal class object class name = external internal class object class name, i.e.
( Outer.lnner new new Outer.lnner OI = () )

The private inner class: For the members of the class have private internal use, private, can only be accessed by methods of the current class, so you can create a method in the outer class, to create an internal class object and then use the inner class method

6. Get External inner class class members, external class name .this. External class members

7. Local inner class: class method defined in the class, when a local variable in local inner class method access where he must use finnal modification,Because after the end of the method popped up, went to the pop-up variables, but class is still the pile, but also use variables, so add the variables into the method area finnal constant pool (jdk1.8 no need to add a finnal If there is added finnal bug)

8. The anonymous inner classes: class is a topical inside, must be written in the method, the nature of a class inherited (usually abstract class) or subclass that implements the interface anonymous objects, preferably only anonymous inner classes interface or only for a class method, there is a fixed format

Published 38 original articles · won praise 4 · Views 837

Guess you like

Origin blog.csdn.net/Hide111/article/details/104356626