Java的多态-进阶
2024-09-02 14:22:23
Java的多态——进阶
总括
Parent p = new Child();
反之,Child() c = new Parent(); 会报错。
当使用多态方式调用方法时,首先检查父类中是否有该方法。
如果没有,则编译错误;
如果有,再去调用子类的同名方法。
下面例子中的p3就是多态。 Person p3 = new Student(); 其中,Person是父类,Student是子类。
举个例子1——静态属性、非静态属性、静态方法
静态属性、非静态属性、静态方法(这三个,对多态调用而言没区别,因为多态仅针对非静态方法(可重写)):
1. 非多态时(即等号左右两侧类型一致),p1和p2,调用的就是类自己的属性和方法。
2. 多态时(即等号左侧为父类,等号右侧为子类),p3,调用的就是父类的属性和方法。
1、main()方法
package com.WZ.Demo.Application;
import com.WZ.Demo.ClassSet.Person;
import com.WZ.Demo.ClassSet.Student;
public class Application {
public static void main(String[] args) {
Person p1 = new Person();
Student p2 = new Student();
Person p3 = new Student();
//Student p4 = new Person(); //编译报错
//测试非静态属性
System.out.println(p1.num); //1
System.out.println(p2.num); //3
System.out.println(p3.num); //1
//测试静态属性
System.out.println(p1.num); //1
System.out.println(p2.num); //3
System.out.println(p3.num); //1
//测试静态方法
p1.test2(); //Person
p2.test2(); //Student
p3.test2(); //Person
}
}
2、Person类
package com.WZ.Demo.ClassSet;
public class Person {
public int num = 1;
public static int num2 = 1;
public static void test2()
{
System.out.println("Person");
}
}
3、Student类
package com.WZ.Demo.ClassSet;
public class Student extends Person {
public int num = 3;
public static int num2 = 3;
public static void test2()
{
System.out.println("Student");
}
}
举个例子2——非静态方法
非静态方法(对多态调用而言有区别,因为多态是针对非静态方法(可重写)的):
非多态时(即等号左右两侧类型一致),p1和p2,调用的就是类自己的非静态方法。
多态时(即等号左侧为父类,等号右侧为子类),p3,时, 若子类有与父类同名的非静态方法,就调用子类的非静态方法(即方法重写,多态)。
1、main()方法
package com.WZ.Demo.Application;
import com.WZ.Demo.ClassSet.Person;
import com.WZ.Demo.ClassSet.Student;
public class Application {
public static void main(String[] args) {
Person p1 = new Person();
Student p2 = new Student();
Person p3 = new Student();
//Student p4 = new Person(); //编译报错
//测试非静态方法
p1.test(); //Person
p2.test(); //Student
p3.test(); //Student
}
}
2、Person类
package com.WZ.Demo.ClassSet;
public class Person {
public void test()
{
System.out.println("Person");
}
}
3、Student类
package com.WZ.Demo.ClassSet;
public class Student extends Person {
public void test()
{
System.out.println("Student");
}
}
举个例子3——非静态方法和静态方法时,父类有方法,子类没有
多态的时候:
父类有方法,子类没有,则用父类的方法;
若父类也没该方法,那就报错;
若子类有该同名的方法,则用子类的,即多态了。
1、main()方法
package com.WZ.Demo.Application;
import com.WZ.Demo.ClassSet.Person;
import com.WZ.Demo.ClassSet.Student;
public class Application {
public static void main(String[] args) {
Person p1 = new Person();
Student p2 = new Student();
Person p3 = new Student();
//Student p4 = new Person(); //编译报错
//测试非静态方法
p1.test(); //Person
p2.test(); //Person
p3.test(); //Person
//测试静态方法
p1.test2(); //Person
p2.test2(); //Person
p3.test2(); //Person
}
}
2、Person类
package com.WZ.Demo.ClassSet;
public class Person {
public void test()
{
System.out.println("Person");
}
public static void test2()
{
System.out.println("Person");
}
}
3、Student类
package com.WZ.Demo.ClassSet;
public class Student extends Person { }
最新文章
- Iterator用法
- try-catch 示例
- HDU 4036 存疑题目,数论 难度:1
- 4-3 yum命令
- 初识jQuery 2013-09-26
- Linker scripts之Intro
- windows下查看某个端口被哪个程序占用的方法
- Android消息推送完美解决方案全析
- 使用zipalign.exe工具优化APK安装包
- about Q&;A in installing linux[centos6,7]
- [C#]异步委托使用小计
- android中的数据库操作(转)
- Ppthon基础学习之Dict
- JS计算字符串长度(中文算2个)
- js的搜索遍历精讲
- cxf的使用以及与SSH的整合
- java.lang.ClassCastException: net.sf.json.JSONNull cannot be cast to net.sf.json.JSONObject的解决方法
- C#常忘语法笔记(C#程序设计基础1-4章)
- 如何开启win10的上帝模式
- Zephyr的Time、Timer、sleep
热门文章
- C# 工具类LogHelper
- C#调用Crypto++库AES ECB CBC加解密
- vue 项目
- Codeforces Round #592 (Div. 2) D - Paint the Tree
- hdu 1087 Super Jumping!(类最长上升子序列)
- 精简Command版SqlHelper
- Dart单例模式最佳实践
- 题解【洛谷P1896】[SCOI2005]互不侵犯
- Mysql-从库只读设置
- Switch The LED Holiday Light To Illuminate The Cheerful Holidays