Java Fundamentals 7-Object Oriented 2

Source: Internet
Author: User

One. this keyword

Refers to the current object's reference, most of which is used to solve the problem of the name of the passed parameter and member variable;

For example, a SetName method is defined in a class that assigns the parameter value of a method to a member variable in the class:

public void SetName (String name) {

THIS.name = name;

}

This refers to invoking the other constructor methods in the current class, but when using the this method in the parameterless constructor method, this is the first sentence to be written in the non-parametric method.

Two. Static keyword

When two classes share a variable, the static modifier is used, and static is represented statically, and can be decorated with variables, constants, methods, and classes.

Format: Class name. Static Class Member

1. The variable or constant is stored in the data area, not in the stack or heap; no matter how many objects are instantiated, all objects share one copy, and static modified variables do not need to be instantiated at the time of access, and the class name is taken out directly.

2. When modifying the method, it is convenient and often used as a tool class because it does not need to be instantiated.

3. Static methods cannot access non-static members and cannot use the This keyword.

Three. Final keyword

The meaning is final, representing the constant

1. Modify the variable to become a constant, can only be assigned once, and must be assigned when defined, the variable name needs to be capitalized;

2. Cannot be overridden when modifying a method;

3. Cannot be inherited when modifying classes; String class comes with final;

Four. Inheritance of classes

Inheritance is a subclass that has a member of the parent class;

Format: modifier (optional parameter) class subclass name extends parent class name {

Class Body

}

Five. Inheritance rewriting (Override/overwrite): It embodies the ability of subclasses to live to change the methods of the parent class;

Overridden Condition:
1. There must be inheritance;
2. Method names are the same;
3. Parameter list (parameter type, parameter number consistent);
4. The return value type is the same;
5. Subclasses overriding the parent class cannot use more restrictive access than the parent class;

Java Fundamentals 7-Object Oriented 2

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.