On the base of Java object-oriented knowledge point

Source: Internet
Author: User
Tags object object

1. Relationship of classes and objects

A class is an abstract template that is created from a template, and objects can be instantiated in a class only after the class is established. For example: I want to cast a brick with gold, I will be in a model, so as to have the shape of the brick, the model is the class, pouring good bricks is the object.

2. Constructors (constructor methods) and member methods

Constructor is also called the constructor method, it is said that the reason is called the constructor is to distinguish the ordinary member method, the difference between the constructor and the ordinary method is;

A. The constructor does not need to add a return value type before the method name, but the member method needs to add a return value type before the method name, and "Void" is also one of the return value types.

B. The method name of the constructor needs to be consistent with the class name, and the normal method is arbitrary.

C. The constructor contains the parameterless constructor and the parameter constructor, where no parameter constructor is added by default if the developer does not write it, but it is not displayed, and the member method does not.

D. Syntax for constructor: public class name (formal argument list) {} syntax for member method: Public return value type method name (return value argument list) {return value}

E. The purpose of the constructor is to assign the initial value to the member variable in the main class, and only this function.

3.this keywords and Super keywords

Consider such a question, why do we need these two keywords?

The reason for this requirement: to represent this class object in a class, and then to resolve the call conflict problem for member variables (also called global variables) and local variables, to improve code readability, this is responsible for calling member variables in the syntax format: this. Member variable name

: Calling member methods in a class can be omitted in the call member method, so it is not explicitly written out in general programming, if you want to write the syntax for this. method name (); Note that this modifier must be written in the first line within the member method.

: A constructor is called in a class where the scope of this call constructor is to call another constructor of this class within one constructor, which cannot be called from each other and is not possible to invoke and be called within a normal member method. It is important to note that a statement that uses the this call construction method must be placed in the first row within the constructor method and can occur only once.

Super needs reason: I use a small example to describe this point, if there is a method in the parent class, this time we need to rewrite the parent class A method in the subclass, after overriding the A method in the subclass, we call him the B method in the subclass, this time we know The B method only shows where we have changed in the B method after rewriting the A method, not the contents of the previous a method, but sometimes we want to show the content of the A method in the subclass, when Super was born to refer to the object on behalf of the parent class.

: Super can modify member variables, member methods, and construction methods of the parent class, with the following syntax: Super. Member variable, super. Member method ("Parameter 1, Parameter 2"), Super ("Parameter 1, Parameter 2: 】)

: Note that the code that super calls the parent class constructor must be in the first row of the constructor and can occur only once.

4. Encapsulation

Encapsulation relative to the above content, better understand, we think about why we need to encapsulate, how to package, to whom?

Who is it packaged for? :

The object we encapsulate is "class", which is to encapsulate the smallest unit in a Java project.

Why do I need encapsulation? :

No matter any program is designed by humans, so the code embodies a variety of thinking is close to the reality, for example: We in the life of age and height and other values will not become negative, this is called illegal assignment, and then, our home is private, belongs to the personal space, not anyone can come in, So we give our private space building four walls, leaving an outside access door, which is called security. so the purpose of encapsulation is to control the assignment of variables to the problem of illegal assignment, and then encapsulate the class to improve security.

How to package? :

When you define a class, you define the properties of the class, such as the gender and the name of the person in this class. Then using a keyword private to modify these attributes, then the use of this keyword encapsulation is also a disadvantage, that is, can only be called in this class, how to use other classes? In order for the outside world to access our private properties, we need to provide some public-modified publicly available methods, which are used to get the GetXXX () method of the property value and the Setxxx () method that alters the property value.

5.Static keywords

See the word, static (static), the main reason for the generation of sub-keywords is, for example: 10,000 people in a school, we do not want to give everyone's memory space to add the school name, but want to 10,000 people common this name, so, with static modified member variable in memory space only one copy, is not created repeatedly.

Static can be modified

1. The static variable syntax for the member variable after static modification is: static data type variable name; Static variables can be shared by all instances in the class. Access is in the form of a class name. Variable name

2. The member method is static modified after the method syntax is: Permission modifier static return value type method name () {} static method invocation There are two ways to instantiate an object object name using 1. Method Name Call 2, class name. Method Name. The second one is more recommended, because the second can omit the instantiation of the object and improve efficiency.

3. code block static modified to static code block syntax is static {method body}, where static is loaded as the class is loaded, and the class is loaded only once, and then only once.

4. After the member inner class static is modified, the static inner class syntax is statically class name {}. Only static members of external classes can be accessed in static inner classes, and static members are defined in static inner classes. The following questions about the internal and external classes will be studied in more detail for this module.

Add: Static cannot coexist with this, super in a method, static cannot modify the construction method


6.final keywords

Final has the ultimate, immutable meaning.

Final can modify variables (member variables, local variables), modified variables can only be initialized, cannot be assigned again, Syntax: Final data type variable name = value;

Final can modify the method, the modified method can be inherited but cannot be overridden, syntax: access permission modifier final return value type method name () {}

Final can modify a class, the decorated class cannot be inherited, and after inheriting it means that it will be incorporated into the attributes of the subclass, which contradicts the final immutable and ultimate meaning. Syntax Format: Final class class name {}


7. Abstract class

Abstract classes are produced because of abstract methods, why do we need abstract methods?  Example: A wealthy businessman's child wants to eat a variety of dishes, but is not sure what he wants to eat, and this time the wealthy businessmen for the child invited a chef, mainly for the children to provide services, leisure time also for the rest of the family members. Then the chef is abstract, and the child is an abstract method, because abstract methods must exist in the abstract class, children want to eat food must rely on the chef. So the two of them are linked together because of the food, so the dish is a modifier, abstract, the keyword can only be modified after the class and method modified into abstract classes, abstract methods, can not modify variables and construction methods.

Characteristics: Abstract method, abstract method is no method body, is like the above example is not sure what to eat, Syntax: Abstract return value type method name (), abstract method must exist in the abstract class, there is an abstract method exists in the class, the class must declare For abstract classes, ordinary methods can exist in abstract classes, and abstract methods are required to be optional.

Abstract class Abstract classes cannot be instantiated, because abstract methods can exist in abstract classes, so abstract methods have no method body and cannot be called. However, an abstract class can be inherited with the syntax: Abstract class class name () {}

Note: A subclass inherits a parent class, and if the parent class is an abstract method, the subclass is implemented (the implementation is the content of the interface, which can be understood here as overriding) all the abstract methods of the parent class, or the subclass becomes an abstract class.

: Members of an abstract class can include: member variables, constructor methods, methods

The above is a random collation of 7 knowledge points.

On the base of Java object-oriented knowledge point

Related Article

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.