当前位置:网站首页>Final, override, polymorphism, abstraction, interface
Final, override, polymorphism, abstraction, interface
2022-07-03 07:06:00 【L gold p】
polymorphic
1、 What is polymorphism
The parent class reference points to the subclass object
Parent class reference : refer to A reference type variable 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、 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
3、 advantage :
Same operation , Act on different objects , Can there be different explanations , Produce different structures , This is 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

shortcoming : Missing subclass specific attributes
grammar :
The disadvantages of polymorphism : Missing subclass specific attributes
Polymorphic property calls :
If the parent class doesn't have , Direct error , Whether or not the subclass has
If the parent class has , Subclasses don't have , Execute the parent class directly
Neither parent nor subclass , Member method execution subclass , Because member methods can override , All others execute the parent class
Several forms of polymorphism :
// 1 Direct polymorphism
Sup sup = new Sub();
// 2 Formal parameters and actual parameters , The method parameter list is the parent type , Call methods to pass in subclass objects
m1(new Sub());
// 3 The return value is polymorphic , The return value type is the parent type , But return subclass objects
Sup result = m2();
2、Final
1、 What is it? :final It's a modifier , Indicates final , Unalterable
2、 What can be done :final Modified class uninheritable
final Modifies the member method Can't be overwritten
final Decorated variable You cannot assign values twice No default Assignment must be displayed
Generally, we put final The variable decorated is called a constant , That is to say public static final data type Variable name = value ;
3、 Class final What's the difference between a variable and a normal variable ?
When used final When acting on a member variable of a class , Member variables ( Note that the member variables of the class , Only the variables need to be assigned before they are initialized ) Initialization assignments must be made at definition time or in the constructor , and final Once the variable has been initialized and assigned , It can't be assigned any more .

instanceof

3、abstract( abstract )
abstract : Modifier , Modified class , Abstract class , It's about abstract methods
*
* abstract class , Object cannot be instantiated
* Abstract method , There is no method body , Only set the function , No function 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
*1.abstract decorator , Will make this class an abstract class , This class will not be able to generate object instances , But it can be declared as a type of object variable , That is, compile time type , Abstract classes are like semi-finished products of a class , Subclasses are required to inherit and override the abstract methods in them .
2.abstract Modification methods , Will make this method an abstract method , That is, only statements ( Definition ) And it didn't happen , The implementation part is based on ";" Instead of . Need subclass inheritance implementation ( Cover ).
Be careful : A class with abstract methods must be an abstract class . But not all abstract methods in abstract classes , It can also be all specific methods .
abstract When decorating a class, the modifier must be placed in front of the class name .
abstract The decorating method is to require its subclasses to cover ( Realization ) This method . Subclass overrides can be called in a polymorphic manner when called ( Realization ) Post method , That is, abstract methods must be implemented in their subclasses , Unless the subclass itself is an abstract class .
Be careful : The parent class is an abstract class , There are abstract methods , Then the subclass inherits the parent class , And implement all abstract methods in the parent class ( Cover ) 了 , Subclasses have the ability to create instances of objects , Otherwise, the subclass must also be an abstract class . There can be construction methods in abstract classes , It is the parent class that the subclass needs to call when constructing the subclass object ( abstract class ) Construction method of .
4、interface( Interface )
* Interface It can be completely understood as a completely abstract class , Class names have only abstract methods and constants
* however 1.8 Start , Static methods and default methods are allowed
* grammar Modifier interface The interface name
* Abstract methods in interfaces , No need to add abstract modification , Method defaults to but 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 = Oh, wow Subclass implementation class () Polymorphism also occurs
* One class Only one class can be inherited , But it can be done 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....{}
* Interfaces and interfaces are multi inheritance , Multiple Separated by commas
*interface The interface name extends Parent interface name 1, Parent interface name 2...{}
* If a class implements 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 member method )
* Static methods , Call with interface
*default Methods need to be called through subclasses , You can also override
*1.9 Start supporting private Method
* If interface and abstraction can do one thing , Interface is preferred , Because this will preserve the inheritance of the class
5、Object
Object Is the ancestor of all classes , yes Java Root class in
* When a class does not obviously inherit 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 , however Default comparison address
* Java Medium Object Inside equals Method , Default compare memory address (==) We need to rewrite it according to the requirements
6、finalize
finalize: This method will be called automatically when garbage is collected , No need for programmers to 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 , The statement did not make , Need to rewrite according to requirements
7、toString
*tostring : Represents the string representation of the current object
* When we print a reference type variable , Will automatically call the tostring Method
* and Object Default in tostring Method Is to print the memory address of the object (hash value )
边栏推荐
- [untitled]
- Mise en place d'un environnement de développement de fonctions personnalisées
- Advanced API (character stream & net for beginners)
- PHP install the spool extension
- RestHighLevelClient获取某个索引的mapping
- Specified interval inversion in the linked list
- 【类和对象】深入浅出类和对象
- 10 000 volumes - Guide de l'investisseur en valeur [l'éducation d'un investisseur en valeur]
- These two mosquito repellent ingredients are harmful to babies. Families with babies should pay attention to choosing mosquito repellent products
- php artisan
猜你喜欢

HMS core helps baby bus show high-quality children's digital content to global developers

dataworks自定义函数开发环境搭建

How to specify the execution order for multiple global exception handling classes
![[set theory] equivalence classes (concept of equivalence classes | examples of equivalence classes | properties of equivalence classes | quotient sets | examples of quotient sets)*](/img/1f/f579110a408c5b5a094733be57ed90.jpg)
[set theory] equivalence classes (concept of equivalence classes | examples of equivalence classes | properties of equivalence classes | quotient sets | examples of quotient sets)*

2022-06-23 VGMP-OSPF-域间安全策略-NAT策略(更新中)

Use the jvisualvm tool ----- tocmat to start JMX monitoring

Practical plug-ins in idea

Pytest -- write and manage test cases

New knowledge! The virtual machine network card causes your DNS resolution to slow down

Jmeter+influxdb+grafana of performance tools to create visual real-time monitoring of pressure measurement -- problem record
随机推荐
Understand software testing
Unit test notes
RestHighLevelClient获取某个索引的mapping
crontab定时任务
Golang operation redis: write and read kV data
Basic components and intermediate components
Advanced API (serialization & deserialization)
Gridome + strapi + vercel + PM2 deployment case of [static site (3)]
[classes and objects] explain classes and objects in simple terms
On the practice of performance optimization and stability guarantee
php artisan
10000小時定律不會讓你成為編程大師,但至少是個好的起點
Distributed lock
Daily question brushing record (11)
EasyExcel
File links cannot be opened or downloaded in Google browser
Software testing assignment - the next day
Golang operation redis: write and read hash type data
How to plan well?
【无标题】