Object-oriented (ii): Common knowledge

Source: Internet
Author: User

First, member variables and local variables

1. Difference1) Different positions in the class
    • Member variables: Outside the method in the class
    • Local variables: In a method definition or on a method declaration
2) different locations in memory
    • Member variable: in heap memory
    • Local variables: In stack memory, as the method runs in the "stack"
3) different life cycle
    • Member variables: As objects are created, disappear as objects disappear (new)
    • Local variables: As the method is called, it disappears as the method's call is complete
4) different initialization values
    • Member variable: has default initialization value
    • Local variables: No default initialization values, must be defined, assigned, and then used
2. Precautions:
    • The local variable name can be the same as the member variable name, when used in the method, using the nearest principle.
    • The nearest principle is used in conjunction with the This keyword, which can be explicitly positioned to determine local variables, member variables, variables in inner classes, and so on.

Ii. problems with formal parameters: (Java value delivery)

1. Basic type: Changes in formal parameters do not affect actual parameters.

2. Reference type: Changes in formal parameters directly affect the actual parameters.

<span style= "FONT-FAMILY:ARIAL;FONT-SIZE:18PX;" ><span style= "font-family:arial;" >class Studentdemo {     ///If you see a method with a formal argument that is a class type (reference type), what you need here is the object of that class. Public     void Method (Student s) {//When called, the address of s in the main method is passed here Student s = new Student ();      S.show ();     }} </span></span>


Third, anonymous objects

<span style= "FONT-FAMILY:ARIAL;FONT-SIZE:18PX;" ><span style= "font-family:arial;" >a a = new A (); new A ();//anonymous </span></span>

Iv. Encapsulation and Private keywords

1, encapsulation: Refers to the hidden object's properties and implementation details, only provide public access to the external mode. 2, Private: is a permission modifier, you can modify the member variables and member methods, the members whose decoration can only be accessed in this class . 3, encapsulation and private application:
    • The member variable is modified with private and is accessed within the class.
    • provides the corresponding getxxx () and Setxxx () methods.

Five, this keyword

1, this: is the object reference of the current class.
    • Simply remember, it represents an object of the current class. Only objects can invoke methods.
    • Who calls this method, the this within the method represents who.
2. This scenario: Resolve local variable hidden member variable3, Memory diagram:



Vi. Construction Methods

1, the construction method notes:
    • If we do not give a construction method, the system will automatically provide a non-parametric construction method.
    • If we give a construction method, the system will no longer provide a default parameterless construction method.
    • Note: This time, if we still want to use the parameterless construction method, we have to give it ourselves. It is recommended that you always give a non-parametric construction method.
2. There are two ways to assign a value to a member variable: (Spring's IOC approach, in fact, is also an attribute input and construction method injection)
    • Setxxx ()
    • Construction method

Vii. what has been done to create the object




Eight, static keyword

1, the characteristics of static:
    • It can decorate member variables, decorate member methods, and also decorate members ' inner classes. The contents of the static decoration are called: class-related members.
    • Loaded as the class loads, precedence over the existence of the object, before the object is created before the construction method executes.
    • There is no this keyword in the static method. Static is loaded as the class loads, and this is present as the object is created.
    • Stored in the method area, shared by all objects of the class.
    • It is recommended to call with the class name.
    • Static methods can only access static member variables and static member methods. Simple to remember: static can only access static.
2, Example: Our class students should share the same class number.
    • If a member variable is shared by all objects, it should be defined as static.
    • Example: Drinking fountains (static modification), water cups (can not be modified by static)

3, Memory diagram:



Ix. Final Keywords

1, final can be modified class, method, variable2. Features:
    • Final can modify the class, which cannot be inherited.
    • Final can modify the method, and the method cannot be overridden. (Overwrite, duplicate)
    • Final can modify the variable, which cannot be re-assigned. Because this variable is actually a constant.
3. Interview questions: Final modification of local variables?
    • Base type: The value of the base type cannot be changed.
    • Reference type: The address value of the reference type cannot be changed, but the value of the heap memory of the object can be changed.
4. Initialization time of final modified variable
    • A final modified variable can only be assigned one time.
    • Assign a value (a non-static constant) before the construction method is complete, that is, it can only be assigned at the time of definition (recommended) and in the constructor method.
    • Static is assigned when the ClassLoader is loaded, that is, the final static definition.
5, inherited code embodiment: Because of the method of inheritance in a phenomenon: Method rewrite. So, the function of the parent class will be overridden by the quilt class. There are times when we don't want subclasses to overwrite the function of the parent class, only to let him use it. Use the final keyword.

Ten, polymorphic

1. Prerequisites1) have the inheritance or realization of the relationship. 2) to have a method rewrite.
    • Not really, but it doesn't make sense without it.
Animal d = new Cat ();
D.show ();
Animal d = new Dog ();
D.show ();3) There are parent or parent interface references to child class objects.
    • Parent F = new Child ();
2. Features of member access in polymorphism:1) member variables
    • Compile look left, run look left.
2) Construction method
    • When you create a subclass object, you access the constructor of the parent class and initialize the data for the parent class.
3) member method
    • Compile to see left, run to see right.
4) static method
    • Compile look left, run look left.
    • Static and class-dependent, not rewrite, so, access is still on the left.
    • Because the member method has a method override, it runs looking to the right.
3. Memory Diagram


4. Object Transformation1) The parent class accesses the function of the subclass:
    • You can call a method to create a subclass object. (yes, but a lot of the time is unreasonable.) Also, it takes up too much memory)
    • Cast a reference to the parent class to a reference to the subclass. (Downward transition)
 2) Issues of transformation between objects:
    • Upward transformation: Fu f = new Zi ();
      • In daily development, the use of interfaces is the most common
    • Downward transformation: Zi z = (Zi) F;
      • Requires that the F must be able to be converted to Zi, otherwise the classcastexception-type conversion exception is reported.
3) Memory diagram of object transformation


Object-oriented (ii): Common knowledge

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.