多态(Polymorphism)为Java面向对象的三大特性之一(另外两个为继承Inheritance和封装Encapsulation)
多态在JAVA 官方帮助文档中的定义:
Subclasses of a class can define their own unique behaviors and yet share some of the same functionality of the parent class.
翻译:子类可以在实现父类的部分功能同时,还可以定义一些自己独特的方法。
一、多态存在的必要条件有三个:
1、继承
2、重写
3、父类引用指向子类
二、多态的实现方式也有三个:
1、接口
2、继承重写
class desktop{
public void move(){
System.out.println("笔记本一般比较便携");
}
}
class workstation extends desktop{
public void move(){
System.out.println("便携工作站是笔记本,但并不便携");
}
}
3、方法重载
public String person(int age){
System.out.println("test3");
return "他14岁了";
}
public String person(String hobbies){
System.out.println("test4");
return "方法重载:他的爱好是踢球";
}