Java Basic Learning _ object-oriented (top) 02_DAY07 Summary

Source: Internet
Author: User

=============================================================================
=============================================================================
The knowledge points involved are:
---------------------------------------
-----------------------------------------------------------------------------
=============================================================================
=============================================================================
1: Differences between member variables and local variables (understanding)
   (1) The position in the class is different:
Member variable: Outside the method in the class.
Local variables: In a method definition or on a method declaration (that is, a formal parameter).
   (2) The location in memory is different:
Member variable: in the heap.
Local variables: in the stack.
   (3) Different life cycle:
Member variables: As objects are created, they disappear as the object disappears.
Local variables: As the method is called, it disappears as the method's call is complete.
   (4) different initialization values:
Member Variable: There is a default value.
Local variables: There are no default values that must be defined, assigned, and then used.
   (5) Precautions:
The local variable name can be the same as the member variable name, but when using the variable in the method, the nearest principle is used (find a small range, then find a wide range).
-----------------------------------------------------------------------------
2: Class as a form parameter problem? (understanding)
   (0) The formal parameters of the previous study:
Basic data type (numeric, character, Boolean): The change of formal parameters does not affect the actual parameters.
Reference data types (classes, interfaces, arrays): Changes to formal parameters directly affect actual parameters.
   (1) If the data type of a method's formal parameter is a class (that is, the reference data type), the formal parameter here actually needs to be a specific object of the class.
as shown in the following:


-----------------------------------------------------------------------------
3: Anonymous Object (understanding)
(1) is an object without a name (a simplified representation of an object).
  (2) Application Scenarios for anonymous objects:
    A: The object calls the method only once.
Note: It is not appropriate to call multiple objects at a time.
So what is the benefit of this anonymous object invocation method?
Yes, the anonymous object call method is garbage after completion. Can be recycled by the garbage collector to improve memory usage efficiency.
    B: An anonymous object can be passed as an actual parameter.
As shown in 02:


-----------------------------------------------------------------------------
4: Encapsulation (understanding)
(1) refers to the properties of hidden objects and implementation details, only provide public access to external.
   (2) Benefits:
A: Hide the implementation details and provide a public access method.
B: Improves the reusability of the code.
C: Improves the security of the code.
   (3) Design principles
Hide the details of the implementation that you don't want to know about, hide the attributes, and provide a common way to access them.
   (4) Private is one embodiment of encapsulation.
     The package has these several:
(1) class
(2) method
(3) Private modifier member variable
-----------------------------------------------------------------------------
5:private Keywords (master)
(0) is a permission modifier.
(1) The meaning of the private, you can modify the member variables and member methods. (in general, private is used to decorate member variables, less to modify member methods.) )
   (2) Features:
Members that are modified by private can only be accessed in this class. To access it, it is going to provide public access to the outside.
  (3) Application of private:
To write a class later:
Give all the member variables to private.
provides the corresponding getxxx ()/setxxx () method.
-----------------------------------------------------------------------------
6:this Keywords (master)
(0) name = name;//assigns himself to himself, without any meaning. (Rules of use of variables: Nearest principle)
(1) Represents a reference object for the current class. Simply remember, it represents a specific object of the current class.
Remember: Which object invokes the method, and this within the method represents which object.
   (2) This application scenario:
A: Fixed a problem with the local variable hidden member variable.
B: In fact, this has other applications, and the other usage is explained later with super.
as shown in the 03/04:


-----------------------------------------------------------------------------

Java Basic Learning _ object-oriented (top) 02_DAY07 Summary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.