标签:instanceof 继承 Father System 多态 println Son out
多态
多态是什么
-
同一个方法可以根据发送对象的不同从而采用不同的行为方式
-
一个对象的类型是确定的,但是可指向对象的引用类型有很多
多态的注意点
-
多态可以是动态编译的
-
多态是方法的多态,属性没有多态
-
类型转换要有父子关系,兄弟关系是不行的
-
多态的前提是继承,且方法要重写(无法重写的方法不可能实现多态:static、final、private)
-
父类的引用指向子类的对象
Father s1=new Son();
package com.oo.oop.polymorphic;
public class Father {
public void run()
{
System.out.println("条条大陆通罗马");
}
}
package com.oo.oop.polymorphic;
public class Son extends Father {
@Override
public void run() {
System.out.println("样样精通");
}
public void play(){
System.out.println("想玩");
}
}
package com.oo;
import com.oo.oop.polymorphic.Father;
import com.oo.oop.polymorphic.Son;
// 一个项目应该只有一个main()方法
public class Application {
public static void main(String[] args) {
//一个对象的实现类型是确定的
new Son();
new Father();
//可以指向大的引用类型是任意父类型;父类型的引用指向子类
//Son是子类,可以调用自己或者继承来的方法
Son s1 = new Son();
//Father是父类,可以指向子类,但是不能直接调用子类独有的方法
Father s2 = new Son();
Object s3 = new Son();
s2.run();//子类重写了父类的方法,执行了子类的方法
s1.run();
//父类没有的方法,没有办法直接调用
//对象能指向哪些方法,主要看对象左边的类型,和右边关系不大
((Son)s2).play();
}
}
Instanceof
-
instanceof关键字,编译看左,运行看右
-
//System.out.println(X instanceof Y);
/*
编译能不能通过主要看X与Y之间是否存在父子关系(或者是本身)
运行的结果看的是X所指向的实际类型与Y是否存在父子关系(或者是本身)
*/
-
-
instanceof判断两个类之间是否存在父子关系
package com.oo;
import com.oo.oop.polymorphic.Daughter;
import com.oo.oop.polymorphic.Father;
import com.oo.oop.polymorphic.Son;
// 一个项目应该只有一个main()方法
public class Application {
public static void main(String[] args) {
//编译看左,运行看右
//System.out.println(X instanceof Y);
/*
编译能不能通过主要看X与Y之间是否存在父子关系(或者是本身)
运行的结果看的是X所指向的实际类型与Y是否存在父子关系(或者是本身)
*/
Object s1 = new Son();
System.out.println(s1 instanceof Son); //true
System.out.println(s1 instanceof Father); //true
System.out.println(s1 instanceof Object); //true
System.out.println(s1 instanceof Daughter); //false
System.out.println(s1 instanceof String); //false
System.out.println("----------------------------");
Father s2 = new Son();
System.out.println(s2 instanceof Son); //true
System.out.println(s2 instanceof Father); //true
System.out.println(s2 instanceof Object); //true
System.out.println(s2 instanceof Daughter); //false
//System.out.println(s2 instanceof String); //编译不通过(Father和String没有父子关系编译报错)
System.out.println("---------------------------");
Son s3 = new Son();
System.out.println(s3 instanceof Son); //true
System.out.println(s3 instanceof Father); //true
System.out.println(s3 instanceof Object); //true
//System.out.println(s3 instanceof Daughter); //编译不通过(Son和Daughter没有父子关系编译报错)
//System.out.println(s2 instanceof String); //编译不通过(Son和String没有父子关系编译报错)
}
}
-
子类转换父类,直接转换,但是可能会丢失方法
-
父类转化子类,强制转换,
-
转换是为了方便方法的调用,减少重复代码
package com.oo;
import com.oo.oop.polymorphic.Daughter;
import com.oo.oop.polymorphic.Father;
import com.oo.oop.polymorphic.Son;
// 一个项目应该只有一个main()方法
public class Application {
public static void main(String[] args) {
//类型之间的转换,
//高 低
Father s1=new Son();
//将s1这个对象转换为Son类型(现在是Father类型的)就可以使用Son类型的方法
//高转低要强制转换
((Son) s1).play();
//同理操作
//Son son=(Son) s1;
//son.play();
Son s2 = new Son();
s2.play();
//低转高 可能会丢失一些自己本来的方法
Father f1=s2;
((Son) f1).play();
}
}
package com.oo.oop.polymorphic;
public class Father {
public void run(){
System.out.println("条条大路通罗马");
}
}
package com.oo.oop.polymorphic;
public class Son extends Father {
public void play(){
System.out.println("我想玩");
}
}
标签:instanceof,继承,Father,System,多态,println,Son,out 来源: https://www.cnblogs.com/Share-my-life/p/14730002.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。