java 父类引用指向子类对象---动态绑定
知识点:
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 父类引用指向子类对象---动态绑定的更多相关文章
- Java多态-如何理解父类引用指向子类对象
java多态,如何理解父类引用指向子类对象 要理解多态性,首先要知道什么是“向上转型”. 我定义了一个子类Cat,它继承了Animal类,那么后者就是前者是父类.我可以通过 Cat c = new ...
- java多态性,父类引用指向子类对象
父类引用指向子类对象指的是: 例如父类Animal,子类Cat,Dog.其中Animal可以是类也可以是接口,Cat和Dog是继承或实现Animal的子类. Animal animal = new C ...
- Java多态 父类引用指向子类对象
Java多态的三个必要条件: 1. 继承 2. 子类重写父类方法 3. 父类引用指向子类对象 然后看一个例子 输出结果为: 给出结论:Father c = new Child() 在c的 ...
- java-多态中成员访问特点-父类引用指向子类对象
多态前提: - 要有继承关系. - 要有方法重写. - 要有父类引用指向子类对象. 1.成员变量:编译看左边(父类),运行看左边(父类) 2.成员方法:编译看左边(父类),运行看右边(子类),动态绑定 ...
- 向上转型---父类引用指向子类对象 A a = New B()的使用
一.向上转型 向上转型是JAVA中的一种调用方式,是多态的一种表现.向上转型并非是将B自动向上转型为A的对象,相反它是从另一种角度去理解向上两字的:它是对A的对象的方法的扩充,即A的对象可访问B从A中 ...
- C#多态;父类引用指向子类对象;new和override的区别;new、abstract、virtual、override,sealed关键字区别和使用代码示例;c#类的初始化顺序
关于父类引用指向子类对象 例如: 有以下2个类 public class Father { public int age = 70; public static string name = " ...
- 28 面向对象编程 instanceof 代码 小结 父类引用指向子类对象
instanceof 代码 // main // Object > Person >Student Object object = new Student(); // 提取公式:XY之间是 ...
- JAVA基础复习与总结<一>(2) 父类引用指向子类对象(向上转型、动态链接)
先来看看下列代码 public class Animal { public static void main(String[] args){ Animal animal = new Cat(); // ...
- C++析构函数的自动调用(用于父类指针指向子类对象,内存泄漏问题)
class A {public:A() { printf("A \n"); }~A() { printf(" ~A \n"); } // 这里不管写不写virt ...
随机推荐
- C语言中sizeof与strlen的区别
1.sizeof sizeof为编译时期被替换,不会等到程序运行再来判断,所以sizeof返回的是数组的总字节数 #include<stdio.h> int main() { ]={'a' ...
- HDOJ 2020 绝对值排序
#include<iostream> #include<cmath> #include<algorithm> #include<vector> usin ...
- [UE4]背景模糊
被遮挡的都会被模糊,没被遮挡的不会模糊
- Laravel 5.5 FormRequest 自定义错误消息 postman调试时X-Requested-With设为XMLHttpRequest
Laravel 5.5 FormRequest 自定义错误消息 使用FormRequest进行表单验证,就不用让验证逻辑和控制器里面的逻辑都混在一起.但在使用的时候呢,发现json错误返回的数据,与我 ...
- ie-table不显示边框解决办法
.thisTd { background-clip: padding-box; position:relative; } 原来背景也有边界的:决定背景会盖住哪些部 ...
- 02-创建String对象
创建一个String对象实在是太简单了,就是因为简单,所以有很多java程序员做了好几年的开发,也没有注意这些小细节问题 String字符串的本质就是char数据对象, 那么char[0]数组当中的一 ...
- Spark2.0.0源码编译
Hive默认使用MapReduce作为执行引擎,即Hive on mr,Hive还可以使用Tez和Spark作为其执行引擎,分别为Hive on Tez和Hive on Spark.由于MapRedu ...
- DB通用类:Access通用类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...
- IIS 修改并发连接数
http://www.cnblogs.com/dudumao/p/4078687.html
- 3-安装hive
1.解压.修改权限 tar -zvxf apache-hive-1.2.1-bin.tar.gz -C /opt/app/ sudo chown -R hadoop:hadoop /opt/app/a ...