Anonymous inner class objects +

Anonymous objects

Ordinary class objects in the use of class defines a variable of type used to save the new address out of the class is located. The anonymous class cancel this variable, the address is handled by the compiler, and after the new came out, it takes the JVM memory will be automatically recovered off. Follow-up is no longer working.
E.g

public class Student{
    public void classBegin(){ System.out.println("good morning teacher!"); } } new Student().classBegin();

Anonymous objects the most common way is to function as a parameter, such as the aforementioned print statements "good morning teacher!" It is an anonymous object, because the string is stored as an object, so there is actually an unused object reference anonymous objects.

Of course, also be anonymous object as the return value of the function.

Inner classes

The kind of inner classes: members of inner classes, static inner classes, local inner classes, anonymous inner classes

Members of the inner class

In another class java allows to define in a class. E.g

public class Car{
    public class Engine{ } }

Engine above example defines a class in the class Car, it is external Car class, and the class is the internal Engine.

Note inner classes: class comprising external inner class, the internal class can be seen that all properties and methods outside the class, including private method. But not vice versa;

There are two ways to use internal category:

  • Use internal class members in the outer class (indirectly). This method is generally created inside the object class in the method outside of class, and call the object's methods
  • Direct use: according to the above definition, can be used as such `Car.Engine eng = new Car () new Engine ().

Partial inner class

Internal class defined as members not only directly inside the outer class-based, can also be defined in the method, the partial inner class

Local inner classes, also known as embedded class area, similar to the local inner class with members of the inner class, but the class is defined regions embedded in a process of inner classes

The main specific are:

  • Partial internal class can only access the corresponding method, the method is not valid outside
  • You can not use private, protected, public modifier.
  • You can not contain static members
  • Local inner class if you want a local variable access methods, local variables must be constant. Because the distribution in the stack when the local variable class is assigned locally in the interior of the heap, there may be such a situation, the method of execution over the external class, the memory is recovered, it may still partial inner classes, so access when a local variable, to make a copy of the local variable are copied to the partial stack located inside the class. To ensure data integrity, the variables are copied here do not allow modification.
public class carShow(){
    public void showCar(){ final float price = 10000000f; final String type = "奔驰"; class Car(){ public void show(){ System.out.println("这个车是" + type + ",售价:" + price); } } } }

Static inner classes

If you use a static inner class declaration, this inner class is called static inner classes. It can be by  外部类 . 内部类 way of access. Because the static inner classes are not related to the object, when using the static class member is not required to create the object. So if you want a static inner classes to access the member variables outside of class, you must be accessed through an object instance of the outer class.

public class Company {
    String companyNam;
    static String country; static class Clear{ String name; public Clear() { } public Clear(String name) { super(); this.name = name; } public void work(String name){ String na = new Company().companyNam="联想"; country="中国"; System.out.println(name+"为"+na+"打扫卫生,该公司属于"+country); } } }

Anonymous inner classes

If an inner class used only once throughout the operation, then, it can be defined as the anonymous inner classes. Anonymous inner classes is no internal class name, which is a mechanism for the convenience of our java programming and design, because sometimes some internal only need to create a class of objects it can be, and will not be used again after this class , this time using an anonymous inner classes is more appropriate.

Anonymous inner classes, generally accompanied with such interface

public interface USB{
    public abstract void open(); public abstract void close(); } public class Demo{ public static void main(String[] args){ USB usb = new USB(){ public void open(){} public void close(){} } usb.open(); usb.close(); //使用匿名内部类的匿名对象的方式 USB usb = new USB(){ public void open(){} public void close(){} }.open(); } }

Guess you like

Origin www.cnblogs.com/sy130908/p/11433015.html