Understanding of objects and references and inner classes in object facing programming in Java

  • 2020-04-01 04:36:59
  • OfStack

Recently, I took advantage of the time I looked at the think in Java feeling after work
Let's move on to the relationship between objects and references in Java and the concept of inner classes.
1. Everything in Java is an object
  What is it to manipulate objects in Java? The answer is a reference, which is like a pointer in C or C++.
If you have a reference, then you must associate it with an object, otherwise the reference will not be in your control as you think, for example, you create a reference to a String:


String s ;

At this point, there is no object associated with, if you do some operations, such as calling some methods of String, there is bound to be a problem, (except some basic types, because when you define them, they will be assigned to the initial value), so in the use of money must be associated with the object:


String s = new String();

or


String s =  " my name is ajun "; 

Just like that.
2. How to associate with objects
In Java, new is usually used to create an object to associate with a reference, such as:


String s = new String("my name is ajun");

This not only creates an object to associate with reference s, but also initializes it, and we can also create our own object type.
3. Storage location
(1) stack: general storage reference and basic type variables, the stack is mainly through the stack pointer up and down to allocate and release memory.
Primitive type variables are not suitable for new because they take up less memory.
Heap (2) : used to store a Java object, when the program execution new pile will be allocated a room for this object, remember heap memory allocation and release is more than the stack for storage and release the memory consumption of time, this is the basic types of variables to exist on the stack, because the basic types of variables is the most frequent, with frequent memory storage and release, while consuming more predictable performance.
4. Inner class
(1) internal basic knowledge:
Classes that are generally defined within Java classes are called inner classes
Inner classes can be divided into classes defined outside the method body, classes defined inside the method, static inner classes (which can only be defined outside the method), and anonymous inner classes
Description:
Class defined outside the method:
The member variables of the class (static and non-static) can be accessed. In order to ensure that the member variables of the class can be correctly referenced, the object of the outer class must be instantiated before the object of the inner class can be instantiated
Access can be any, you can think of it as a member variable of the class, so it's a lot easier to understand.
A class defined in a method body;
The member variables of the class (static and non-static) can be accessed. In order to ensure that the member variables of the class can be correctly referenced, the object of the outer class must be instantiated before the object of the inner class can be instantiated
Instead of having access rights, think of it as a local variable of a method.
Static inner class:
Only static member variables of a class can be accessed
Access any
Anonymous inner class:
The member variables of the class (static and non-static) can be accessed. In order to ensure that the member variables of the class can be correctly referenced, the object of the outer class must be instantiated before the object of the inner class can be instantiated
Access cannot be granted
(2) the role of inner class
Inner classes hide classes well, and private protect default access is not allowed for general classes.
Inner classes can implement multiple inheritance, which makes up for the fact that Java cannot have multiple inheritance
(3) examples


package com.ajun.test.innerclass.example; 
 
 
public interface Contents { 
   String value(); 
} 

package com.ajun.test.innerclass.example; 
 
 
public interface Destination { 
 
  //destination
  String readLabel(); 
} 

package com.ajun.test.innerclass.example; 
 
public class Goods { 
 
  private String des="is ruit!!"; 
   
  //Methods external
  private class Content implements Contents{ 
    private String name = "apple "+des; 
    @Override 
    public String value() { 
      return name; 
    } 
  } 
   
  //Methods external
  private class GDestination implements Destination{ 
    private String label ; 
    private GDestination(String label){ 
      this.label= label; 
    } 
    @Override 
    public String readLabel() { 
      return label; 
    } 
  } 
   
   
  //Anonymous inner class
  public Destination getdestination(final String label){ 
    return new Destination(){ 
      @Override 
      public String readLabel() { 
        return label; 
      } 
    }; 
  } 
   
  public Destination dest(String s){ 
    return new GDestination(s); 
  } 
   
  public Contents content(){ 
    return new Content(); 
  } 
   
  public Destination dest2(String s){ 
    class GDestination implements Destination{ 
        private String label; 
        private GDestination(String label){ 
          this.label= label; 
        } 
        @Override 
        public String readLabel() { 
          return label; 
        } 
    } 
    return new GDestination(s); 
  } 
   
} 

package com.ajun.test.innerclass.example; 
 
public class Test { 
 
  public static void main(String [] a){ 
    Goods gs = new Goods(); 
    Contents c = gs.content(); 
    Destination d = gs.dest("Beijing"); 
    System.out.println(c.value()); 
    System.out.println(d.readLabel()); 
    Destination d1 = gs.getdestination("Shanghai"); 
    System.out.println(d1.readLabel()); 
    System.out.println(gs.dest2("Tianjin").readLabel()); 
  } 
} 

Content and Gdestination are well hidden, and when called outside, you don't know which class is being called, which makes the class have the property of multi-inheritance.


Output;


apple is ruit!! 
Beijing 
Shanghai 
Tianjin 


Related articles: