通过多态生成对象和直接创建对象的区别
来源:互联网 发布:淘宝修改中差评步骤 编辑:程序博客网 时间:2024/05/22 05:34
这是父接口,写了两个test方法。
package com.wjc.Nio;/** * Created by wang.jiancheng on 2017/8/4. */interface InterParent { public void test1(); public void test2();}
这是子类,实现了父接口的方法后自己也写了个test方法。
package com.wjc.Nio;/** * Created by wang.jiancheng on 2017/8/4. */public class child implements InterParent{ public void test2() { System.out.println("我是子类重写父类方法的test2----"); } public void test1() { System.out.println("我是子类重写父类方法的test1----"); } public void test() { System.out.println("我是子类本身的test"); } public static void main(String[] args) { InterParent i = new child(); child c = new child(); }}随后便可以看到i这个对象只有InterParent的test1方法和test2方法,但是调用的是child的实现。
但是c这个对象不仅有test1方法和test2方法,还有自己本身的test方法。
总结:通过接口多态生成一个子类,生成的对象里只有父接口的方法,但是方法的实现的子类的。
但是直接通过本身创建自己的对象,就会有所有的方法。(好像是废话。。。)
阅读全文
1 0
- 通过多态生成对象和直接创建对象的区别
- 抽象类和接口作为形式参数 多态 抽象类和接口虽然都不能直接创建对象,但是可以通过多态进行对象的创建
- new创建对象和直接定义的区别 (
- new创建对象和直接定义的区别
- new创建对象和直接定义的区别
- 声明对象和创建对象的区别
- 87.直接向对象发送消息和通过performSelector调用的区别
- new对象和直接声明对象的区别
- 直接定义一个对象和new一个对象的区别
- new创建类对象与直接定义的区别
- Java多态的创建对象区别
- 如何通过反射来创建对象?getConstructor()和getDeclaredConstructor()区别?
- new创建对象和直接声明创建对象
- new创建对象和直接声明创建对象
- new创建对象和直接声明创建对象
- C++创建对象的方式和区别
- 用new方法生成一个img对象和document.createElement方法创建一个img对象的区别
- java中利用类名生成Class对象,通过class对象创建实体类的理解
- 利用命令行解决Tomcat启动时8080端口被占用的问题
- HTML语义化的理解
- String与StringBuilder与StringBuffer的对比
- angularjs $http 的post方法发送前准备返回404 解决方案
- Server responded "Algorithm negotiation failed"【SSH Secure链接服务器错误】解决办法
- 通过多态生成对象和直接创建对象的区别
- MyBatis实现增删改查
- 快速排序(演化版by数组)
- spring batch的使用和定时器Quart的使用
- 基于jstack的一次问题调查
- Super Jumping! Jumping! Jumping!
- UC/Gui和EmWin的区别:同与不同
- HDU 6073 Matching In Multiplication
- linux下与时间相关的结构体 struct timeb,struct timeval ,struct timezone,struct timespec,struct tm 等