java 多态
2017-11-07 本文已影响0人
谁吃了我的薯条
多态的作用主要是类型之间的耦合关系,又被称为动态绑定,后期绑定或者运行时绑定;
package com.Polymorphism;
public class Shapes {
public void draw(){
System.out.println("Shapes.draw()");
}
public void erase(){
System.out.println("Shapes.erase()");
}
public void plink(){
System.out.println("Shapes.plink()"+this.getClass());
}
}
package com.Polymorphism;
public class Square extends Shapes {
@Override
public void draw() {
System.out.println("Square.draw()");
}
@Override
public void erase() {
System.out.println("Square.erase()");
}
@Override
public void plink(){
System.out.println("Square.plink()"+this.getClass());
}
}
package com.Polymorphism;
public class Triangle extends Shapes {
@Override
public void draw() {
System.out.println("Triangle.draw()");
}
@Override
public void erase() {
System.out.println("Triangle.erase()");
}
@Override
public void plink(){
System.out.println("Triangle.plink()"+this.getClass());
}
}
package com.Polymorphism;
public class Circle extends Shapes {
@Override
public void draw() {
System.out.println("Circle.draw()");
}
@Override
public void erase() {
System.out.println("Circle.erase()");
}
@Override
public void plink(){
System.out.println("Circle.plink()"+this.getClass());
}
}
package com.Polymorphism;
import java.util.Random;
public class Polymorphism {
private Random random=new Random(47);
public Shapes next(){
switch ((random.nextInt(3))){
default:
case 0:
return new Circle();
case 1:
return new Square();
case 2:
return new Triangle();
}
}
}
package com.Polymorphism;
public class test {
private static Polymorphism polymorphism=new Polymorphism();
public static void main(String args[]){
Shapes[] shapes=new Shapes[12];
for(int i=0;i<shapes.length;i++){
shapes[i]=polymorphism.next();
}
for(Shapes shapes1:shapes){
shapes1.plink();
}
}
}
Triangle.plink()class com.Polymorphism.Triangle
Triangle.plink()class com.Polymorphism.Triangle
Square.plink()class com.Polymorphism.Square
Triangle.plink()class com.Polymorphism.Triangle
Square.plink()class com.Polymorphism.Square
Triangle.plink()class com.Polymorphism.Triangle
Square.plink()class com.Polymorphism.Square
Triangle.plink()class com.Polymorphism.Triangle
Circle.plink()class com.Polymorphism.Circle
Square.plink()class com.Polymorphism.Square
Circle.plink()class com.Polymorphism.Circle
Circle.plink()class com.Polymorphism.Circle
将一个方法调用预期方法体关联起来,称为方法调用;如果在运行前已经被绑定,则称为前期绑定。而对于一个Shapes
引用时,无法知道究竟调用那个方法,解决的办法就是后期绑定,也称为运行期绑定或者动态绑定。
注意,在私有方法被”覆盖“,由于私有方法被认为是,private
方法,故导出类,无法访问,也就无法被覆盖,只是拥有与基类相同的方法名而已;
静态方法,也不具备多态性;构造器,其实际上也是static
方法,也不存在多态性,但是要了解其在多态的复杂情况下的运行情况;
多态情况下的构造器运行为:
1)调用基类构造器,即首先是根基类的构造器,其次是基类的构造器;
2)其次,按照申明顺序调用成员的初始化方法;
3)调用导出类构造器的主体;
举例说明:
package com.Polymorphism;
class Meal{
Meal(){
System.out.println("Meal()");
}
}
class Chese{
Chese(){
System.out.println("Chese()");
}
}
class Lettuce{
Lettuce(){
System.out.println("Lettuce()");
}
}
class Lunch extends Meal{
Lunch(){
System.out.println("Lunch()");
}
}
class dinner extends Lunch{
dinner(){
System.out.println("dinner()");
}
}
public class se extends dinner{
private Chese chese=new Chese();
private Lettuce lettuce=new Lettuce();
public se(){
System.out.println("MEal()");
}
public static void main(String[] args){
new se();
}
}
Meal()
Lunch()
dinner()
Chese()
Lettuce()
MEal()
向上转型,会损失部分导出类的新方法,如果我们要访问这些方法,可以通过向下转型,但是如果类型转换存在错误,会抛出ClassCastException
异常;