Programming Architecture (06): Java object-oriented

Source code of this article: GitHub·click here || GitEE·click here

1. Basic concepts

1. Object-oriented concept

The main idea of ​​object-oriented programming is to decompose each transaction that constitutes a problem into various objects. The purpose of establishing objects is not to complete a step, but to describe the behavior of a thing in the entire problem-solving step.

Programming Architecture (06): Java object-oriented

2. Classes and objects

Object: the only thing that really exists; in the application, the object is a combination of data and action, which can not only perform operations, but also record the results of operations.

Class: An abstract concept, which is actually the extraction of common attributes and behaviors of certain types of things; for example, the User[name.age.gender] class is used to describe the basic information of users.

How to understand object-oriented in Java development: build a suitable object interface API system, create suitable objects, and solve suitable problems. For example, JDK API has defined classes, and objects created by these classes are used directly; they are created by custom classes Object use, in the MVC mode, different business functions create different interfaces and class systems.

3. Properties and methods

Class variable

Location difference:

  • Member variables are defined outside the method, inside the class;
  • Local variables are defined in the method.

Function difference:

  • The role of member variables is to describe the public properties of a class of things.
  • The role of local variables is to provide a variable for internal use in the method.

Initial value difference:

  • Member variables have default initial values.
  • Local variables do not have a default initial value and must be initialized before they can be used.

Life cycle:

  • It exists as the object is created, and disappears as the object disappears.
  • The local variable exists when the statement that creates the variable is executed when the corresponding method is called. Once the local variable goes out of its own scope, it immediately disappears from the memory.

Method in class

A specific description of a class function, which may be a class method or an object method;

  • Class methods, also called static methods, are directly called by classes, commonly used tool classes;
  • Object methods, also called instance methods, are called using class objects, common API methods;

Construction method

The function of the constructor is to initialize the corresponding object. If there is no return value type, the function name of the constructor must be consistent with the class name. If a class does not explicitly declare a constructor, then the java compiler will Add a parameterless constructor to the class. If a class has been explicitly written with a constructor, then the java compiler will not add a parameterless constructor for the class. The constructor can be There are multiple overloads in a class.

4. Interface

The interface is a special class. The default modifier for the member variables of the interface is public-static-final, and the methods in the interface are abstract methods. The default modifier is public-abstract. The interface cannot create objects and has no construction method. Yes, the interface is used when the class is implemented. If a non-abstract class implements an interface, all methods in the interface must be implemented.

The role of the interface: the decoupling of the program, the definition of the constraint specification of the implementation class, if you need to expand the special function, it can also be implemented based on the interface.

The relationship between class and interface: multiple realization relationship; the relationship between interface and interface: multiple inheritance relationship.

5. Overloading and rewriting

Method rewriting

When the function of the parent class cannot meet the needs of the child class, the method can be rewritten, that is, the child parent class uses the function of the same name, and the method overridden by the child class uses the @Override annotation. There is an inheritance relationship here.

  • The method name and the parameter list must be consistent;
  • The permission modifier of the subclass must be greater than or equal to the permission modifier of the parent class;
  • The return value type of the subclass must be less than or equal to the return value type of the parent class;
  • The exception type thrown by the subclass should be less than or equal to the exception type thrown by the parent class;

Method overloading

There are two or more functions with the same name in a class, which are called method overloads. The function names must be consistent and the parameter lists are inconsistent (the number of parameters or the types of parameters are inconsistent).

6. Packages and permissions

Permission modifier

Permission modifier is to control the visibility of the scope of the modified member, divided into: public, private, protected, default (no modification). Only public and protected can be accessed under different packages, and protected must be accessible under inheritance relationship.

Package package

A package can be understood as a folder in a computer system to solve the problem of repeated conflicts of class names. The package name is all lowercase, and the statement must be located in the first statement in the java file.

2. Basic features

Package

Hide the internal implementation details of the class, and only expose the external interface. For example, all the member attributes (member variables) of the general entity class in the actual development scenario must be encapsulated, which can be considered as a protective barrier to prevent the code of this class And the data is randomly accessed by the code defined by the external class. Proper encapsulation can make the program code easier to understand and maintain, and strengthen security.

inherit

On the basis of the existing parent class, rebuild a new class, that is, the subclass. Through the subclass object, you can access the non-private member variables and member methods of the parent class, and override the non-private member methods in the parent class. The role of inheritance It can improve the reusability of the code.

Polymorphism

