当前位置:网站首页>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
边栏推荐
- 用贪吃蛇小游戏表白(附源码)
- The era of copilot free is over! The official version is 67 yuan / month, and the student party and the defenders of popular open source projects can prostitute for nothing
- Unity技术手册 - 生命周期LifetimebyEmitterSpeed-周期内颜色ColorOverLifetime-速度颜色ColorBySpeed
- File IO (1)
- 高性能算力中心 — RDMA — 实现技术
- CVPR大会现场纪念孙剑博士,最佳学生论文授予同济阿里,李飞飞获黄煦涛纪念奖...
- Build a security video monitoring platform using Huawei cloud ECS server
- RT-Thread 添加 msh 命令
- 基于STM32设计的宠物投喂器
- SQL教程之 5 个必须知道的用于操作日期的 SQL 函数
猜你喜欢

炫酷相册代码,祝对象生日快乐!

Servlet-02 lifecycle

R和RStudio下载安装详细步骤

启明星辰华典大数据量子安全创新实验室揭牌,发布两款黑科技产品

Go string comparison

SQL writing problem to calculate the ring ratio of the current month and the previous month

解决audio自动播放无效问题

基于STM32设计的宠物投喂器

SQL create a new field based on the comparison date

给RepVGG填坑?其实是RepVGG2的RepOptimizer开源
随机推荐
2021-05-11instanceof和类型转换
Shengshihaotong enables high-quality development with industrial Digitalization
Comic | code review is driving me crazy!
搭建一个点歌QQ机器人,另外还能看美女
2021-04-15
ICLR 2022 | 视频中的动态卷积TAdaConv以及高效的卷积视频理解模型TAdaConvNeXt
数值计算方法
After the uncommitted transactions in the redo log buffer of MySQL InnoDB are persisted to the redo log, what happens if the transaction rollback occurs? How does the redo log handle this transaction
High performance computing center RDMA implementation technology
Thin film interference data processing
【软件与系统安全】堆溢出
高性能算力中心 — NVMe/NVMe-oF — NVMe-oF Overview
Build a security video monitoring platform using Huawei cloud ECS server
Simple understanding of quick sort
Mysql database introduction summary
利用华为云ECS服务器搭建安防视频监控平台
thymeleaf如何取url中请求参数值?
Copilot免费时代结束!正式版67元/月,学生党和热门开源项目维护者可白嫖
Bi SQL drop & alter
必须知道的RPC内核细节(值得收藏)!!!