当前位置:网站首页>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 方法
边栏推荐
- Is there anyone who plans to open source an industrial "seckill" system architecture?
- Gstore weekly gstore source code analysis (IV): black and white list configuration analysis of security mechanism
- Gesture recognition based on mediapipe
- AI: the Elephant in Room
- Go language JSON processing
- J. Med. Chem. | RELATION: 一种基于靶标结构的深度学习全新药物设计模型
- 薄膜干涉数据处理
- Subscript operator of map
- regular expression
- ICLR 2022 | dynamic convolution tadaconv in video and efficient convolution video understanding model tadaconvnext
猜你喜欢

AI系统前沿动态第38期:谷歌已放弃TensorFlow?;训练大模型的四种GPU并行策略;LLVM之父:模块化设计决定AI前途
![[plugin:vite:import-analysis]Failed to resolve import “@/“ from ““.Does the file exist](/img/1f/dde52dc63de58d67f51161e318a9fb.png)
[plugin:vite:import-analysis]Failed to resolve import “@/“ from ““.Does the file exist

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

IPv6 的速度比 IPv4 更快?

汇编语言中断及外部设备操作篇--06

Lying trough, the most amazing paper artifact!

sql根据比较日期新建字段

AI system frontier dynamics issue 38: Google has abandoned tensorflow?; Four GPU parallel strategies for training large models; Father of llvm: modular design determines AI future

Three methods to find the limit of univariate function -- lobida's rule and Taylor's formula
![[GXYCTF2019]BabyUpload](/img/82/7941edd523d86f7634f5532ab97717.png)
[GXYCTF2019]BabyUpload
随机推荐
[plugin:vite:import-analysis]Failed to resolve import “@/“ from ““.Does the file exist
高性能算力中心 — InfiniBand — Overview
RT thread add MSH command
JS教程之 什么是 JSX?为什么我们需要它?
【CTF】 2018_ rop
[geek Challenge 2019] hardsql
[极客大挑战 2019]HardSQL
Game of life of leetcode topic analysis
16. system startup process
RPC kernel details you must know (worth collecting)!!!
Gorm advanced query
2022 gdevops global agile operation and maintenance summit - essence playback of Guangzhou station (with PPT download)
UEFI source code learning 4.1 - pcihostbridgedxe
sql编写问题,求出当月和上月的环比值
Install the typescript environment and enable vscode to automatically monitor the compiled TS file as a JS file
[極客大挑戰 2019]HardSQL
开发者,你对云计算可能有些误解
Thin film interference data processing
High performance computing center nvme / nvme of nvme of overview
Developer, you may have some misunderstandings about cloud computing