02. OO and encapsulation

First, the object-oriented thinking

  • Outline
    • Java is an object-oriented programming language, an object here refers to the reality of all things, every thing all have their own attributes and behavior .
    • Three characteristics of object-oriented
      • Encapsulation, inheritance and polymorphism.
  • Classes and Objects
    • class
      • Is a group of related properties and behavior of the collection. It can be seen as a template for a class of things.  
        • Properties: status of things.
        • Behavior: what things can do.
    • Objects
      • Is a concrete manifestation of a class of things. Object is an instance of a class, it must have the properties and behavior of such things.
    • The relationship between classes and objects
      • Class is a description of a class of things, is abstract.
      • Objects are instances of a class of things, it is specific.
      • Class is a template object, the object is a class of entities.
  • Class definition
    • The real world of a class of things
      • Properties: status of things.
      • Behavior: what things can do.
    • Java class definitions
      • Member variables: the corresponding properties of things.
        • And previously defined variables is almost the same. But the location has changed. Outside the class, method.
      • Member method: the corresponding behavior of things.
        • And previously defined method is almost the same. Just get rid of the static, static in action again later explain in detail in an object-oriented curriculum.
  • Use objects
    • Typically, a class can not be used directly, we need to create an object based on the class to use.
    • Guide package
      • That is the class that you want to use, in what position.
        • import package name class name.;
      • For the case where the current category belongs to the same packet, the packet may be omitted guide statements are not written.
    • Creating format
      • Name = new class name of the object class name ();
    • Use: two cases
      • Use member variables: Object-member variable name.
      • Use member method: Object members of the method name (parameter).
    • Precautions
      • If the member variable is not assignment, then there will be a default value, and an array of different rules.
  • Object calls memory map
    • Three object calls memory map. ,

      

 

 

      

        

           

 

 

  • The difference between the local variables and member variables
    • Defined position is not the same [focus]
      • Local variables: internal in the method.
      • Member variables: the external method of directly writing them in class.
    • Scope is not the same [focus]
      • Local variables: The only way which can only be used, the method can not be reused.
      • Member variables: the entire class can all be common.
    • The default value is not the same as [focus]
      • Local variables: There is no default value, if it is to use, you must manually assign.
      • Member variables: If there is no assignment, there will be defaults, rules, and the same array.
    • Not the same memory location (understand)
      • Local variables: Located stack memory.
      • Member variables: Located heap memory.
    • Life cycle is not the same (to know) 
      • Local variables: With the push method was born, with the method of the stack disappear.
      • Member variables: With the object creation and birth, as the object is garbage disappear.

Second, the package

  • Outline
    • Object-oriented programming language is a simulation of the objective world, the objective world, member variables are hidden inside the object, the outside world can not directly manipulate and modify. Package may be considered as a protective barrier against the class codes and other types of data are accessed randomly. To access this kind of data, you must specify the way through. Appropriate encapsulation makes the code easier to understand and maintain, and enhance the security of the code.
  • in principle
    • The property hide it, if you need access to a property, provide public methods to access it.
  • Encapsulating step
    • Use keywords to modify private member variables.
    • Member variables need to access, provides a pair of getXxx method, setXxx corresponding method.
  • private keyword
    • private modifier is a privilege, on behalf of least privilege.
    • Member variables and member methods can be modified.
    • The member variables and member methods after private modification, only be accessed in this class.
  • this keyword
    • This leads
      • When the class member variables and local variables of the same name of the method, in accordance with "the principle of proximity" preferentially use local variables. If you need to access the member variables of this class which is required to use the format:. This member variable name.
      • References the current object of this class represents the location (address value) that their object references.
      • Which method is called an object, in this method represents that object. That is who to call, who this represents.
    • Construction method
      • Constructor method is designed to create an object, when we create an object by keyword new, in fact, call the constructor method.
      • format
        • public class name (parameter type parameter name) {
        •          The method body 
        • }
    • Precautions
      • And the name of the constructor must be exactly the same as the name of the class, and even the case should be the same.
      • Do not write constructor return type, not even void write.
      • Constructor does not return a specific return value.
      • If you do not write any constructors, the compiler will default presented a constructor, with no parameters, the method body does nothing at all.
      • Once you've written at least a constructor, the compiler will no longer presented.
      • Construction method also can be overloaded.
        • Overloaded: The same method name but different parameter list.
  • JavaBean
    • JavaBean is a Java standard specification written in a language class. JavaBean meet class requirements and specific class must be public, and constructor with no arguments, to provide a method for operating a set and get member variable.
    • A standard class typically has the following four components
      • All member variables must be modified using the private key.
      • Write a pair of children Getter / Setter methods for each member variable.
      • Write a constructor with no arguments.
      • Write a full-constructor parameters.

Third, the end of the package and the object-oriented

 

      

Guess you like

Origin www.cnblogs.com/su920602/p/12447158.html