Polymorphism means that an object can have many forms, for example: the reference type variable of the parent class points to the object of the subclass; the reference type variable of the interface points to the object of the interface implementation class; this situation exists in the inheritance or realization relationship , In the case of polymorphism, when there is a member variable with the same name in the child parent class, all members of the parent class are accessed, except for non-static functions with the same name.

3. Common keywords

1. This keyword

The this keyword represents the caller object of the function. If there are member variables and local variables with the same name, the default is to access the data of the local variable inside the method. The data of the member variable can be specified by the this keyword, in a constructor You can call another constructor to initialize the object.

2. Static keyword

Static modified member variable: If there is data that needs to be shared for all objects, then static modification can be used.

Static modified member function: The static method can be accessed without relying on any object. Non-static member methods/variables must rely on specific objects to be called.

Static modified code blocks: The static keyword is used to package static code blocks to improve program performance. Static code blocks can be placed anywhere in the class. When the class is first loaded, each static code block will be executed in turn.

3. Super keyword

The super keyword represents a reference to the parent class space. When there are members with the same name in the sub-parent class, the sub-class defaults to access the members of the sub-class, and the super keyword can be used to specify access to the members of the parent class; when creating a sub-class object , The default construction method of the parent class without parameters will be called first, and the construction method of the parent class can be specified by the super keyword.

4. Instanceof keyword

The role of the instanceof keyword: to determine whether an object belongs to a specified category, the object and the specified category must have an inheritance or realization relationship. This keyword is usually used to judge before the forced type conversion, and then perform the conversion.

5. Final keywords

Final modified class: Indicates that the class cannot be inherited. The member variables in the final class can be set to final as needed, but note that all member methods in the final class will be implicitly designated as final methods.

Final modification method: the locking method prevents any inherited class from modifying its meaning; the second reason is efficiency, the final method of the parent class cannot be overridden by the subclass.

Final modified variable: A member variable is marked as a constant and can only be assigned once. The value will not change after assignment. When the parameter type of the function is declared as final, it means that the parameter is read-only.

6. Abstract keywords

Abstract modified class: The abstract class cannot be instantiated. The difference between an abstract class and an interface is that there can be no instance methods in the interface to implement business logic, while abstract classes can have instance methods and implement business logic. Abstract classes cannot be used Final keyword modification, because the final modified class cannot be inherited, and for abstract classes it is necessary to implement abstract methods through inheritance.

Abstract modification method: Abstract method has no method body, which is similar to interface method. Abstract method cannot be modified with private, because abstract method must be implemented by subclass, and abstract method cannot be modified with static. Abstract class. Abstract method is meaningless.

Four, scene analysis

1. Design pattern

Object-oriented and design patterns are very hot concepts in the past few years. Questions that must be asked in interviews. If you want to use design patterns in programming, you must understand the object-oriented mechanism, which is a high degree of abstraction and encapsulation of the problem, and layer by layer. Solve the realization, improve the code reusability, readability, flexibility, maintainability, and the most important thing is to have a high style.

2. IO stream system

If you want to understand the code of object-oriented and design patterns, you can focus on reading the API of the IO stream system in Java, the top-level interface, the downward implementation, the packaging class, and the extension class. These two concepts are really used to the extreme. Of course, the IO stream is the core of file processing, and it is also worth reading the source code.

3. MVC development model

In the MVC development model, business layer interface and business layer implementation; database interface and data access implementation are also the most basic object-oriented usage interface and implementation class of the interface.

Five, source code address

GitHub·地址
https://github.com/cicadasmile
GitEE·地址
https://gitee.com/cicadasmile

Recommended reading: finishing programming system

Serial number project name GitHub address GitEE address Recommended
01 Java describes design patterns, algorithms, and data structures GitHub·click here GitEE·Click here ☆☆☆☆☆
02 Java foundation, concurrency, object-oriented, web development GitHub·click here GitEE·Click here ☆☆☆☆
03 Detailed explanation of SpringCloud microservice basic component case GitHub·click here GitEE·Click here ☆☆☆
04 Comprehensive case of SpringCloud microservice architecture GitHub·click here GitEE·Click here ☆☆☆☆☆
05 Getting started with SpringBoot framework basic application to advanced GitHub·click here GitEE·Click here ☆☆☆☆
06 SpringBoot framework integrates and develops common middleware GitHub·click here GitEE·Click here ☆☆☆☆☆
07 Basic case of data management, distribution, architecture design GitHub·click here GitEE·Click here ☆☆☆☆☆
08 Big data series, storage, components, computing and other frameworks GitHub·click here GitEE·Click here ☆☆☆☆☆   

Guess you like

Origin blog.51cto.com/14439672/2540516