javaSE summary (b) - java object-oriented

A, classes and objects

  (1) Class

[Modifier] class class name {
     // modifier 1: private public protected three most frequent one
     @ Modifier 2: abstract final two most frequent one
     // above can be used in combination with a modified static class 
}

  - the first letter of each word capitalized, the rest lowercase letters
  - is not the best keywords, you can include the keyword


Associated with class constructor

[Modifier] constructor name (parameter list) 
{ 
    // modifier: Private public protected
     // constructor name: the name of the same class
     // parameter list: define the format and method of the same parameter list 
}

  - no return value Constructor

  (2) Objects

    a) Create Object

      Object class name name name = new constructor ();

    b) object calls 

      Call content object class (static not been modified)
        a call method: Method name object name (); ==> per.say ();.
        2 call attributes: object name attribute name; available assignment. Example: per.name = "test";
        . 3 the this keyword: this represents the object class, this method name (); the this property name;.

    c) the object of the memory mechanism

The p-= the Person new new   the Person (); After creating the object, the object is stored in the allocated memory, there are two memory. One is the stack memory: storing variables p, p just store the variable name of the object, it is stored in the heap memory; the other is the heap memory: storage entity object, all properties and methods are in the stack memory. 
Stack memory heap variable points to a corresponding entity object, p is equivalent to a reference variable.

  (3) static Static   

    1. Static static, the method may be modified, properties
    2. When a modified static member, then the members belong to the class itself, such as the use of the scheduling; static is not modified, the object belongs to that class, generally used object call (also available class calls).
    3. Summary: static call with the name of the class
         non-static objects by calling (class name can also be invoked)

Second, the method

Modified keyword [Return Type] method name ([Parameter Type Parameter Name]) {
        // SUMMARY 
        [ return Return value] 
}

note:

  - The method can not be independently defined, the method can only be defined in the class; method can not be performed independently, or through the object need to call the execution class; the same class, the same method can not appear two or more.
  - Method overloading: the same method name, different parameters.

 

Third, the member variables and local variables

  (1) member variables

    a) Examples of variables: No static modification

    b) class variable: There are in the form of static

  (2) local variable

    a) The method of local variables (as defined in method): From the definition of the variables to take effect, to failure is the method ends.

    b) the code block of local variables (defined in block): effect from the definition of the variable, to the end of the block failure.

    c)  parameter (variable defined method signature): signature method when defining, defined variables, the effective scope parameter in the overall process.

Note: 
    Local variables and member variables can be the same name as a local variable coverage of the value of a member variable. If you need to refer to member variables covered In this method, the use of this (for instance variables) or class name (for class variables) as the caller to restrict access to member variables.

Fourth, the object-oriented features

  (1) inheritance  extends

    Subclass inherits the parent class, subclass will have a non-private methods and properties of the parent class members

    a) method overrides (Override, also known as Cover Method)

Method override follow the " two small with a large two ." 
    1 "with the two" : methods, the same parameter list
    2 "two small" : Return Value Type subclass than the parent class or less the same; thrown range exception class is smaller than the parent class.
   3 "big": access to the subclass methods more than access to the parent class method or equivalent.
Note: The 
      parent class subclass covering after subclasses of objects can not access method in the parent class overridden by a subclass of the subclass method overrides the parent class can be called.

    b) super defined

Super (); call the parent class constructor can only be used in a subclass constructor.
Super . attribute; call the parent class property.
Super . Method (); call methods of the parent class.

    c) super attention

    1 in the sub-class constructor does not pass the super () to call the parent class constructor, the implicit default parent class calls a constructor with no arguments.
    2 subclass constructor only one call parent class constructor that normal parent class property or method may be invoked multiple times.

  (2) Packaging

    Access specifier
    Private (current access class) default (package access) protected (subclass access) public (public access)

  (3) Polymorphism

1 . The same type of variable, presenting calling a method with a variety of different behavioral characteristics, is polymorphic.
2 . It can only be called a reference variable compile-time type of method, but it can not be called runtime type of method.
3. difficult to understand, then, so remember. F = Father new new Son (); Son Example subclass variable assigned to the parent class Father, f variable contents can only call the parent class Father
instanceof operator: 
    object name instanceof   class, before determining whether the object is a class (subclass implementation class) instance. If returns true, otherwise false.

V. interfaces and abstract classes

  (1) Interface

a modified using the interface keyword class, which is called interface. 
b Interface: 1 . All methods are abstract (abstract can be omitted).
         2. The member variables used are static constants ( Final and static may be omitted). 
c interfaces can inherit an interface, the class can not be inherited, allowing multiple inheritance. A multi-class can implement multiple interfaces.
   

  (2) abstract class

Definition: abstract classes and abstract methods must be modified to use abstract. The method must abstract class is an abstract class, an abstract class is not an abstract method. 
An abstract class can not be instantiated. 
Abstract class as a template for subclass, subclass designed to avoid arbitrariness.

  Similarities and Differences (3) interfaces and abstract classes

1 . They are not be instantiated, and implementation inheritance to subclasses.
2 . An abstract class may contain abstract methods and the conventional method, the ordinary static member variables and constants; interface can define abstract and static constant.
3 interface is not configured; abstract class has a constructor, but not instantiated, a sub-class calls the initialization operation is completed.
4 interface can not include an initial block of code; abstract class may contain the original code blocks.
5. A class has a direct parent, including the most abstract class; but a class can implement multiple interfaces.

 

Six, packaging

  Description: The basic data types do not have the characteristics of the "object", no member variables, methods can be called. A method of packaging data processing exists, the data to facilitate a good operation. By autoloader / unboxing, ease the transition between the base and the type of packaging objects.

  类型:Byte Int Short Long Character Float Double Boolean String

  Related methods of packaging:

    (1) xxxValue (): Number object converted to the data type value and returns xxx. Parentheses are no parameters

