Object-oriented third day, object-oriented

Source: Internet
Author: User

Object-oriented third day, object-oriented

1: Review

1: this keyword

1: this indicates the reference of the current object.

2: this accesses the member variables and member methods in this class. You can also access the constructor method, which must be placed in the first statement.

3: this can be passed as a real parameter.

2: Object-oriented features: encapsulation, inheritance, and polymorphism.

2.1: encapsulation: private properties, providing public methods for external access.

2: Inheritance

2.1: when analyzing some things, we find that they have many similarities. To improve code reusability, we can extract the same parts and put them into a class. Let other things establish a relationship with this class, and this relationship is called inheritance. It is represented by the keyword extends.Extends. (Subclass is also called a derived class)

2.2: Impact on sub-classes after an inheritance relationship is generated

1: subclass can have all attributes or methods of the parent class (access permission will be discussed later .)

2.3: super:

1: indicates the identifier of the parent class, that is, a name.

2: Role

1: You can access the super. member variable name of the parent class.

2: Method super for accessing the parent class. Method Name ()

3: Access the constructor of the parent class. When creating a subclass object, the non-argument constructor of the parent class is called by default, and then the corresponding constructor of the subclass is called. The constructor that calls the parent class must be the first statement in the constructor. To call a constructor with parameters of the parent class, you must call the constructor explicitly.

3: What is the difference between this and super?

1: function. this indicates the reference of the current object, and super indicates the ID of the parent class.

2: Access. This can access the member variables and methods of this class, while super can access the member variables and methods of the parent class.

3: similarities. When accessing the constructor method, it must be placed in the first statement of the constructor method. They will not meet each other. If you explicitly call your own constructor In the constructor of the subclass, the constructor of the parent class is not called. If it is not written, it is implicitly required to call the non-argument constructor of the parent class.

4: this can be used as a real parameter. But super cannot. (Why not?)

Super is only an identifier of the parent class. It has no substantive meaning and does not involve memory. This indicates the object reference, which can represent the object and point to the first address of the referenced object.

2.4: override of Method = overwrite of Method = rewrite of Method

Different people have different naming conventions. However, rewriting, overwriting, and rewriting all mean that methods and functions are the same.

1: When will the subclass method rewrite the parent class method? If the function of the method of the parent class cannot meet the function of the subclass, this function will be overwritten. It's not just code reuse!

2: What is method rewriting?

1: The method declaration is the same, and the method body is generally different.

Access permission return value type method name (parameter list ).

The access permissions can be different. Must comply with certain rules. When talking about access permissions later, we now think that the access permissions are the same. Override the method of the parent class in the subclass. The access permission must be greater than or equal to the access permission of the parent class.

The thrown exception is also required.

3: differences between method overloading and method Rewriting

The premise of rewriting is to inherit extends! Extend is the meaning of the extension, and such a name has a certain meaning, it is easier to understand with the intention of the name.

1: Method overloading occurs in the same class. Method rewriting occurs in the parent and child classes.

2: The method is overloaded with the same method name and different parameter lists. Method rewriting is the method name and parameter list. The return values must be of the same type, the method declaration must be the same, and the method body is different.

3: function. The method is rewritten to extend the function.

4: inheritance details

1: java only supports single inheritance and multi-level inheritance.

2: Why cannot I inherit more in java? It is prone to conflicts. If there is more inheritance, and the inherited parent class has the same method, which method should be called?

3: When will there be an inheritance relationship?

When there is a relationship between the class and the class, it is the premise of inheritance. A is one of B. A inherits B. A wolf is a type of dog. Therefore, to determine the ownership relationship, you can simply consider that if all the functions of the inherited class can be possessed by this subclass after inheritance, the inheritance will take effect. If not, it cannot be inherited.

4. We recommend that you create the most subclass object by using the parent method or subclass method.

3:

3.1: Package and access permission.

3.1.1: Package

The role of a package: To resolve class name conflicts and classify classes. Classes with similar effects are placed in the same package.

How to define a package: package name; the first statement (excluding comments) must be included in the file ).

Naming rules: all are in lower case. It is generally the reverse of the domain name.

Www.baidu.com ---> com. baidu. entity: After compilation, the com folder, baidu folder, and entity folder are automatically generated. The baidu folder is in com, and the entity is in baidu.

Import package: import package name. Class Name

3.1.2: access permission: private. Default value ). Protected: protected. Public: public.

Private: It can only be accessed in this class. The class cannot be modified.

Default: the class of the same package can be accessed.

Protected: the class of the same package can be accessed, and The subclass of different packages can be accessed. The class cannot be modified.

Public: All classes are accessible.

Private<Default<Protected<Public

3.2: static.: static

3.2.1: when the values of one member variable of all objects in a class are the same, you can set this member variable to static to save memory, this variable has only one space in the memory, no matter how many objects are created. It can also be said that the purpose of sharing is achieved. Since all objects share the data, the value changes once, and the value of all objects changes.

3.2.2: the access and loading sequence of static member variables.

Static member variables can be accessed using classes or objects. Generally, they are accessed using classes.

Static members are loaded along with the loading of classes, and objects may not exist at this time. It has the longest lifecycle in the memory.

Loading Sequence: Assign static member variables first, static method ------- assign non-static member variable space after the object is created ----- when non-static method is called, non-static method will be added to the stack.

3.2.3: static methods (load first) cannot access non-static members (load later), but non-static methods can access static members.

When a method does not access a member of the class or a non-static member of the class, you can define this method as a static method. To facilitate the call.

3.2.4: static code block: extension.

Execution sequence: static code block ------- construct code block ------ construction method.

Static code block: it is executed only once.

Construct code blocks and constructor Methods: execute a new code once.

What is the role of static code blocks? Initialize static members.

3.3: final

3.3.1: final: Indicates final.

3.3.2: The final modifier indicates that the subclass cannot override this method.

Final modifies the member variable, indicating that the value of this variable cannot be changed and must be explicitly initialized. Non-static final members can be initialized within the constructor code block or constructor. They can only be initialized in one of them. If they are initialized in the constructor, they can be assigned a value in each constructor! Static final members can be initialized in static code blocks. Since its value cannot be modified later, it is often used together with final and static to modify a constant. This constant indicates that it is used in the entire class or project, and does not modify the value. It only uses the value of this constant.

Final modifier class: indicates that this class is the final class and cannot be inherited.

3.4: abstract class.

3.6: Interface

3.7: Polymorphism

3.8: Design Mode: Singleton mode, simple factory mode.

3.9: internal class. Interface callback

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.