"Java Basics" object-oriented (on) - Notes

Classes and Objects

1, can be defined in the class member variable and methods
             - features described object (property) - member variables
             - member method - described the behavior of objects (methods)
2, in java,
          variable defined in the class is referred to as member variables
          define the variables in the process is referred to as local variables
3, using new to create objects:
                      format: class name = new class name object name ();
             after creating the class name of an object, can be accessed through the object reference All members of the target
                       format: object reference object members.  
4, the so-called class package refers to the definition of a class, the class of property privatization, namely to modify using the private key,
            private property can only category in which it It is accessed.
      In order to allow outside access to the private property, the property value can be obtained getXxx () setXxx method and set the property value () method

Construction method

Specifically:
        - the same as the class name and method name
        - no return value type declared in front of the method name
        - return statement can not be used in the method returns a value
        
overloads constructor
      - constructor can be defined in a plurality of class, as long as the number of parameters or different parameters for each type constructor can
      - when you create an object, can be assigned different attributes different construction methods
      - in order to facilitate instantiating the object constructor is usually decorated with a public

this keyword

Common methods:
       1, can definitely go to a class member variable access through this key to solve the conflict with local variable name
       2, call the method by members of this keyword
       3, the constructor is when the object is instantiated virtual java called automatically, not like other method calls in a program
                   like to call the constructor, a constructor may be used in the process, "the this ([parameter 1, parameter 2 ····])"
                        form to call other configurations method.

When using this constructor calls the class, you should note the following
        1, can only use this call other constructor in the constructor can not be used in the member method.
        2, the construction method, call the constructor of this statement must be in the first line, and only once.
        3, this can not be used in a two class constructor call each other.

Garbage Collection

    - but wait java virtual machine for automatic garbage collection, may be notified by a call to the java virtual System.gc () method of
           machine immediately garbage collection
    - within the class defined by the observed finalize () method when the object was released
    -java Virtual garbage collection operation machine is done in the background, after the end of the program, garbage collection operation has ended

The static keyword

In one class java, static - the static variable
                             access form: class name variable name.
In a java classes, static - the static method
                             to access the form: . Class name method name
in a java classes, static - the static code block
          Since the class is loaded only once, because the static code block is executed only once

Static keyword modified member variable that is referred to as static variables
before the method defined in the class can be combined with the static keyword, we call this method is a static method of
the modified code blocks referred to with the static keyword static code block

Note:
      1, static keyword can only be used to modify the member variables, local variables can not be used to modify, otherwise the compiler will complain
      2, in a static method can only be accessed with a modified static member, because of not being modified into a static
                members need to create objects to access, and how static methods can not create an object when it is called
      3, static methods do not need to create an object you can call

Single class implements a singleton pattern, which has the following features:
           - the use of the class constructor private modifier, declared as private, so can not be used to create an instance of the new class keywords outside.
           - inside the class creates an instance of an object class, and static variable INSTANCE reference to the right, due to the variable should prohibit direct access to the outside world,
                   and therefore the use of private modification, is declared as private members
           - in order to allow access to the outer class instance of an object, need to define a static method getInstance (), to return this instance iNSTANCE. A
                  is in the static method, the outside world can " class name. Method name to access" approach.

 Inner classes

Members of the inner class
inside the class can be used outside of the class, and access to members outside of class
if you want to create an internal class object, create an internal class object to access the inner class via an external class, you need an external class object specific syntax :
          external class name inside the class name external variable name = new class name () .new inner class name ();
inner class Inner use the static keyword to modify, is a static inner class

Note:
      If the internal class is declared private, it will not be able to access the outside world

Static inner classes
can be modified a member of the inner class using the static keyword, the inner class is called a static inner classes
to create a static inner class object specific syntax:
          External class name name variable name = new class name inside the class outside inside. class name ();
Note:
     in static inner classes can only access static members outside of class
     may define static members in a static inner class, but does not allow the definition of a static member in a non-static inner classes.
         

Method inner class
method refers inner class members in the class defined in the method, it can only be used in the present method,
      the method can also access the internal member variable classes outside the class

 

Published 32 original articles · won praise 11 · views 6186

Guess you like

Origin blog.csdn.net/Cai1010110/article/details/104025176