Java object-oriented knowledge

1. The process-oriented and object-oriented

  • Process-oriented: emphasis on the functional behavior as a function of the smallest unit 考虑怎么做.

  • Object-oriented: have emphasized the object function to the class / object minimum unit 考虑谁来做.

    面向过程
    把大象放进冰箱
    ① 把冰箱门打开
    ② 抬起大象,塞进冰箱
    ③ 把冰箱门关闭
    
    面向对象
    {
       打开(冰箱)
       {
     		冰箱.开开();
     	}
     	抬起(大象)
     	{
     		大象.进入(冰箱);
     	}
     	关闭(冰箱)
     	{
     		冰箱.闭合();
     	}
     
     }
     
     冰箱
     {
     	开开()
     	{
     	}
     	闭合()
     	{
     	
     	}
      }
      
     大象
     {
     		进入(冰箱)
     		{
    		}
      }
     
    

2. Classes and Objects

  • Categories: one describing things, is abstract, conceptual definitions

  • Objects: real subject exists, which is also known instance (instance)

    Focus on object-oriented programming is the class design
    design class, that is, members of the design class.

3. The use of the attribute class

Attributes (member variables) vs local variables

① the same point:

  • Format-defined variables: variable name = data type variable value
  • First statement after use
  • Variable has its corresponding scope

② different points

  • Different position in the class declaration of

    • Properties: one pair of directly defined within the class {}
    • Local variables: within a method, the method parameter, the code block, the constructor parameter, the internal configuration of the variables
  • Permissions on different modifier

    • Attributes: When can declare property, indicating its rights, usage rights modifier.

      Common permissions modifiers: private, public, default, protected ---> encapsulation

    • Local variables: You can not use Permissions modifier.

  • By default initialization value:

    • Properties: properties of the class, according to their type, have default initialization values.

      Integer (byte, short, int, long): 0

      Float (float, double): 0.0

      Char (char): 0 (or '\ u0000')

      Boolean (boolean): false

      Reference data types (classes, arrays, interfaces): null

    • Local variables: no default initialization values.
      Means that, before we call the local variables must be explicitly assigned.
      In particular: the parameter when you call, we can assign.
      public void add(int numA)

  • Loaded in memory location:

    • Properties: loaded into the heap space (non-static)
    • Local variables: Load the stack space

3. The creation and use of objects

Object class = class is created to instantiate instances of classes =

  • Creating object syntax: name = new class name of the object class name ();
  • "Object name Object members" of access object members (including properties and methods)

If more than one object of a class is created, each object has properties independent of a class. (Non-static)

Means: If we modify the properties of an object a, another object did not affect the value of a property.

Memory allocation map:

Statement 5. The method and use of the class

  • Method: class should be described with a function.
  • Method statement: Permission modifier return type method name (parameter list)

​ {

The method body

}
Note: static, final, abstract modified method,

  • Description:

    • About permissions modifiers: default method permission to use the public modifier

      • 4 kinds of privileges specified modifier Java: private, public, default, protected -> encapsulation and then elaborate
    • Return Value Type: return value vs no return value

      • If the method returns a value, it must be in the method declaration specifies the type of value returned. Meanwhile, the method requires the use of the return keyword to return the specified type of variable or constant: "return data."
      • If this method does not return value, the method statement, using a void to represent. Typically, there is no return value method, there is no need to use return, however, if used, can only "return;". Indicates the end of the meaning of this method.
      • We should not define the method returns a value?
        ① ② questions asked by experience: to analyze specific issues
    • Method name: identifier belongs to, follow the rules and norms identifiers, "see to know the name meaning"

    • Parameter list: A method can declare 0, 1, or zero or more parameters.

      • Format: Type 1 Data parameter 1, parameter 2 2 data type, ...
      • When we define methods, which should define the parameter?
        ① ② questions asked by experience: to analyze specific issues
    • Procedure: method embodying features.

  • return use keyword:

    • 1. Scope: The method used in the body
    • 2. Role: ① the end of the method
      ② for there are ways to return value type, use the "return data" method returns the data you want.
    • 3. Note the point: return keyword is not a statement to execute a statement.
  • Using the method, you can call a property or method of the current class
    special: A method in turn invokes a method A: recursive method.
    Method, the method can not be defined.

Guess you like

Origin www.cnblogs.com/zhenqk/p/12578694.html