当前位置:网站首页>Polymorphism, final, etc

Polymorphism, final, etc

2022-07-07 12:36:00 Xiaobai shelter

1 Final

1.1 What is it?
final It's a modifier , Indicates final , Unalterable

1.2 What can be done

final Modified class , uninheritable

final Modifies the member method , Can't be overwritten

final The modified variable cannot be assigned twice , No default , Assignment must be displayed

Generally, we put final Decorated static variables are called constants ,public static final data type Variable name = value ;
1.3 How to use it?
 Insert picture description here
 Insert picture description here
 Insert picture description here
2 polymorphic
2.1 What is it?

     Parent class reference     Point to a subclass object 

     Parent class reference : refer to     Referential variables declared with a parent type 

     Point to : Which object can be found by memory address         

     Subclass object :new   Subclass    Created heap memory object 

     Subclass      Variable   = new   Subclass ();

    Cat   c   =  new Cat();

     Parent type          Variable name      =   new     Subclass ();

    Animal   a   =   new  Cat();

2.2 Related knowledge

Six principles of software design :

1 Principle of single responsibility : Single function , Embrace only one change

2 Richter's principle of substitution : When the parent class can be used , You must be able to use subclasses

Because inheritance , Functions of parent class , Subclasses have

3 The principle of Dependence Inversion : Details should depend on abstraction , And abstraction should not depend on details

4 Interface isolation principle : I don't care

5 Dimitar's law : Minimum knowledge principle , And other classes or objects , Know as little as possible

6 Opening and closing principle : Turn off for changes , Open to expansion

2.3 advantage :

     Same operation , Scope different objects , There can be different explanations , It produces different results , It's polymorphism 

     When there are many different ways to implement something , We choose to rely on the top , To embrace variety 

     The essence is to reduce the coupling between classes and details 

2.4 shortcoming

     Missing subclass specific properties 

2.5 Applicable grammar
 Insert picture description here
 Insert picture description here
 Insert picture description here
 Insert picture description here
 Insert picture description here
Type conversion exception
 Insert picture description here
2.6 Several forms of polymorphism
 Insert picture description here
2.7 Instanceof
 Insert picture description here
3.abstract
3.1 What is it?
abstract: Modifier Modified class Abstract class , The decorated method is abstract method
Abstract classes cannot instantiate objects
Abstract methods have no method bodies , Define only the functions , No functional implementation , And the abstract method must be in the abstract class
conversely , In an abstract class There can be no abstract methods
abstract Unable to join final At the same time
3.2 Use the syntax
 Insert picture description here
4. Interface (Interface)
4.1 What is it?

Interface It can be understood as a completely abstract class , There are only abstract methods and constants

But from 1.8 Start , Allow to appear Static methods and default methods

grammar Modifier interface The interface name {}

Abstract methods in interfaces , No need to add abstract modification , Method The default is public abstract

Interface , No variables , Constant only , also public static final It can be omitted

Between classes and interfaces , No longer an inheritance relationship , It becomes an implementation relationship , from extends Instead of implements

The interface name Variable = new Subclass implementation () Polymorphism also occurs

One class Only one class can be inherited , however Can achieve N Interface , Separated by commas , It can solve the problem of weak single inheritance function

           class  Class name  implements  Interface 1 ,  Interface 2, Interface 3....{}

Interface and interface , It's multi inheritance , Multiple Separated by commas

          interface  The interface name  extends  Parent interface name 1, Parent interface name 2,...{}

One class If you implement an interface , Then you must implement all the abstract methods in the interface ,. Otherwise, it is necessary to add abstract modification

An abstract class , Implement an interface , Can achieve 0~N Abstract methods

1.7 There can only be abstract methods

1.8 There can be static methods , There can be default Method ( It can be understood as a member method )

                  Static methods , Call with the interface name 

            default  Method needs to be called by the sub implementation class , You can also override 

   1.9  Start   Support  private Method 

4.2 Usage method
 Insert picture description here
 Insert picture description here
 Insert picture description here
 Insert picture description here
5 Object

5.1 Equals

Object Is the ancestor of all classes , yes java The root class provided in
When a class does not show that it inherits from another class , Default inheritance object
Object xx = new xxx(); Polymorphism can occur
==: When comparing basic types , Compare the size of the value , But when comparing reference types , Compare memory addresses
*

  • Compare memory addresses , It has no value , We usually compare the attribute values of two objects , Is it consistent , Instead of comparing whether the addresses of two objects are consistent
    *equals(): The method is designed for , Used to compare whether two objects are equal , But the default comparison address
  • java in Object Inside equals Method , Default compare memory address (==) We need to rewrite according to the requirements
    5.2Finalize

finalize : This method will be called automatically when the garbage is collected , Unordered programmers manually call
The garbage : Be an object , When there are no more references to it , This object is treated as garbage data ( Is to create an object , No one can find him )
protected void finalize() throws Throwable { }
Object Medium finalize Method , Nothing has been done , You need to rewrite it according to your needs
5.3toString

toString: Represents the string representation of the current object
When we print a reference variable , Will automatically call the toString Method
and Object In the default toString Method Is to print the memory address of the object (hash value )

原网站

版权声明
本文为[Xiaobai shelter]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130617040708.html