多态polymorphism:方法或者对象具有多种形态

方法的多态

  1. 方法的重载可以体现多态

代码示例

// 通过方法重载,展现同一种方法的不同形态
public class PolyMethod {
public static void main(String[] args) {
AA aa = new AA();
aa.f();
aa.f(1);
aa.f(1, 2);
}
}
class AA {
public void f() {
System.out.println("f()");
}
public void f(int i) {
System.out.println("f(i)");
}
public void f(int i, int j) {
System.out.println("f(i,j)");
}
}
  1. 方法的重写可以体现多态

代码示例

// 通过子类继承父类并重写父类中的方法,体现同一个方法的不同形态
public class PolyMethod02 {
public static void main(String[] args) {
BB bb = new BB();
bb.f();
BBB bbb = new BBB();
bbb.f();
}
}
class BB {
public void f() {
System.out.println("BB f()");
}
}
class BBB extends BB {
@Override
public void f() {
System.out.println("BBB f()");
}
}

对象的多态

  1. 对象的编译类型与运行类型可以不同
  2. 对象的编译类型在定义对象的时候就确定了不能更改
  3. 对象的运行类型可以改变
  4. 对象的编译类型看创建对象时=的左边,运行类型看=的右边
    • Animal animal = new Dog();
    • animal = new Cat();

代码示例

public class PolyObject {
public static void main(String[] args) {
People people = new Student();
//people编译类型为People,运行类型为Student
//System.out.println(people.id);
//people.f2();
System.out.println("name=" + people.name);
System.out.println(people.show());
}
}
class People {
String name = "jack";
int age = 18;
public void f1() {
System.out.println("People f1()");
}
public String show() {
return "name=" + name + ",age=" + age;
}
}
class Student extends People {
int id = 1;
double score = 100;
@Override
public String show() {
return super.show() + ",id=" + id + ",score=" + score;
}
public void f2() {
System.out.println("Student f2()");
}
}

多态的注意事项和细节

  1. 使用父类对象引用可以调用父类中的所有成员(遵守访问权限)
  2. 使用父类对象引用不能调用子类的特有成员(编译阶段,只能按编译类型访问)
  3. 最终的运行效果,看运行类型
  4. !!!属性没有多态之说,属性的值直接看编译类型
  5. instanceof比较操作符,用于判断对象的运行类型是否为XX类型或者XX类型的子类型

代码示例

public class PolyDetail02 {
public static void main(String[] args) {
A a = new B();
System.out.println(a.count);
//System.out.println(a.x);
}
}
class A{
int count = 10;
}
class B extends A{
int count = 20;
int x = 100;
}

代码示例

public class PolyDetail03 {
public static void main(String[] args) {
C c = new D();
System.out.println(c instanceof C);
System.out.println(c instanceof D);
System.out.println(c instanceof Object);
D d = new D();
System.out.println(d instanceof C);
System.out.println(d instanceof D);
C cc = new C();
System.out.println(cc instanceof C);
System.out.println(cc instanceof D);
}
}
class C {}
class D extends C {}

向下转型

  1. 语法:子类类型 变量=(子类类型)父类引用;
  2. 只能强转父类的引用,而不能强转父类的对象
  3. 要求父类的引用必须指向当前目标类型的对象
  4. 当向下转型后,可以调用子类类型中所有的成员

Java动态绑定机制

  1. 当调用对象方法的时候,该方法会和该对象的内存地址/运行类型绑定
  2. 当调用对象属性时,没有动态绑定机制,哪里声明,哪里使用

代码示例

public class DynamicBinding {
public static void main(String[] args) {
A a = new B();
System.out.println(a.sum());
System.out.println(a.sum1());
}
}
class A {
public int i = 10;
public int sum() {
return getI() + 10;
}
public int sum1() {
return i + 10;
}
public int getI() {
return i;
}
}
class B extends A {
public int i = 20;
@Override
public int sum() {
return getI() + 20;
}
@Override
public int sum1() {
return i + 10;
}
@Override
public int getI() {
return i;
}
}

最新文章

  1. 详解<a>标签
  2. hasOwnproperty详细总结
  3. 学习日记day9: PC端页面流程优化
  4. linux库之pam
  5. linux----设置、添加别名(alias,unalias)
  6. Hadoop: Start-all.sh 后发现JPS后Namenode没有启动
  7. keil mdk中如何确保某一段程序不被优化掉
  8. 连接SQL SERVER数据库实例出错
  9. ABP增删改查代码片段
  10. css实现垂直水平居中的5种方法
  11. 神奇的Scala Macro之旅(一)- 什么时候用宏
  12. 7.2.4 else与if配对
  13. python基础学习(十)字符串
  14. 运营商专线服务的基本原理(BGP传递私网路由)
  15. jQuery(三) javascript跨域问题(JSONP解决)
  16. 【模板】kmp
  17. django 中 Oauth2 实现第三方登陆
  18. JAVA后端笔试试题(一)
  19. 项目中遇到的问题:Gradle传递性依赖冲突
  20. Educational Codeforces Round 37-F.SUM and REPLACE题解

热门文章

  1. Python版本共存、语法、变量和数据类型
  2. k8s中应用GlusterFS类型StorageClass
  3. 技术管理进阶——技术Leader如何拒绝业务方?
  4. 【深入理解计算机系统CSAPP】第六章 存储器层次结构
  5. 斯坦福NLP课程 | 第15讲 - NLP文本生成任务
  6. CentOS7 单节点和多节点 HPL测试
  7. vuex+Es6语法补充-Promise
  8. PHP odbc查询SQL SERVER数据库带有中文时无返回数据
  9. 图解 Apache SkyWalking UI 的使用
  10. C#取消正在运行的Task