当前位置:网站首页>继承和多态(下)
继承和多态(下)
2022-07-06 09:19:00 【犇犇犇犇犇犇】
继承和多态
目录:
承接上回
1.9proteced关键字

1.10继承方式
- 单继承
class A{
}
class B extends A{
}
- 多层继承
class A{
}
class B extends A{
}
class C extends B{
}
- 不同类继承同一个类
class A{
}
class B extends A{
}
class C extends A{
}
1.11final关键字
final int Size = 10 //常量;final class B extend A {}//密封类:表示此类不能再被继承final修饰方法//密封方法:代表此方法不能被重写
1.12继承与组合
继承是is - a的关系
- 比如说狗是动物
组合是has a or a part of
比如说学校由学生和老师组成
class Student{ } class Teacher{ } class School{ private Student[] students; private Teacher[] teachers; }
2.多态
2.1多态的概念
多态的概念:通俗来说,就是多种形态,具体点就是去完成某个行为,当不同的对象去完成时会产生出不同 的状态。
2.2多态的条件
完成向上转型
直接赋值
Animal animal = new Bird("鹦鹉",2);方法的传参
public static void func1(Animal animal){ } public static void main(String[] args) { Bird bird = new Bird("鹦鹉",2); func1(bird); }方法的返回值
public static Animal func2(){ return new Bird("鹦鹉",2); }
完成方法的重写
通过父类的引用来调用这个重写的方法(动态绑定)

