Java Basics - Object Oriented (on)

First, the object-oriented concepts

1. What is object-oriented?

  (1) it is an object-oriented programming ideas in line with human thinking habits.

  (2) object-oriented way of thinking is a way of thinking problems.

2. three characteristics:

  (1) encapsulation

  (2) inheritance

  (3) polymorphism

3. Establish an object-oriented way of thinking:

  (1) First integers, then the local

  (2) to the abstract, more specific

  (3) What to do, then how do

4. How to learn object-oriented?

  (1) master an object-oriented syntax

  (2) be familiar with object-oriented design principles

  (3) be familiar with object-oriented design patterns

Second, with the object class

1. The class definition:

  (1) class is a classification category

  (2) By classification, we can distinguish between different kinds of things, in everyday life, we often do so

  (3) Therefore, the class is a group having the same properties (attributes) and a set of food behavior (method).

2. Definition of object

  A real class in order to operate, and must rely on the object, the object is defined in the following format:

= New name of the object class name of the class name ();

2. relationship with the object of class

  (1) represents a common class of product as an integrated feature, and the object, is a product of the individual, the individual is a feature.

  (2) composed of a class attributes and methods:

    Properties: equivalent to one feature

    Methods: the equivalent of one person's behavior, such as: talking, eating, singing, sleeping

3. The format definition of classes and objects

You can use the following statement to define a class in Java:

{class class name 
        attribute name; 
        return type method name () {} 
}

Object definition format:

= New name of the object class name of the class name ();

If you want to access the class property or method (method definition),

It can rely on the following syntax:

Access class attributes:

Object Properties;

Call the class method:

Object Method ();

4. Summary of the object class

(1) new keyword: application memory space to express, also said instantiate an object, create an object.

(2) a size of the object in memory, the sum of all the properties of the object occupied by memory size. Reference type variable is four bytes in the 32-bit system, 8 bytes on the 64-bit system. Plus stealth target data out of proportion size.

(3) of the same type can only be assigned.

(4) different reference point to the same object, a reference to any change in value of an object, other references will be reflected.

(5) problems with the programming should be noted, is not used in determining the object, the object to be released as soon as possible: null reference

(6) When a heap object is not pointed to any reference variable, the object is considered to be the JVM GC procedure garbage objects so as to be recovered.

Third, encapsulation

1. The concept of encapsulation

  (1) encapsulation of one of the three characteristics of object-oriented.

  (2) the package is to hide the implementation details, only provide external access interface.

    Enclosing: Package Package encapsulation properties, methods, class packaging, assembly, modular package, system-in-package ...

2. The package benefits

(1) Modular

(2) Information Hiding

(3) code reuse

(4) of the plug-ins easy to debug

(5) with security

3. Package disadvantage

  It will affect the efficiency

Fourth, member variables and local variables

1. Different positions in the class

  Member variables: definitions in the class

  Parameters defined in the method or methods: local variables

2. Different positions in memory

  Member variables: the heap (member variables belong to the object, the object into the heap memory)

  Local variables: a memory stack (local variable belongs method, into the stack memory method)

3. different life cycle

  Member variables: With the creation of the object exists, with the destruction of the object disappear

  Local variables: With call a method exists, along with the method call is completed disappear

4. Initialize different values

  Member variable: there is a default initialization value, default null reference type

  Local variables: no default initialization values, you must define, assign, and then you can use

note:

  Local variables and member variables can name the same name, when used in the method, using the principle of proximity.

 

Fifth, the constructor

1. What is the constructor:

  (1) when the constructor method call is the class of objects configured for object initialization operation

  (2) method is a constructor object instance of a class, that is, when the new method, called first.

2. The method defined configuration:

  Constructor is defined format, the constructor defined in the class: class name and method name of the same, non-return type declaration.

3. instantiated object syntax:

Dog dog = new Dog (); // new Dog has a rear bracket, parentheses represents a method call, the method is called at this point the constructor

4. constructor overloads

Constructor with no arguments:

public Dog () {}
 // constructor that takes one parameter: 
public Dog (String name) {
     the this .name = name; 
}

The method of construction with a plurality of parameters:

public Dog(String name,int age){
    this.name =name;
    this.age = age;
}

The constructor Summary

(1) is configured the same as the class name and method name, no return value declared (including void)

(2) A method for initializing a data structure (properties)

(3) in each class will have a default constructor with no arguments

(4) If there is a class constructor is displaying default constructor method invalid

(5) The method may be configured with a plurality, but not the same parameters, the method becomes overloaded constructor

(6) In the constructor call another construction method using this. (), Which must be of a sentence code

(7) between the constructor call, there must be exported

(8) the data may be used to initialize the object constructor or setter method, usually, both are retained

(9) A good programming practice is to keep the default constructor. (For convenient framework code to create objects using reflection)

(10) private Dog () {}, constructor privatization, when we need to ensure that only one object class.

  When a class need only one object? For example, tools (no attributes of the class, only behavior) and the tool is frequently used objects. Generating a plurality of memory using only weigh objects with an object, to determine whether the class to be defined as only one object.

 

Guess you like

Origin www.cnblogs.com/s1023/p/11221396.html