java的多态性是面向对象的三大特性之一.(封装,继承,多态),多态是指程序中定义的引用变量所指向的具体类型和通过该引用变量发出的方法调用在编程时并不确定,而是在程序运行期间才确定,即一个引用变量倒底会指向哪个类的实例对象,该引用变量发出的方法调用到底是哪个类中实现的方法,必须在由程序运行期间才能决定。因为在程序运行时才确定具体的类,这样,不用修改源程序代码,就可以让引用变量绑定到各种不同的类实现上,从而导致该引用调用的具体方法随之改变,即不修改程序代码就可以改变程序运行时所绑定的具体代码,
让程序可以选择多个运行状态,这就是多态性。多态性增强了软件的灵活性和扩展性。
java多态分为:
1.方法的重载与重写。
重载:同类中返回值和函数名相同,而参数列表不同的两个方法。
重写:是子类的方法覆盖父类的方法,要求方法名和参数都相同
2.对象的多态性
在这里我们主要介绍对象的多态性
我自己的理解是小转大自动转换,大转小需要强制转换。父类大于子类。
(1)父类引用指向子类实例
public class Mystring {
<br style="word-wrap:break-word">
public static void main(String[] args) { <br style="word-wrap:break-word">
<br style="word-wrap:break-word">
//父类大,子类小,小转大自动转换,只能调用父类也有的方法 <br style="word-wrap:break-word">
person temp=new student();//父类的引用指向子类对象 <br style="word-wrap:break-word">
temp.say(); <br style="word-wrap:break-word">
<br style="word-wrap:break-word">
person temp1=new teacher(); <br style="word-wrap:break-word">
temp1.say(); <br style="word-wrap:break-word">
} <br style="word-wrap:break-word">
}
//抽象类
abstract class person
{
public String name; <br style="word-wrap:break-word">
<br style="word-wrap:break-word">
<br style="word-wrap:break-word">
public String getName() { <br style="word-wrap:break-word">
return name; <br style="word-wrap:break-word">
} <br style="word-wrap:break-word">
public void setName(String name) { <br style="word-wrap:break-word">
this.name = name; <br style="word-wrap:break-word">
} <br style="word-wrap:break-word">
public abstract void say();//抽象方法 <br style="word-wrap:break-word">
}
//学生类
class student extends person//继承person类
{
//实现抽象方法 <br style="word-wrap:break-word">
public void say() <br style="word-wrap:break-word">
{ <br style="word-wrap:break-word">
System.out.println("我是学生。。。。"); <br style="word-wrap:break-word">
<br style="word-wrap:break-word">
} <br style="word-wrap:break-word">
}
//老师类
class teacher extends person//继承person类
{
public void say() <br style="word-wrap:break-word">
{ <br style="word-wrap:break-word">
System.out.println("我是老师。。。。"); <br style="word-wrap:break-word">
<br style="word-wrap:break-word">
} <br style="word-wrap:break-word">
//扩展方法 <br style="word-wrap:break-word">
public void sayHello() <br style="word-wrap:break-word">
{ <br style="word-wrap:break-word">
System.out.println("Hello,我是老师。。。。"); <br style="word-wrap:break-word">
} <br style="word-wrap:break-word">
}
复制代码
(2)子类对象指向父类引用
public class Mystring {
<br style="word-wrap:break-word">
public static void main(String[] args) { <br style="word-wrap:break-word">
<br style="word-wrap:break-word">
//父类大,子类小,自动转换,只能调用父类也有的方法 <br style="word-wrap:break-word">
person temp=new student();//父类的引用指向子类对象 <br style="word-wrap:break-word">
temp.say(); <br style="word-wrap:break-word">
<br style="word-wrap:break-word">
person temp1=new teacher(); <br style="word-wrap:break-word">
temp1.say(); <br style="word-wrap:break-word">
//------------调用teacher的扩展方法sayHello() <br style="word-wrap:break-word">
//父类person没有sayHello方法,不能直接temp1.sayHello() <br style="word-wrap:break-word">
<br style="word-wrap:break-word">
//子类小于父类,需要强制转换 <br style="word-wrap:break-word">
teacher temp3=(teacher)temp1; <br style="word-wrap:break-word">
temp3.sayHello(); <br style="word-wrap:break-word">
<br style="word-wrap:break-word">
} <br style="word-wrap:break-word">
}
//省略类的定义
复制代码
执行结果:
我觉得记住小转大自动转换,大转小需要强制转换。父类大于子类就很好理解
既然父类可以自动转化为子类,那我们可以利用这特性解决一个经常遇到问题
public class Mystring {
public static void main(String[] args) { <br style="word-wrap:break-word">
student temp=new student(); <br style="word-wrap:break-word">
say(temp); <br style="word-wrap:break-word">
teacher temp1=new teacher(); <br style="word-wrap:break-word">
say(temp1); <br style="word-wrap:break-word">
} <br style="word-wrap:break-word">
public static void say(student temp) <br style="word-wrap:break-word">
{ <br style="word-wrap:break-word">
temp.say(); <br style="word-wrap:break-word">
} <br style="word-wrap:break-word">
<br style="word-wrap:break-word">
public static void say(teacher temp1) <br style="word-wrap:break-word">
{ <br style="word-wrap:break-word">
temp1.say(); <br style="word-wrap:break-word">
} <br style="word-wrap:break-word">
//省略类的定义 <br style="word-wrap:break-word">
}
复制代码
我们发现say()由于需要的参数不同,所以重载了两份。这样写,要是类很多,那不要重载很多份。这里我们使用对象的多态就能很好的解决。把代码改为:
public class Mystring {
<br style="word-wrap:break-word">
public static void main(String[] args) { <br style="word-wrap:break-word">
<br style="word-wrap:break-word">
person temp=new student(); <br style="word-wrap:break-word">
say(temp); <br style="word-wrap:break-word">
<br style="word-wrap:break-word">
person temp1=new teacher(); <br style="word-wrap:break-word">
say(temp1); <br style="word-wrap:break-word">
<br style="word-wrap:break-word">
<br style="word-wrap:break-word">
} <br style="word-wrap:break-word">
public static void say(person temp) <br style="word-wrap:break-word">
{ <br style="word-wrap:break-word">
temp.say(); <br style="word-wrap:break-word">
} <br style="word-wrap:break-word">
//省略类的定义 <br style="word-wrap:break-word">
}
复制代码
当然这样写的话,调用的方法只能是父类也有的方法,这里能性是因为父类也有say()方法。为了解决这一问题,我们引入关键字instanceof。
instanceof的基本语法为:对象 instanceof 类型-----------返回boolean值
用来判断一个对象是否属于某和类的实例,是就返回true,否则返回false
下面我们在say()方法里调用teacher的扩展方法sayHello()。student和person类都没用该方法,上面代码肯定报错。这是我们可以将代码改为:
public class Mystring {
<br style="word-wrap:break-word">
public static void main(String[] args) { <br style="word-wrap:break-word">
<br style="word-wrap:break-word">
person temp=new student(); <br style="word-wrap:break-word">
say(temp); <br style="word-wrap:break-word">
<br style="word-wrap:break-word">
person temp1=new teacher(); <br style="word-wrap:break-word">
say(temp1); <br style="word-wrap:break-word">
<br style="word-wrap:break-word">
<br style="word-wrap:break-word">
} <br style="word-wrap:break-word">
public static void say(person temp) <br style="word-wrap:break-word">
{ <br style="word-wrap:break-word">
//如果是teacher对象就强制转换为teacher(大转小) <br style="word-wrap:break-word">
if(temp instanceof teacher) <br style="word-wrap:break-word">
{ <br style="word-wrap:break-word">
teacher ok=(teacher)temp; <br style="word-wrap:break-word">
ok.sayHello(); <br style="word-wrap:break-word">
} <br style="word-wrap:break-word">
<br style="word-wrap:break-word">
} <br style="word-wrap:break-word">
<br style="word-wrap:break-word">
<br style="word-wrap:break-word">
//省略类的定义 <br style="word-wrap:break-word">
}
复制代码
父类大转小自动转换,让我想起了所有类的父类Object类,它当然是最大的啦。所有我们代码当然还可以改为:
public class Mystring {
<br style="word-wrap:break-word">
public static void main(String[] args) { <br style="word-wrap:break-word">
<br style="word-wrap:break-word">
person temp=new student(); <br style="word-wrap:break-word">
say(temp); <br style="word-wrap:break-word">
<br style="word-wrap:break-word">
person temp1=new teacher(); <br style="word-wrap:break-word">
say(temp1); <br style="word-wrap:break-word">
<br style="word-wrap:break-word">
<br style="word-wrap:break-word">
} <br style="word-wrap:break-word">
//使用Object类 <br style="word-wrap:break-word">
public static void say(Object temp) <br style="word-wrap:break-word">
{ <br style="word-wrap:break-word">
//如果是teacher对象就强制转换为teacher(大转小) <br style="word-wrap:break-word">
if(temp instanceof teacher) <br style="word-wrap:break-word">
{ <br style="word-wrap:break-word">
teacher ok=(teacher)temp; <br style="word-wrap:break-word">
ok.sayHello(); <br style="word-wrap:break-word">
} <br style="word-wrap:break-word">
<br style="word-wrap:break-word">
} <br style="word-wrap:break-word">
<br style="word-wrap:break-word">
<br style="word-wrap:break-word">
//省略类的定义 <br style="word-wrap:break-word">
}
//抽象类
复制代码
这篇博客是对我学习java多态的一个总结,也拿出来和大家分享,有什么错误还望指正。