2.3重写
满足方法重写的条件
- 方法名相同
- 方法的参数列表相同【个数,类型,顺序】
- 方法的返回值相同
- 协变类型:子类的返回值与父类的返回值是父子关系也可以
- static修饰的方法不能被重写
- private修饰的方法不能被重写
- 子类的访问修饰符要大于等于父类的访问修饰符
重写与重载的区别
类型 重载 重写 参数列表 必须修改 一定不能修改 返回值类型 可以修改 一定不能修改 访问限定修饰符 可以修改 一定不能做更严格的限制 动态绑定
class Animal{ private String name; private int age; public Animal(String name, int age) { this.name = name; this.age = age; //eat();->也可以发生所谓的动态绑定,以后不要这样写代码 } } class Bird extends Animal{ public String wing; public Bird(String name, int age) { super(name, age); } public void fly(){ System.out.println(getName()+" 正在飞"); } @Override public void eat() { System.out.println(getName()+" 要吃鸟粮"); } } public static void main(String[] args) { // Bird bird = new Bird("小鸟",3); // Animal animal = bird; //父类的引用 引用了子类的对象 Animal animal = new Bird("鹦鹉",2); animal.eat();//动态绑定 会输出子类的eat // animal.fly();//不可以调用了 此时通过父类引用,只能调用自己特有的方法,不可以调用子类方法 // animal.wing; }静态绑定:例如方法的重载
2.4向上转型和向下转型
2.4.1向上转型
在多态的条件中已经介绍

2.4.2向下转型
**向下转型非常不安全,不用 **
public static void main(String[] args) {
Animal animal = new Dog("狗子",3);
if(animal instanceof Bird){
//不是所有的动物都是鸟
Bird bird = (Bird)animal;
bird.fly();
}
}
public static void main5(String[] args) {
Animal animal = new Bird("鸟儿",3);
Bird bird = (Bird)animal;//向下转型
bird.fly();
}
2.5多态的优缺点
【使用多态的好处】
- 能够降低代码的 “圈复杂度”, 避免使用大量的 if - else
- 可扩展能力更强
- 如果要新增一种新的形状, 使用多态的方式代码改动成本也比较低.
public static void drawMap3(){
Rect rect = new Rect();
Cycle cycle = new Cycle();
Flower flower = new Flower();
Shape[] shapes = {
cycle, rect, cycle, rect, flower};
for (Shape shape:shapes) {
shape.draw();
}
}
public static void drawMap2(){
Rect rect = new Rect();
Cycle cycle = new Cycle();
Flower flower = new Flower();
String[] shapes = {
"cycle", "rect", "cycle", "rect", "flower"};
for (String shape : shapes) {
if (shape.equals("cycle")) {
cycle.draw();
} else if (shape.equals("rect")) {
rect.draw();
} else if (shape.equals("flower")) {
flower.draw();
}
}
}
public static void main(String[] args) {
drawMap3();
}
【多态的缺陷】
代码的运行效率降低。
2.6尽量避免在构造方法中重写
public Animal(String name, int age) {
this.name = name;
this.age = age;
//eat();->也可以发生所谓的动态绑定,以后不要这样写代码
}
class Bird extends Animal{
public String wing;
public Bird(String name, int age) {
super(name, age);
}
public void fly(){
System.out.println(getName()+" 正在飞");
}
@Override
public void eat() {
System.out.println(getName()+" 要吃鸟粮");
}
}
public class Test {
public static void main(String[] args) {
Animal animal = new Animal("动物",10);
// Animal animal = new Dog("狗子",3);
animal.eat();
}
}
结论: “用尽量简单的方式使对象进入可工作状态”, 尽量不要在构造器中调用方法(如果这个方法被子类重写, 就会触发动态绑定, 但是此时子类对象还没构造完成), 可能会出现一些隐藏的但是又极难发现的问题.
边栏推荐
- [算法] 剑指offer2 golang 面试题4:只出现一次的数字
- How do architects draw system architecture blueprints?
- Fairygui character status Popup
- 基本Dos命令
- Basic DOS commands
- [算法] 剑指offer2 golang 面试题13:二维子矩阵的数字之和
- 如何保障 MySQL 和 Redis 的数据一致性?
- [dry goods] cycle slip detection of suggestions to improve the fixed rate of RTK ambiguity
- FairyGUI条子家族(滚动条,滑动条,进度条)
- 341. Flatten nested list iterator
猜你喜欢

面试必备:聊聊分布式锁的多种实现!
![[dry goods] cycle slip detection of suggestions to improve the fixed rate of RTK ambiguity](/img/9d/7284c1399964d3fb48886f12e4941c.jpg)
[dry goods] cycle slip detection of suggestions to improve the fixed rate of RTK ambiguity
![[algorithm] sword finger offer2 golang interview question 1: integer division](/img/e6/f17135207b3540ec58e5a9eed54220.png)
[algorithm] sword finger offer2 golang interview question 1: integer division

系统设计学习(二)Design a key-value cache to save the results of the most recent web server queries

C code implementation of robust estimation in rtklib's pntpos function (standard single point positioning spp)

Chromatic judgement bipartite graph

闇の連鎖(LCA+树上差分)
![[untitled]](/img/b1/9a2bebebb24132a405fc4e7d854e51.png)
[untitled]

121 distributed interview questions and answers

121道分布式面试题和答案
随机推荐
面渣逆袭:Redis连环五十二问,三万字+八十图详解。
2022国赛Re1 baby_tree
几道高频的JVM面试题
阿里云一面:并发场景下的底层细节 - 伪共享问题
Fundamentals of UD decomposition of KF UD decomposition [1]
3月15号 Go 1.18 正式版发布 了解最新特色以及使用方法
Record: solution of 404 error of servlet accessing database in dynamic web project
What are the advantages of using SQL in Excel VBA
【GNSS数据处理】赫尔默特(helmert)方差分量估计解析及代码实现
系统设计学习(三)Design Amazon‘s sales rank by category feature
Detailed explanation of balanced binary tree is easy to understand
Novatel board oem617d configuration step record
FairyGUI增益BUFF數值改變的顯示
Affichage du changement de valeur du Buff de gain de l'interface graphique de défaillance
isEmpty 和 isBlank 的用法区别
FairyGUI簡單背包的制作
MySQL backup -- common errors in xtrabackup backup
Edit distance (multi-source BFS)
Record: newinstance() obsolete replacement method
Problems and solutions of robust estimation in rtklib single point location spp