javase 02 java object-oriented

02 java object-oriented

Problem-solving thinking: object-oriented, how to do it, who can do it, ask him to do it.

​ Process-oriented, what to do in the first step and what to do in the second step.

Object-oriented:

  • Both are the way of thinking to solve problems, and the way of code organization.

  • To solve simple problems, you can use process-oriented

  • Solve complex problems: use object-oriented grasp at the macro level, while processing at the micro level is still process-oriented.

Object-oriented thinking:

When encountering a complex problem, first find nouns from the problem, then determine which of these nouns can be used as classes, and then determine the relationship between the classes according to the attributes and methods of the classes determined by the problem requirements.

Three characteristics of object-oriented programming languages

Encapsulation inheritance polymorphism

Encapsulation: Display the functions required by the customer, and hide the underlying functions.

Inheritance: Inherit the parent class and inherit its method attributes. A way to connect classes is similar to a singly linked list.

Polymorphism: The target of the class is not clear, and the method of obtaining the target subclass according to the object.

Static code block

  • It is executed when the class is initialized, not when the object is created.
  • Non-static members cannot be accessed in the static initialization block

Execution sequence: go back to the Object class, execute the static initialization block of the Object first, and then execute the static initialization block of the subclass down

State initialization block until the static initialization block of our class. (Look at inheritance)

package

It is usually the first non-commentary sentence of the class.

Package name: Write the domain name backwards, plus the module name, and the internal management class.

In fact, the internal implementation is achieved by the directory structure.

You must add packages when writing projects, do not use the default packages.

Main packages in jdk

  • java.lang-Contains some core classes of the Java language, such as String, Math, Integer, System and Thread, which provide common functions.
  • java.net-Contains classes that perform network-related operations.
  • java.io-Contains classes that provide multiple input/output functions.
  • java.util-Contains some utility classes, such as defining system characteristics and using functions related to date and calendar.

import

Import other packages

Static import (understand)

The role of static import: used to import static properties of the specified class

import static java.lang.Math.*; //Import all the static attributes of the Math class

import static java.lang.Math.PI; //Import the PI attributes of the Math class

Keyword Same class Same package Subclass All categories
private *
default * *
protected * * *
public * * * *

javaBean object

JavaBean is a simple java class, used to store basic information, generally corresponding to the database table

Method override

Rewrite of the parent class method:

  • "==": The method name and parameter list are the same.
  • "≤≤": return value type and exception type, the subclass is less than or equal to the parent class.
  • "≥": access authority, the child class is greater than or equal to the parent class

The following methods cannot be overridden:

  • private modified method
  • static modified method
  • final modified method

Attribute lookup order

  • Find if there is an attribute h in the current class
  • Trace each parent class in turn to see if there is h in each parent class until Object
  • If it is not found, a compilation error occurs.
  • In the above steps, as long as the h variable is found, the process is terminated.

The calling sequence of the constructor

  • According to the description of super, the first sentence of the constructor is always: super(...) to call the corresponding constructor of the parent class.

  • First trace back to Object, and then execute the initialization block and construction method of the class in turn until the current subclass.

  • If the default constructor is empty, the empty constructor is called layer by layer.

Methods in the Object class

  • Each class has Objectas a super class. All objects (including arrays) implement the methods of this class.

    Method signature Method function
    Class<?> getClass() Returns the Objectruntime class.
    boolean equals(Object obj) Indicates whether some other objects are equal to this.
    String toString() Returns the string representation of the object.
    void notify() Wake up a single thread that is waiting for the object monitor.
    void notifyAll() Wake up all threads that are waiting for the object monitor.
    void wait(long timeout) Causes the current thread to wait until another thread calls the object's notify()method or notifyAll()methods.
    int hashCode() Returns the hash code value of the object.

Polymorphism

Fuzzy designation of the parent class, the specific function of the method can be obtained during use, and various forms can be realized.

Method binding

When executing the call, the JVM can execute the code representing the method in the memory address according to related information.

Static binding

Finished in the compile period, can improve the code execution speed. Methods of static binding include:

  1. Static method

  2. Constructor

  3. private method

  4. Method called with keyword super

Dynamic binding

Dynamic binding refers to judging the actual type of the referenced object during execution (non-compilation period), and calling its corresponding method according to its actual type. During the running of the program, the process of combining the function (or procedure) call with the code needed to respond to the call is called dynamic binding. The method called by the object uses a dynamic binding mechanism. Although this gives us flexibility in programming, it reduces the execution speed of the code. This is also one of the main reasons why JAVA is slower than C/C++.

Abstraction and interface

abstract

Abstract class abstract method

