java13 object-oriented depth 3



Outline

Not written here

Object-oriented-encapsulation

understanding

Package ( encapsulation ) to understand how a computer using a package housing, which protects the electrical components, providing a small amount of use of the key abutment therewith. I want to watch TV, just press the switch and change channels. Is it necessary to understand the internal structure of the TV? Is it necessary to touch the picture tube? In order to make it convenient for us to use the TV, the manufacturer encapsulates all the complicated internal details, and only exposes simple interfaces, such as the power switch. We don't need to worry about how it is implemented internally.

What users need to know is exposed, and what users don’t need to know is hidden. This is the package, the vernacular: " The dew that should be exposed, the hidden that should be hidden ."

concept

​ Our program design should pursue " high cohesion, low coupling ". High cohesion means that the details of the internal data operation of the class are completed by themselves, and no external interference is allowed; low coupling: only a few methods are exposed for external use. Use abstract data types to encapsulate data and data-based operations to form an indivisible and independent entity. The data is protected inside the abstract data type, and internal details are hidden as much as possible, and only some external interfaces are kept. Contact with the outside world. The other parts of the system only communicate and interact with this abstract data type through authorized operations wrapped outside the data. In other words, the user does not need to know the implementation details of the internal method of the object, but can access the object according to the external interface (object name and parameters) provided by the object.

effect

a) Realize the professional division of labor. After encapsulating the code that can realize a specific function into an independent entity, each programmer can call it when needed, thus realizing professional division of labor, that is, the development of sub-modules and sub-functions in the work.
b), hide information and implement details. By controlling access rights, you can hide information that you don't want client programmers to see. For example, a customer's bank password needs to be kept secret and can only develop rights for that customer.

javabean

rule

There are some commonly used rules in writing javabeans such as:
1), the property as much as possible to privatize private
2), accessor: setter and getter accessor>private, generally public is mostly setterXxx: storage > and getterXxx: view

Code

public class Person {
    
      //姓名 
		private String name;   //年龄 
 	private int age;  //性别 
 	private boolean sex; 
public Person() {
    
    } 
//setter与getter public String getName() {
    
     
  		return name; 
 	} 
public void setName(String name) {
    
     
  		this.name = name; 
 	} 

 	public int getAge() {
    
     
 		 return age; 
 	} 

 	public void setAge(int age) {
    
     
 		 this.age = age; 
 	} 

 	public boolean isSex() {
    
     
 		 return sex; 
 	} 

 	public void setSex(boolean sex) {
    
     
 		 this.sex = sex; 
 	} 

}

Classic MVC ideas

Overview

MVC is a layered idea, a design pattern.

M: Model layer (model layer)

Process data and business, and then provide the processed data to the view layer

V: View layer (view layer)

Showing data to users is the most intuitive embodiment of data.

C: Controller layer (business logic layer)

Receive the data of the interface layer, and encapsulate and convert the received data.
Call the service of the model layer to process the business logic.
Call the appropriate view to render the data.

advantage

  • Reduce the amount of code and improve performance (efficiency);
  • Improve code scalability (requirement changes, no need to change too much code)
  • Follow the principles of high cohesion and low coupling

[External link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-pZvJ4SlB-1609312950192)(/1581945749553.png)]

Permission modifier

public: at a glance;

protected: The son inherits his father's business (the son uses it himself);

default: family harmony;

private: account for existing

Object-oriented-polymorphism

My girlfriend took me home, and my mother-in-law looked crazy! It turned out to be like "Zeng Zhiwei"!

Overview

Polymorphism is mainly used to implement dynamic binding. In other words, the final state of the program is only determined during execution rather than during compilation. This can improve the flexibility and scalability of the system for large systems.

How to achieve polymorphism in java? The benefits of using polymorphism?

Two types of reference variables: • Compile-time type (a little fuzzy, usually a parent class)

​ • Determined by the type of declaration.

​ • Runtime type (runtime, which subclass is the specific subclass)

​ • Determined by the actual corresponding object type.

There are three necessary conditions for the existence of polymorphism: there must be inheritance, there must be method rewriting, and the parent class reference points to the child class object

Code

Not written here

Type conversion of reference data types

• Subclass conversion to parent class: automatic conversion

​ The transformation object cannot operate the new member variables and methods of the subclass.

​ The upper transformation object can manipulate the member variables and methods inherited or overridden by subclasses

​ If the subclass overwrites a certain method of the parent class, when the method is called by the cast object, it is the overridden method that is called.

• The parent class is converted to a child class: forced conversion

(It is definitely not an operation, but the true face of the parent class is a subclass, otherwise a type conversion error will occur

Abstract class

Overview

The class declared with the keyword abstract is called "abstract class".

An abstract class is to model the abstract world and is used as a parent class. For example: request the area of ​​a figure? This is what graphics are you thinking about? Is this graphic too abstract? Is it a triangle or a prototype? How to find the area of ​​a graph? At this time, graphics can be defined as abstract classes.

A few points

  • Both abstract methods and abstract classes must be decorated with abstract.
  • An abstract method has no method body, it only needs to be declared without implementation.
  • Classes with abstract methods can only define abstract classes.
  • On the contrary, the methods in an abstract class are not all abstract methods, and there may be no abstract methods.
  • Abstract classes can contain attributes, methods, and construction methods.
  • Abstract classes cannot be instantiated, and abstract classes cannot be instantiated with new, they can only be used by subclasses.
  • Abstract classes can only be used for inheritance.
  • Abstract methods must be implemented by subclasses. Subclasses of abstract classes must cover all abstract methods to be instantiated, otherwise they are still abstract classes.

Code

Not written here

interface

Overview

​ Interface ( interface ), an "abstract class" that is more "abstract" than an "abstract class", can restrict subclasses more standardized. Realized comprehensively and professionally: separation of specification and specific realization

How to define

• Format:

​ • [Access modifier] interface interface name [extends parent interface 1, parent interface 2...] {

​ • Constant definition // always public static final

​ • Method definition // always: public abstract •

​ }

How to achieve

  • Subclasses implement the specifications in the interface through implements
  • Interfaces cannot create instances, but can be used to declare reference variable types.
  • A class implements an interface, it must implement all the methods in the interface, and these methods can only be public.
  • Java classes only support single inheritance, and interfaces support multiple inheritance

Empty interface

It is just an identification for JVM, it is the passport of JVM.
1), java.lang.Cloneable: Not all objects can be cloned otherwise CloneNotSupportedException
2), java.io.Serializable

In actual development, a business logic processing class should never inherit a well-implemented class. Either inherit an abstract class or implement an interface. The interface is preferred to avoid the limitation of single inheritance: abstract class represents the "is a" relationship, interface Represents the "like a" relationship

Insert picture description here

Guess you like

Origin blog.csdn.net/xyx12321/article/details/111991530