Home>

Recently, I thought about using think in java when I was off work. It feels very different from the first time.

Next, talk about the relationship between objects and references in Java.And the concept of inner classes.

Everything in Java is an objectWhat is the operator object in java?The answer is a quote,This is like a pointer in c or c++.

If you use a reference,Then you have to associate it with an object,Otherwise the reference will not be left under your control as you think,For example, you create a reference to a string:

string s;

While not associated with any objects at this time,If you do something at this time,Such as calling some methods of string,There must be problems,(Except for some basic types,Because when you define,They will be given initial values), so the money must be associated with the object in use:

string s=new string ();

or

string s="my name is ajun";

Just like this.

2, how to associate with the objectIn java, an object is usually created by new,To associate with references,Such as:

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

This not only creates an object and reference s to associate,Colleagues also initialized,We can also create our own object types.

3.Storage location(1) Stack:generally stores references and basic type variables,The stack mainly allocates and releases memory by moving the stack pointer up and down.

Basic type variables are not suitable for new creation.Because it takes up less memory.

(2) Heap:It is used to store java objects. When the program executes new, the heap will allocate a space for this object.Remember that heap allocations and deallocations take more time than stacks store and deallocate memory,This is the basic type variable to be stored on the stack,Because primitive type variables are used most often,Frequent storage and release of memory,When consuming more,Performance is conceivable.

4, inner class(1), basic knowledge of internal classes:

Classes generally defined inside java classes become inner classes

Inner classes can be divided into:classes defined outside the method body, classes defined inside the method, static inner classes (only defined outside the method), anonymous inner classes

Explanation:

The class defined outside the method:

Class member variables (static, non-static) are accessible,In order to ensure that the member variables of the class can be correctly referenced,So you must first instantiate the object of the outer class,Before you can instantiate objects of the inner class

Access can be anything,Think of it as a member variable of a class,This understanding is much better.

The class defined in the method body;

Class member variables (static, non-static) are accessible,In order to ensure that the member variables of the class can be correctly referenced,So you must first instantiate the object of the outer class,Before you can instantiate objects of the inner class

Access rights are not allowed,Think of it as a local variable of the method.

Static inner class:

Can only access static member variables of a class

Access any

Anonymous inner class:

Class member variables (static, non-static) are accessible,In order to ensure that the member variables of the class can be correctly referenced,So you must first instantiate the object of the outer class,Before you can instantiate objects of the inner class

Access rights cannot have

(2) The role of inner classes

The inner class can hide the class well,General classes are not allowed to have private protect default access.

Inner classes can implement multiple inheritance,Make up for the characteristics that java cannot inherit more

(3) Examples

package com.ajun.test.innerclass.example;
/**
 * Fruit content
 * @author administrator
 *
 * /
public interface contents {
   string value ();
}
package com.ajun.test.innerclass.example;
/**
 * Fruit destination
 * @author administrator
 *
 * /
public interface destination {
  //destination
  string readlabel ();
}
package com.ajun.test.innerclass.example;
public class goods {
  private string des="is ruit !!";
  //method outside
  private class content implements contents {
    private string name="apple" + des;
    @override
    public string value () {
      return name;
    }
  }
  //method outside
  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 ());
  }
}

Where content and gdestination are well hidden,When calling outside,I don't know which class is called at all,Make this class have multiple inheritance features.

Output;

apple is ruit !!
beijing
shanghai
tianjin
  • Previous Android programming method for adding menus to applications
  • Next Collect daily C # interface knowledge (comprehensive knowledge)