当前位置:网站首页>2021-05-10 method rewrite polymorphism considerations
2021-05-10 method rewrite polymorphism considerations
2022-06-23 10:07:00 【Deer like deer】
Method rewriting
package oop.demo6;
// Rewriting is the rewriting of methods , It has nothing to do with attributes
public class B {
public static void test(){
System.out.println("B=>test()");
}
}
package oop.demo6;
public class A extends B{
public static void test(){
System.out.println("A=>test()");
}
}
package oop.demo6;
public class Application {
public static void main(String[]args){
// There is a big difference between static and non static methods
// Static methods : Method is only related to the data type defined on the left
// The static : rewrite
A a = new A();
a.test();
// A reference to a parent class points to a child class
B b = new A();// The subclass overrides the method of the parent class
b.test();
}
}
rewrite : There needs to be an inheritance relationship , Subclasses override methods of the parent class
- Method name must be the same
- The parameter list must be the same
- Modifier : The scope can be expanded (public>Protected>Default>private)
- Exception thrown : The scope can be narrowed , But not to expand (ClassNotFoundException–>Exception( Big ))
rewrite , The method of the subclass must be consistent with that of the parent class , Different methods
Why rewrite :
Functions of the parent class , Subclasses don't have to be , Or not necessarily satisfied
Alt+Insert:override
polymorphic
That is, the same method can adopt a variety of different behavior modes according to the different sending objects
The actual type of an object is certain , But there are many reference types that can point to objects
The condition of polymorphism :
- There is an inheritance relationship
- Subclass override parent method
- The parent class reference points to the subclass object
Be careful : Polymorphism is the polymorphism of method , Property is not polymorphic
instanceof
public class Person {
public void run(){
}
}
public class Student extends Person {
@Override
public void run() {
System.out.println("son");
}
}
public class Application {
public static void main(String[]args){
// The actual type of an object is certain
//new Student();
//new Person();
// The type of reference that can be pointed to is uncertain : A reference to a parent class points to a child class
//Student All methods that can be called are their own or inherited from the parent class
Student s1 = new Student();
//Person Parent type , Can point to subclasses , But you can't call methods unique to subclasses
Person s2 = new Student();
Object s3 = new Student();
// The methods that an object can execute mainly depend on the type on the left of the object , It has nothing to do with the right side
s2.run();// The subclass overrides the method of the parent class , Then execute the subclass method
s1.run();
}
}
Polymorphism considerations :
Polymorphism is the polymorphism of method , Property is not polymorphic
Parent and child classes are related , Type conversion exception :ClassCastException
Existence condition : Inheritance relationships , Method needs to be overridden
Can not rewrite ( You can't achieve polymorphism ):
static Method , Belong to category , Not an example
final Constant
private Method
边栏推荐
- Bioinformatics | 基于相互作用神经网络的有效药物-靶标关联预测
- Go language JSON processing
- NiO example
- Distributed common interview questions
- 构建信创产业生态,移动云立足全栈自主创新连放大招
- 启明星辰华典大数据量子安全创新实验室揭牌,发布两款黑科技产品
- 2021-04-16递归
- Mysql database introduction summary
- Unable to enter the system normally, press F8 to select other items to try to enter
- [GXYCTF2019]BabySQli
猜你喜欢
![[GXYCTF2019]BabySQli](/img/51/a866a170dd6c0160ce98d553333624.png)
[GXYCTF2019]BabySQli

基於STM32設計的寵物投喂器

SQL create a new field based on the comparison date

利用华为云ECS服务器搭建安防视频监控平台

必须知道的RPC内核细节(值得收藏)!!!

Go语言JSON 处理

Liujinhai, architect of zhongang Mining: lithium battery opens up a Xintiandi for fluorine chemical industry

Gorm advanced query

Bioinformatics | effective drug target correlation prediction based on interacting neural network

启明星辰华典大数据量子安全创新实验室揭牌,发布两款黑科技产品
随机推荐
Bugs encountered in Oracle
2021-05-07封装 继承 super this
#gStore-weekly | gStore源码解析(四):安全机制之黑白名单配置解析
2021-05-12接口的定义与实现
Go 字符串比较
AI系统前沿动态第38期:谷歌已放弃TensorFlow?;训练大模型的四种GPU并行策略;LLVM之父:模块化设计决定AI前途
Copilot免费时代结束!正式版67元/月,学生党和热门开源项目维护者可白嫖
SQL create a new field based on the comparison date
2021-05-11 static keyword
Gstore weekly gstore source code analysis (IV): black and white list configuration analysis of security mechanism
Confessing with Snake games (with source code)
利用华为云ECS服务器搭建安防视频监控平台
Jog sport mode
Fill the pit for repvgg? In fact, it is the repoptimizer open source of repvgg2
Navagation navigation
Install using snap in opencloudos NET 6
Comic | code review is driving me crazy!
Thin film interference data processing
高性能算力中心 — NVMe/NVMe-oF — NVMe-oF Overview
数学分析_笔记_第2章:实数与复数