当前位置:网站首页>2021-05-10方法重写多态注意事项
2021-05-10方法重写多态注意事项
2022-06-23 09:55:00 【鹿其其鹿】
方法重写
package oop.demo6;
//重写都是方法的重写,和属性无关
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){
//静态方法和非静态方法的区别很大
//静态方法:方法的调用只和左边定义的数据类型有关
//非静态:重写
A a = new A();
a.test();
//父类的引用指向子类
B b = new A();//子类重写了父类的方法
b.test();
}
}
重写:需要有继承关系,子类重写父类的方法
- 方法名必须相同
- 参数列表必须相同
- 修饰符:范围可以扩大(public>Protected>Default>private)
- 抛出的异常:范围可以被缩小,但是不能扩大(ClassNotFoundException–>Exception(大))
重写,子类的方法必须和父类一致,方法体不同
为什么要重写:
父类的功能,子类不一定需要,或者不一定满足
Alt+Insert:override
多态
即同一方法可以根据发送对象的不同而采用多种不同的行为方式
一个对象的实际类型是确定的,但可以指向对象的引用类型有很多
多态存在的条件:
- 有继承关系
- 子类重写父类方法
- 父类引用指向子类对象
注意:多态是方法的多态,属性没有多态
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){
//一个对象的实际类型是确定的
//new Student();
//new Person();
//可以指向的引用类型就不确定了:父类的引用指向子类
//Student能调用的方法都是自己的或者继承父类的
Student s1 = new Student();
//Person父类型,可以指向子类,但是不能调用子类独有的方法
Person s2 = new Student();
Object s3 = new Student();
//对象能执行哪些方法主要看对象左边的类型,和右边关系不大
s2.run();//子类重写了父类的方法,那就执行子类的方法
s1.run();
}
}
多态注意事项:
多态是方法的多态,属性没有多态
父类和子类有联系,类型转换异常:ClassCastException
存在条件:继承关系,方法需要重写
不能重写(也就不能实现多态):
static 方法,属于类,不属于实例
final 常量
private 方法
边栏推荐
- NiO example
- SQL教程之SQL 中数据透视表的不同方法
- High performance computing center nvme / nvme of nvme of overview
- 谷贱伤农,薪贱伤码农!
- 文件IO(1)
- UEFI source code learning 3.7 - norflashdxe
- Successful experience in postgraduate entrance examination for MTI master of English translation major of Beijing University of science and technology in 2023
- 135,137,138,445禁用导致无法远程连接数据库
- Web -- Information Disclosure
- [SUCTF 2019]CheckIn
猜你喜欢

开发者,你对云计算可能有些误解

Typora set up image upload service

2022 Gdevops全球敏捷运维峰会-广州站精华回放(附ppt下载)

web--信息泄漏

AI系统前沿动态第38期:谷歌已放弃TensorFlow?;训练大模型的四种GPU并行策略;LLVM之父:模块化设计决定AI前途
![[极客大挑战 2019]HardSQL](/img/73/ebfb410296b8e950c9ac0cf00adc17.png)
[极客大挑战 2019]HardSQL

sql根据比较日期新建字段

ICLR 2022 | dynamic convolution tadaconv in video and efficient convolution video understanding model tadaconvnext

谷贱伤农,薪贱伤码农!

Unity技术手册 - 生命周期内速度限制(Limit Velocity Over Lifetime)子模块和速度继承(Inherit Velocity)子模块
随机推荐
高性能算力中心 — InfiniBand — Overview
[極客大挑戰 2019]HardSQL
Gorm advanced query
高性能算力中心 — NVMe/NVMe-oF — NVMe-oF Overview
RPC kernel details you must know (worth collecting)!!!
Gesture recognition based on mediapipe
漫画 | Code Review快把我逼疯了!
2022 gdevops global agile operation and maintenance summit - essence playback of Guangzhou station (with PPT download)
Shengshihaotong enables high-quality development with industrial Digitalization
ICLR 2022 | 视频中的动态卷积TAdaConv以及高效的卷积视频理解模型TAdaConvNeXt
Three implementation methods of distributed lock
同花顺是炒股的么?在线开户安全么?
J. Med. Chem. | Release: a new drug design model for deep learning based on target structure
AI系统前沿动态第38期:谷歌已放弃TensorFlow?;训练大模型的四种GPU并行策略;LLVM之父:模块化设计决定AI前途
Three methods to find the limit of univariate function -- lobida's rule and Taylor's formula
Distributed common interview questions
xml相关面试题
[GYCTF2020]Blacklist
ICLR 2022 | dynamic convolution tadaconv in video and efficient convolution video understanding model tadaconvnext
2022 Gdevops全球敏捷运维峰会-广州站精华回放(附ppt下载)