首页 > 编程语言 > 详细

Java中的多态

时间:2020-02-17 13:25:35      阅读:68      评论:0      收藏:0      [点我收藏+]

一.定义

  超类对象和从超类中派生出来的子类对象,或者实现同一接口的不同类型的对象,都可以被当作同一种类型的对象看待。Java中的多态性是指在超类中定义的属性或者方法被子类继承后,可以具有不同的数据类型或者不同的行为。

 

二.早绑定和晚绑定

  绑定的定义为将接口和对象实例结合在一起的方法。根据绑定时间的不同可分为:

1.早绑定:在程序运行之前就进行绑定,即在实例化对象之前定义它的特性和方法,这样编译器或解释器程序就能提前转换机器代码。

 

2.晚绑定:编译器或解释器程序在运行前,不知道对象的类型,需要在程序运行时才能进行绑定。

 

//超类Share建立了一个通用接口
class Shape{
	void draw() {}
	void erase() {}
}
//子类覆盖了draw方法,为每种特殊的几何形状都提供独一无二的行为
class Circle extends Shape{
	void draw(){
		System.out.println("Circle.draw()");
	}
	void erase(){
		System.out.println("Circle.erase()");
	}
}

class Square extends Shape{
	void draw(){
		System.out.println("Square.draw()");
	}
	void erase(){
		System.out.println("Square.erase()");
	}
}

class Triangle extends Shape{
		void draw(){
		System.out.println("Triangle.draw()");
	}
	void erase(){
		System.out.println("Triangle.erase()");
	}
}

public class BindingTester{
	public static void main(String[] args){
		Shape[] s = new Shape[9];
		int n;
		for(int i=0;i<s.length;i++){
			n = (int)(Math.random()*3);
			switch(n){
				case 0: s[i] = new Circle(); break;
				case 1: s[i] = new Square(); break;
				case 2: s[i] = new Triangle();
			}
		}
		for(int i=0;i<s.length;i++){
			s[i].draw();
		}
	}
}
//编译时无法知道s数组元素指向的实际对象类型,运行是才能确定类型

 

 

三.构造方法和多态

  当需要构造子类方法时,构造方法需要先调用潮涌超类的构造方法,然后才会执行当前子类的构造方法体和其他语句。

public class Point{
	private double xCoordinate;
	private double yCoordinate;
	public Point(){}
	public Point(double x,double y){
		xCoordinate = x;
		yCoordinate = y;
	}
	public String toString(){
		return "("+Double.toString(xCoordinate)+","+
			Double.toString(yCoordinate)+")";
	}
}

public class Ball{
	private Point center;//中心点
	private double radius;
	private String color;
	public Ball(){}
	public Ball(double xValue,double yValue,double r){
		center = new Point(xValue,yValue);
		radius = r;
	}
	public Ball(double xValue,double yValue,double r,String c){
		this(xValue,yValue,r);
		color = c;
	}
	public String toString(){
		return "A ball with center" + center.toString() +", radius"
			+ Double.toString(radius)+",color" + color;
	}
}

public class MovingBall extends Ball{
	private double speed;
	public MovingBall(){}
	public MovingBall(double xValue,double yValue,double r,String c,double s){
		super(xValue,yValue,r,c);
		speed = s;
	}
	public String toString(){
	//super.toString调用父类Ball的toString()输出Ball中声明的属性值
		return super.toString()+",speed"+Double.toString(speed);
	}
}

public class Tester{
	public static void main(String[] args){
		MovingBall mb = new MovingBall(10,20,40,"green",25);
		System.out.println(mb);
	}
}

//运行结果
A ball with center(10.0,20.0),radius 40.0,color green,speed 25.0

 其中调用顺序为:MovingBall(double xValue,double yValue,double r,String c,double s) -> Ball(double xValue,double yValue,double r,String c) -> Ball(double xValue, double yValue,double r) -> Point(double x,double y) 

  

  

Java中的多态

原文:https://www.cnblogs.com/thwyc/p/12318883.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!