当前位置:网站首页>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?
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
Type conversion exception
2.6 Several forms of polymorphism
2.7 Instanceof
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
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
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 )
边栏推荐
- SQL Lab (46~53) (continuous update later) order by injection
- 【统计学习方法】学习笔记——逻辑斯谛回归和最大熵模型
- Sorting, dichotomy
- 【PyTorch实战】图像描述——让神经网络看图讲故事
- leetcode刷题:二叉树24(二叉树的最近公共祖先)
- 跨域问题解决方案
- Pule frog small 5D movie equipment | 5D movie dynamic movie experience hall | VR scenic area cinema equipment
- What is an esp/msr partition and how to create an esp/msr partition
- 2022A特种设备相关管理(锅炉压力容器压力管道)模拟考试题库模拟考试平台操作
- Routing strategy of multi-point republication [Huawei]
猜你喜欢
Inverted index of ES underlying principle
BGP third experiment report
Static routing assignment of network reachable and telent connections
【统计学习方法】学习笔记——支持向量机(下)
What if does not match your user account appears when submitting the code?
Static vxlan configuration
Tutorial on principles and applications of database system (010) -- exercises of conceptual model and data model
[deep learning] image multi label classification task, Baidu paddleclas
Configure an encrypted web server
Solutions to cross domain problems
随机推荐
H3C HCl MPLS layer 2 dedicated line experiment
Connect to blog method, overload, recursion
[statistical learning method] learning notes - support vector machine (I)
[statistical learning methods] learning notes - Chapter 5: Decision Tree
【统计学习方法】学习笔记——逻辑斯谛回归和最大熵模型
Financial data acquisition (III) when a crawler encounters a web page that needs to scroll with the mouse wheel to refresh the data (nanny level tutorial)
Idea 2021 Chinese garbled code
通讯协议设计与实现
Several methods of checking JS to judge empty objects
SQL lab 11~20 summary (subsequent continuous update) contains the solution that Firefox can't catch local packages after 18 levels
Decrypt gd32 MCU product family, how to choose the development board?
浅谈估值模型 (二): PE指标II——PE Band
In the small skin panel, use CMD to enter the MySQL command, including the MySQL error unknown variable 'secure_ file_ Priv 'solution (super detailed)
The left-hand side of an assignment expression may not be an optional property access.ts(2779)
Preorder, inorder and postorder traversal of binary tree
消息队列消息丢失和消息重复发送的处理策略
leetcode刷题:二叉树20(二叉搜索树中的搜索)
【统计学习方法】学习笔记——支持向量机(上)
[Q&A]AttributeError: module ‘signal‘ has no attribute ‘SIGALRM‘
[statistical learning methods] learning notes - improvement methods