o byteValue (); Returns the specified number as a byte. 
doubleValue in (); Returns the specified number as a double. 
o floatValue (); Returns the specified number as a float. 
intValue (); Returns the specified number as an int. 
longValue in (); Returns the specified number as a long. 
shortValue (); Returns the specified number as a short. 
Example: Integer X =. 5 ; 
x.doubleValue (); // returns the native data types double

  (2) compareTo (): number comparing object parameters.

X =. 5 Integer ;
 int NUM = that x.compareTo (. 3 ); 
if the specified parameter is equal to the number of returns 0. 
If the number is less than the specified parameters return -1 . 
If the number is greater than the specified parameter returns 1.

  If (3) equals () object is equal to the parameter number is determined.
  (4) valueOf () Returns a specified Number object built-in data types
  (5) toString (): returns the value as a string.

  (6) parseXXX (): parse a string of type XXX.

parseInt (): parse a string int. 
parseFloat (): parse a string type Float. 
parseDouble (): parse a string type double. 
parseShort (): Short to parse a string type. 
parseLong (): parse a string to Long.

  (7) random (): returns a random number between 0 and 1.

 

Seven, inner classes

  (1) Definition: A class defined within another class in, this is called an inner class in other classes inside the class (in some places, also known as nested classes).

  (2) the role of:

providing a better packaging, the interior of the external type hidden in the class, the class does not allow the same type of access to other package. 
b inner classes have direct access to private property as well as other members of the outer class, inner classes can also be seen as the other members of the inner class. But implementation details (such as member variables within the class) external class can not access the internal class. 
c class can use more internal than external modifiers three categories: Private , protected , static ----- outer class can not be used. 
d non-static inner classes can not have static members

  (3) internal static type / non-static inner class

    a) Static inner class

      Definition: The modified using static inner classes, called static inner classes. Static inner classes are external class itself.

         Static inner class can contain static members, but also non-static member. Static members can not access non-static members.

           Interface can define inner classes and inner classes can only be a static inner classes.

    b) non-static inner class

      1 external class can not directly access the internal class members. In addition to internal class outside of class, by creating an instance of the inner class, instance access to the internal class members through an internal class. (Using the format with the same general category)

      2 may be used as inner classes outside the class members. Within the class by the this, the external .this class name as defined class distinction.

        a.this Member Name: instance variable access non-static inner classes, this.inVarName
        b external class name .this Member Name: access to external class instance variables, OutClass.this.outVarName

        Note : Static members are not allowed in a non-static inner classes

  (4) inner classes

    a) inner classes outside 

      1 and usually use the general category is not much difference, create an instance of the inner class constructor calls within the class through new, calling members of the inner class with an example.
      2 Do not use static internal static class member outside the class (including static and static initial code blocks), the static member can not access the non-static member.

    b) a non-static inner classes outside of the outer class

. 1 .private internal modified classes, only in the outer class.
2 . Omitted access specifier inner classes, may be only the external class is used in the other classes in the same package.
. 3 .protected internal modified classes, may be only the outer class is a subclass of the class of other classes and external use in the same package.
4.public internal modified class, any place can be used.
Use non-static inner class basic syntax:
 1. Inner OuterClass.InnerClass = new new OuterClass was (). New new InnerClass (); creates an instance of an object class interior.
OuterClass was OUT = 2. new new Outer (); 
    OuterClass.InnerClass in = OUT. New new InnerClass (); The same can create an instance of an object class interior.
3. The non-static inner class constructor, you must use the outer class object to call.
When a class inherits a non-static inner classes in the subclass constructor calls the constructor of the parent class non-static inner classes, we need to pass the object outside the class to subclass constructor. Since non-static inner class constructor must be called outside the class instance object. As follows:
 public  class SubClass the extends Out.In {
     public SubClass (Out out) {
             // through out object and out, In constructor call. 
            OUT. Super ( "Hello" ); 
} 
}

    c) using a static inner classes outside of the outer class

      The basic syntax:
        OuterClass.InnerClass in = new new OuterClass.InnerClass ();
        Because static inner classes are related classes outside of class, so the object can not be created outside of class when creating a static inner class object.

  (5) Internal partial class (Method inner classes)    

    Definition: define a class into the method, then the class a partial inner classes (method internal), local inner class only within the process.
      1. A method anywhere outside are not members of a local call in the inner classes, only valid method
      2. Local inner classes can not use static modification.
      3. If a local variables inside the class definition, create an instance or subclass, the method can only be localized in the interior of which the class.

Eight members of the class and object processing

   (1) processed

. 1 toString (); when a print target by toString () method, the object details ( "self-description" ) print. Not used toString () method, the printed information is a hash value of an object.
== 2 and equal difference 
    when the determination of the basic data types, without distinction. Is the equal values are equal 
    when the reference data type is determined: == value and when the memory address (the same object) are equal, it is equal; only equal values are equal, i.e. equal to

  (2) Class Members

     Fields, methods, constructors, an initial block of code, an internal class (including interfaces, enumerations). = Constructors are available in addition to static modification

     Singleton class: A class instance can only create a

  (3) final qualifier

a) final modified variables must be assigned an initial value 
b) final modified variable, after the initial value exists, the value set can not be changed. 
    1 primitive values immutable.
    2 reference type, immutable reference object, the memory address is not changed, the contents of the address can be. The         
p.setName () can be changed in the content object. Not P = null ; this memory address is eliminated, is not feasible.
    3 subclasses may not override the parent class method final modification
     . 4 class modified final immutable class, it can not be inherited.

 

Guess you like

Origin www.cnblogs.com/FondWang/p/11457436.html