Abstract class

  • Classes with abstract methods can only define abstract classes

  • Abstract classes cannot be instantiated, that is, abstract classes cannot be instantiated with new.

  • Abstract classes can contain attributes, methods, and construction methods. But the constructor cannot be used for new instances, it can only be used by subclasses.

  • Abstract classes can only be used for inheritance.

  • Abstract methods must be implemented by subclasses.

The difference between an abstract class and an ordinary parent class is that it contains abstract methods, which must be implemented by subclasses so that the design of subclasses can be standardized. To put it plainly, the role of abstract classes is :

Realize the separation of specification and specific implementation. Define the specification through the abstract method, and then require the subclass to define a specific implementation. Reference variables can still be defined as abstract classes, so that polymorphism can be standardized.

interface

Implementation specification of the canonical method

Why do I need an interface ? The difference between an interface and an abstract class ?

An interface is an "abstract class" that is more "abstract" than an "abstract class", and can restrict subclasses more standardized.

Realized comprehensively and professionally: the separation of specification and specific realization.

The abstract class also provides some specific implementations, the interface does not provide any implementation, all methods in the interface are abstract

law. The interface is completely specification-oriented and specifies the public method specifications of a batch of classes.

From the perspective of the implementer of the interface, the interface defines the services that can be provided to the outside.

From the perspective of the caller of the interface, the interface defines the services that the implementer can provide. **Interface is the standard of communication between two modules, the norm of communication. **If you can put the modules between the systems you want to design

The interface between the definitions is equivalent to the completion of the system design outline, and the rest is to add more concrete

Achieved. After you work, you often use the "interface-oriented" idea to design the system when building the system.

System.

When defining a vehicle, I define an abstract class Vehicle, and define subclasses Car, Plane, Train**. Not asked **

question. Abstract classes and subclasses are a general and special relationship.

As for the interface, it has a relationship with subclasses to implement rules. For example: I define an interface Runnable**, Car implements**

It can run on the ground, and Train can also run on the ground. The airplane realizes that it can also run on the ground. That is, as

If he is a vehicle, he must be able to run, and he must realize Runnable**. I define the interface again:**

Fligntable**, if you want to fly in the sky, you must implement the Flightable interface. **

Class class -> one class inherits another class

Class abstract class> One class inherits another abstract class (the class must override the abstract method)

Class interface -> One class implements several interfaces (classes need to rewrite all methods of all interfaces)

Interface interface -> one interface inherits several interfaces

After Java8

After Java 8 is released, new methods can be added to the interface, but the interface can still be compatible with its implementation class. This is very important because the library you develop may be widely used by multiple developers. Before Java 8, after an interface was published in the class library, if a new method was added to the interface, applications that implemented this interface would be in danger of crashing using the new version of the interface. (This problem cannot be completely avoided in java8) The method implemented in the interface is called the default method, which is identified by the keyword default as a modifier. When a class implements an interface, it can implement methods that have already been implemented in the interface, but this is not required. This class will inherit the default method. This is why when the interface changes, the implementation class does not need to be changed. The interface in java8 not only adds default methods, but also adds static methods. Define one or more static methods. Similar to the static method in the class, the static method defined by the interface can be called independently of any object. Therefore, when calling a static method, there is no need to implement an interface or an instance of the interface, which means that it is similar to the way of calling a static method of a class. The syntax is as follows: Interface name. Static method name. The class or sub-interface that implements the interface will not inherit the static methods in the interface. Static cannot be used with default at the same time. Static methods have been added to many interfaces in java8

Design principles of classes

  • Single responsibility principle

  • 里氏代换原则This is a strict is-a relationship that requires inheritance. All references to the base class must be able to transparently use its subclasses

    Object.

  • Dependency inversion principle-oriented interface programming

  • Interface isolation principle

  • 迪米特法则The Law of Demeter is also called the Least Knowledge Principle

    Write LKP), the less a class knows about other classes, the better, that is, an object should have

    Know as little as possible, and only communicate with friends, not strangers.

  • The principle of opening and closing is closed for modification and open for expansion.

The relationship between class and class

  • Inheritance

  • Realization relationship

  • 依赖关系 Simply understand, dependency is that a class A uses 2 and this use relationship is accidental and temporary

    Sexual, very weak, but changes in class B will affect class A.

  • 关联关系 Association reflects a strong dependency on the semantic level between two classes, such as my friend and me, this relationship

    The relationship is stronger than dependence, there is no contingency of dependence, and the relationship is not temporary, generally long-term

    , And the relationship between the two parties is generally equal. The association can be one-way or two-way.

  • 聚合关系 Aggregation is a special case of association relationship, which embodies the relationship between the whole and the part, that is, the has-a relationship.

  • 组合关系 Combination is also a special case of association relationship. It embodies a contains-a relationship, which is more

    The combination is stronger, also called strong polymerization.

Guess you like

Origin blog.csdn.net/xiaoliang98/article/details/109341341