专栏:JavaSE
作者简介:大三学生,希望跟大家一起进步

猫吃猫粮:

再比如学习:
有些小猫咪学习:

再有些小猫咪学习:

总的来说:同一件事情,发生在不同对象身上就会产生不同的结果。
🥝在java中要实现多态,必须要满足如下几个条件,缺一不可:
1.必须在继承体系下
2.子类必须要对父类中方法进行重写
3.通过父类的引用调用重写的方法(向上转型)
多态体现:在代码运行时,当传递不同类对象时,会调用对应类中的方法
- class Animal {
- String name;
- int age;
- public Animal(String name, int age){
- this.name = name;
- this.age = age;
- }
- public void eat(){
- System.out.println(name + "吃饭");
- }
- }
- class Cat extends Animal{
- public Cat(String name, int age){
- super(name, age);
- }
- @Override
- public void eat(){
- System.out.println(name+"吃鱼~~~");
- }
- }
- class Dog extends Animal {
- public Dog(String name, int age){
- super(name, age);
- }
- @Override
- public void eat(){
- System.out.println(name+"吃骨头~~~");
- }
- }
- public class Test {
- public static void eat(Animal a){
- a.eat();
- }
- public static void main(String[] args) {
- Cat cat = new Cat("元宝",2);
- Dog dog = new Dog("小七", 1);
- eat(cat);
- eat(dog);
- }
- }
输出结果:

当类的调用者在编写eat这个方法时,参数类型为Animal(父类),此时在该方法内部并不知道,也不关注当前的a引用指向的时哪个类型(哪个子类)的实例。此时a这个引用调用eat方法可能会有多种不同的表现。

🧀重写(override):也称为覆盖。重写是子类对父类非静态、非private修饰、非final修饰,非构造方法等的实现过程进行重新编写,返回值和形参都不能改变。
🍨重写的好处:子类可以根据需要,定义特定于自己的行为。
【方法重写的规则】
1.子类在重写父类的方法时,一般必须与父类方法原型一致:返回值 方法名 (参数列表)要完全一致
2.被重写的方法返回值可以不同,但是必须时具有父子关系的
3.访问权限不能比父类中被重写的方法的访问权限更低。比如:如果父类方法被public修饰,则子类中重写该方法就不能声明为protected
4.重写的方法,可以使用@Override注解来显式指定。有了这个注解,就能帮我们进行一些合法性校验。
当比如,我们把子类的方法名字故意改成eat1,则会出现下面的编译错误。

【重写和重载的区别】

🌾方法重载是一个类的多态性表现,而方法重写是子类与父类的一种多态性表现
方法重载的参数是不一样的。方法重写的参数是一样的。
【重写的设计原则】
🥙动态绑定的条件:
1.发生向上转型
2.发生重写
3.通过父类引用,调用这个父类和子类重写的方法
我们举这个代码为例:
- class Animal{
- String name;
- public void eat(){
- System.out.println(name+"吃饭");
- }
- }
- class Cat extends Animal{
- public void eat(){
- System.out.println(name+"吃猫粮");
- }
- }
- class Dog extends Animal{
- public void eat(){
- System.out.println(name+"吃狗粮");
- }
- }
- public class Test {
- public static void eat(Animal a){
- a.eat();
- }
- public static void main(String[] args) {
- Animal animal1=new Dog();
- animal1.name="小汪";
- animal1.eat();
- Animal animal2=new Cat();
- animal2.name="小喵";
- animal2.eat();
- }
- }
在代码界面右键,点击Open In,选择exploer,进入该代码所在的文件下,然后寻找该代码的out文件:

选择:

接着,我们在这里:

删掉内容,输入 cmd,回车:

跳出以上页面。输入
javap -c Test
回车生成:

我们会注意到:

这证明了,动态绑定需要等到程序运行时,才能够确定具体调用那个类的方法。
语法格式:父类类型 对象名 = new 子类类型()
Animal animal = new Cat ( " 小喵 " , 2 );
使用场景
1.直接赋值
2.方法传参
3.方法返回
- class Animal{
- String name;
- public void eat(){
- System.out.println(name+"吃饭");
- }
- }
-
- class Dog extends Animal{
- public void eat(){
- System.out.println(name+"吃狗粮");
- }
- }
- public class Test {
- public static void eat(Animal a){
- a.eat();
- }
- public static void main(String[] args) {
- Animal animal1=new Dog();
- animal1.name="小汪";
- animal1.eat();
- }
- }
其中:

为向上转型。
🍐将一个子类对象经过向上转型之后当成父类方法使用,再无法调用子类的方法,但有时候可能需要调用子类特有的方法,此时:将父类引用再还原为子类对象即可,即向下转换。
👇为向下转型:
向下转型用的比较少,而且不安全,万一转换失败,运行时就会抛异常。Java中为了提高向下转型的安全性,引入了 instanceof ,如果该表达式为true,则可以安全转换。
- class Animal{
- String name;
- public void eat(){
- System.out.println(name+"吃饭");
- }
- }
-
- class Dog extends Animal{
- public void eat(){
- System.out.println(name+"吃狗粮");
- }
- }
- public class Test {
- public static void eat(Animal a){
- a.eat();
- }
- public static void main(String[] args) {
- Animal animal1=new Dog();
- if(animal1 instanceof Dog){
- Dog dog=(Dog)animal1;
- animal1.name="小汪";
- animal1.eat();
- }
- }
- }
💰使用多态的优点:
1.能够降低代码的“圈复杂度”,避免使用大量的if-else
圈复杂度是一种描述一段代码复杂程度的方式. 一段代码如果平铺直叙, 那么就比较简单容易理解. 而如果有很多的条件分支或者循环语句, 就认为理解起来更复杂.因此我们可以简单粗暴的计算一段代码中条件语句和循环语句出现的个数, 这个个数就称为 "圈复杂度". 如果一个方法的圈复杂度太高, 就需要考虑重构. 不同公司对于代码的圈复杂度的规范不一样. 一般不会超过 10
- class DrawDraw{
- public void Draw(){
- System.out.println("画画");
- }
- }
-
- class Flower extends DrawDraw{
- public void Draw(){
- System.out.println("❀");
- }
- }
- class Star extends DrawDraw{
- public void Draw(){
- System.out.println("⭐");
- }
- }
- public class Test {
- public static void main(String[] args) {
- DrawDraw[] shapes = {new Star(), new Flower()};
- for (DrawDraw shape : shapes) {
- shape.Draw();
- }
- }
- }
2.可扩展能力强
- class DrawDraw{
- public void Draw(){
- System.out.println("画画");
- }
- }
-
- class Flower extends DrawDraw{
- public void Draw(){
- System.out.println("❀");
- }
- }
- class Star extends DrawDraw{
- public void Draw(){
- System.out.println("⭐");
- }
- }
- class Sun extends DrawDraw{
- public void Draw(){
- System.out.println("☀");
- }
- }
- public class Test {
- public static void main(String[] args) {
- DrawDraw[] shapes = {new Star(), new Flower(),new Sun()};
- for (DrawDraw shape : shapes) {
- shape.Draw();
- }
- }
- }
- class B {
- public B() {
- // do nothing
- func();
- }
- public void func() {
- System.out.println("B.func()");
- }
- }
- class D extends B {
- private int num = 1;
- @Override
- public void func() {
- System.out.println("D.func() " + num);
- }
- }
- public class Test {
- public static void main(String[] args) {
- D d = new D();
- }
- }
- // 执行结果
- D.func() 0