知识点:

1、java 中父类引用指向子类对象时动态绑定针对的只是子类重写的成员方法;

2、父类引用指向子类对象时,子类如果重写了父类的可重写方法(非private、非 final 方法),那么这个对象调用该方法时默认调用的时子类重写的方法,而不是父类的方法;

3、对于java当中的方法而言,除了final,static,private 修饰的方法和构造方法是前期绑定外,其他的方法全部为动态绑定;(编译看左边,运行看右边)

本质:java当中的向上转型或者说多态是借助于动态绑定实现的,所以理解了动态绑定,也就搞定了向上转型和多态。

package test;

public class test{
public static void main(String[] args) {
System.out.println("--------父类引用指向子类对象-----------"); Father instance = new Son();
instance.printValue(); //this is Son's printValue() method.---Son
instance.printValue2();                    //this is father's printValue2() method.---father
System.out.println(instance.name);              //father
instance.printValue3();                    //this is father's static printValue3() method. System.out.println("----------创建子类对象------------"); Son son = new Son();                     
son.printValue();                       //this is Son's printValue() method.---Son
son.printValue2();                      //this is father's printValue2() method.---father
System.out.println(son.name);                //Son 
son.printValue3();                      //this is Son's static printValue3() method. System.out.println("---------创建父类对象-----------"); Father father = new Father();               
father.printValue();                    //this is father's printValue() method.---father
father.printValue2();                   //this is father's printValue2() method.---father
System.out.println(father.name);            //father
father.printValue3();                   //this is father's static printValue3() method.
} } class Father { public String name = "father"; public void printValue() {
System.out.println("this is father's printValue() method.---"+this.name);
} public void printValue2(){
System.out.println("this is father's printValue2() method.---"+this.name);
} public static void printValue3(){
System.out.println("this is father's static printValue3() method.");
}
} class Son extends Father { public String name = "Son"; public void printValue() {
System.out.println("this is Son's printValue() method.---"+this.name);
} public static void printValue3(){
System.out.println("this is Son's static printValue3() method.");
}
}

分析:

1、父类引用指向子类对象,对象调用的方法如果已经被子类重写过了则调用的是子类中重写的方法,而不是父类中的方法;

2、父类引用指向子类对象,如果想要调用子类中和父类同名的成员变量,则必须通过getter方法或者setter方法;

3、父类引用指向子类对象,如果想调用子类中和父类同名的静态方法,直接子类“类名点” 操作获取,不要通过对象获取;

4、父类引用指向子类对象的两种写法:(第二种得了解,面试中可能用到)

// 1、第一种常规写法
Father instance = new Son();
// 2、第二种变形写法;
Son son = new Son();
Father mson = (Father) son;

java 父类引用指向子类对象---动态绑定的更多相关文章

  1. Java多态-如何理解父类引用指向子类对象

    java多态,如何理解父类引用指向子类对象 要理解多态性,首先要知道什么是“向上转型”. 我定义了一个子类Cat,它继承了Animal类,那么后者就是前者是父类.我可以通过   Cat c = new ...

  2. java多态性,父类引用指向子类对象

    父类引用指向子类对象指的是: 例如父类Animal,子类Cat,Dog.其中Animal可以是类也可以是接口,Cat和Dog是继承或实现Animal的子类. Animal animal = new C ...

  3. Java多态 父类引用指向子类对象

    Java多态的三个必要条件: 1. 继承 2. 子类重写父类方法 3. 父类引用指向子类对象 然后看一个例子 输出结果为: 给出结论:Father  c  =  new  Child()    在c的 ...

  4. java-多态中成员访问特点-父类引用指向子类对象

    多态前提: - 要有继承关系. - 要有方法重写. - 要有父类引用指向子类对象. 1.成员变量:编译看左边(父类),运行看左边(父类) 2.成员方法:编译看左边(父类),运行看右边(子类),动态绑定 ...

  5. 向上转型---父类引用指向子类对象 A a = New B()的使用

    一.向上转型 向上转型是JAVA中的一种调用方式,是多态的一种表现.向上转型并非是将B自动向上转型为A的对象,相反它是从另一种角度去理解向上两字的:它是对A的对象的方法的扩充,即A的对象可访问B从A中 ...

  6. C#多态;父类引用指向子类对象;new和override的区别;new、abstract、virtual、override,sealed关键字区别和使用代码示例;c#类的初始化顺序

    关于父类引用指向子类对象 例如: 有以下2个类 public class Father { public int age = 70; public static string name = " ...

  7. 28 面向对象编程 instanceof 代码 小结 父类引用指向子类对象

    instanceof 代码 // main // Object > Person >Student Object object = new Student(); // 提取公式:XY之间是 ...

  8. JAVA基础复习与总结<一>(2) 父类引用指向子类对象(向上转型、动态链接)

    先来看看下列代码 public class Animal { public static void main(String[] args){ Animal animal = new Cat(); // ...

  9. C++析构函数的自动调用(用于父类指针指向子类对象,内存泄漏问题)

    class A {public:A() { printf("A \n"); }~A() { printf(" ~A \n"); } // 这里不管写不写virt ...

随机推荐

  1. 《Java并发编程实战》笔记-Happens-Before规则

    Happens-Before规则 程序顺序规则.如果程序中操作A在操作B之前,那么在线程中A操作将在B操作之前执行. 监视器锁规则.在监视器锁上的解锁操作必须在同一个监视器锁上的加锁操作之前执行. v ...

  2. 学习笔记之Matplotlib

    Matplotlib: Python plotting — Matplotlib 2.2.2 documentation https://matplotlib.org/ Matplotlib is a ...

  3. 廖雪峰Java1-3流程控制-1输入输出

    1.输入 导入java.util.Scanner 创建Scanner对象并传入System.in 使用Scanner.nextLine()读取用户输入的字符串 Scanner.nextInt()读取用 ...

  4. springboot+dubbo+tomcat部署出错问题

    刚用springboot+dubbo有点不熟悉,部署的时候出现了问题 1 The APR based Apache Tomcat Native library which allows optimal ...

  5. tesseract 训练

    下载chi_sim.traindata字库下载tesseract-ocr-setup-3.02.02.exe 下载地址:http://code.google.com/p/tesseract-ocr/d ...

  6. [UE4]虚幻4链接独立服务器

    如果虚幻4只做客户端的话,应该怎么连接服务器呢? 官方并没有提供蓝图的网络函数,C++里面有. 一.自己实现,提供接口给蓝图使用. 二.第三方插件.插件下载地址:https://github.com/ ...

  7. [UE4]蓝图继承方法:重写父类方法时,增加父类方法调用

    包括构造函数也可以调用父类方法 事件也可以调用父级的事件

  8. header头参数不能带下划线

    header头参数不能带下划线:game_id是错误的

  9. Android ImageView点击效果

    ImageView设置点击效果需要注意两点,第一个设置android:clickable="true",第二个 <item android:drawable="@d ...

  10. 使用Redis-Dump 导出、导入redis数据

    一.安装ruby https://www.cnblogs.com/EikiXu/p/9406707.html 二.安装redis-dump工具 yum install ruby rubygems ru ...