当前位置: 首页 > news >正文

JavaSE多态

多态:一个对象在不同条件下表示的不同形态就叫多态。在程序中,多态是父类引用指定子类对象就叫多态。

多态是面向对象程序设计中的第三个特征

// 多态
class Father {String name;public void desc() {System.out.println("----------");System.out.println(name + "小头爸爸");}
}
class Son extends Father {String name;public void desc() {System.out.println("+++++++++++++");System.out.println(name + "大头儿子");}
}public class PolymorphicDemo1 {public static void main(String[] args) {Father father = new Son();father.name = "大头儿子";father.desc();}
}

另一种多态的使用方式是在方法的参数上,一般我们在使用工厂设计模式时会使用多态。

在参数列表上,使用父类去接收,它也是一种多态的使用方式。

abstract class Animal {abstract void haha();
}
class Cat extends Animal {@Overridevoid haha() {System.out.println("Cat haha()");}
}
class Dog extends Animal {@Overridevoid haha() {System.out.println("Dog haha()");}
}class HelloKit {public static Animal create(Animal animal) {if (animal instanceof Cat) {return new Cat();}else if (animal instanceof Dog) {return new Dog();} else {return null;}}
}
public class HelloKitDemo {public static void main(String[] args) {Cat cat = new Cat();Dog dog = new Dog();Animal animal = HelloKit.create(cat);animal.haha();Animal animal1 = HelloKit.create(dog);animal1.haha();}
}

使用多态的注意事项

1)在多态的情况下,不能使用子类特有的方法。

class Person {public int age = 5;public void show() {System.out.println("Person age: " + age);}
}
class Student extends Person {public int age = 10;public String name = "小明";public void show() {System.out.println("Student age: " + age);}public void speak() {System.out.println(name);}
}public class PolymorphicDemo2 {public static void main(String[] args) {Person p = new Student();p.show(); // 可以执行p.speak(); // 报错,因为 speak 方法是子类特有的方法}
}

2)多态在实例时如果属性是非静态的,它的值是看等号的左边

// 多态的注意事项
class Person {public int age = 5;public void show() {System.out.println("Person age: " + age);}
}
class Student extends Person {public int age = 10;public String name = "小明";public void show() {System.out.println("Student age: " + age);}public void speak() {System.out.println(name);}
}public class PolymorphicDemo2 {public static void main(String[] args) {Person p = new Student();System.out.println(p.age); // 5 ,是不是 10}
}

原因:我们使用的是多态,也就是说我们输出的 p.age ,而 p 是父为的引用,它获取到的值就是父类中定义的值。

在多态下,编译是看等号的左边,运行时如果是属性则看等号左边。

3)在多态下的非静态方法

// 多态的注意事项
class Person {public int age = 5;public void show() {System.out.println("Person age: " + age);}
}
class Student extends Person {public int age = 10;public String name = "小明";public void show() {System.out.println("Student age: " + age);}
}public class PolymorphicDemo2 {public static void main(String[] args) {Person p = new Student();p.show();}
}

在多态下,编译是看等号的左边,运行时如果方法是非静态的看等号的右边

4)在多态下的静态成员

// 多态的注意事项
class Person {public static void x() {System.out.println("Person static x");}
}
class Student extends Person {public static void x() {System.out.println("Student static x");}
}
public class PolymorphicDemo2 {public static void main(String[] args) {Person p = new Student();p.x(); // Person static x}
}

在多态下,静态的成员在编译和运行时都看等号的左边

http://www.lryc.cn/news/348321.html

相关文章:

  • M 有效算法
  • 知识付费系统制作,托管机构如何提高体验课转化率?要注意什么?
  • 【iOS逆向与安全】网上gw如何自动登录与签到SM2,SM3,SM4算法加解密
  • 《CKA/CKAD应试指南/从docker到kubernetes 完全攻略》学习笔记 第14章 包管理helm v3
  • 蓝桥杯备战.19有奖问答dfs
  • 【JS红宝书学习笔记】第1、2章 初识JS
  • 学习java
  • Redis日常维护流程及技巧:确保稳定性与性能
  • 牛客华为机试题——难度:入门(python实现)
  • 数据结构与算法学习笔记之线性表五---循环链表的表示和实现(C++)
  • 微信小程序生命周期揭秘:从启动到消亡的全过程剖析【附代码】
  • Linux 下载 miniconda
  • 第十五篇:全面防护:构建不容侵犯的数据库安全策略与实战指南
  • 电脑快速搜索文件及文件夹软件——Everything
  • 02-登录页面、动态路由、权限等模块开发
  • 万物生长大会 | 创邻科技再登杭州准独角兽榜单
  • (六)Linux的Shell编程(上)
  • CANopen总线_CANOpen开源协议栈
  • Rust 语言不支持 goto 语句
  • uniapp日期区间选择器
  • k8s job
  • Python---NumPy万字总结【此篇文章内容难度较大,线性代数模块】(3)
  • 【面试经典题】环形链表
  • 【联合索引】最左匹配原则是什么?
  • LeetCode 700.二叉搜索树中的搜索
  • 程序设计实践-课程设计任务布置(麦当劳) (price 200)(不包含文档)
  • leetcode 918.环形子数组的最大和
  • Spring中用到的设计模式有哪些
  • CSS 样式清单整理:文字超出部分显示省略号和设置placeholder的字体样式
  • Docker容器:Docker-Consul 的容器服务更新与发现