Java学习(十) 多态

多态

 

1. 多态的特性

(1)当父类和子类具有相同的成员变量,那么在多态下访问的是父类的成员变量;

(2)当父类和子类具有相同的非静态方法(就是子类重写父类方法),多态下访问的是子类的成员方法;

(3)当父类和子类具有相同的静态成员法(就是子类重写父类静态方法),多态下访问的是父类的静态方法;

 

2. 多态的体现

(1)父类引用变量指向了子类的对象;

(2)父类引用也可以接受自己的子类对象;

 

3. 多态的总结

(1)多态的前提:类与类之间有关系,继承或者实现;

(2)多态的弊端:提高扩展性,但是只能是使用父类引用指向父类成员;

(3)多态在编译时期,参考引用型变量所属的类是否有调用的方法,如果有编译通过,没有则编译失败;

(4)多态在运行时期,参考对象所属的类是否有调用方法;

(5)成员行数在多态调用时,编译看左边,运行看右边;

 

4. 多态练习

 

(1)多态作为形参,接受范围更广的对象,避免函数重载过度使用

 

     描述:定义功能,根据输出任何图形的面积和周长;

abstract class MyShape{
	public abstract double getArea();
	public abstract double getLen();
}

class  Rect extends MyShape{
	double width ;
	double height;

	Rect(){

	}

	Rect(double width ,double height){
		this.width=width;
		this.height=height;
	}

	public double getArea(){
		return width*height;
	}

	public  double getLen(){
		return 2*(width+height);
	}
	
}
class Circle extends MyShape{
	double r;
	public static final double PI=3.14;
	 
	Circle(){
	 
	}
	 
	Circle(double r){
	 	this.r=r;
	}

	public double getLen(){
	 	return 2*PI*r;
	}
	
	public double getArea(){
	 	return PI*r*r;
	}
}

class Main{
	public static void main(String[] args){
		System.out.println();
		print(new Rect(3,4)); //MyShape m =new Rect(3,4);
		print(new Circle(3));
     }
     
     //根据用户传入的图形对象,计算出该图形的面积和周长
     //1:多态可以作为形参,接受范围更广的对象,避免函数重载过度使用。 
     public static void print(MyShape m){  
     	System.out.println(m.getLen());
     	System.out.println(m.getArea());
     }   	
}

(2)作为返回值类型

 

描述:获取任意一辆车对象,定义汽车类,有名字和颜色,提供有参合无参构造,有运行的行为;

class Car {
	String name;
	String color;

	Car() {

	}

	Car(String name, String color) {
		this.name = name;
		this.color = color;
	}

	void run() {
		System.out.println("跑跑。。。。");
	}
}

class Bmw extends Car {
	Bmw() {

	}

	Bmw(String name, String color) {
		super(name, color);
	}

	void run() {
		System.out.println("宝马很拉风。。。。");
	}
}

class Benz extends Car {
	Benz() {

	}

	Benz(String name, String color) {
		super(name, color);
	}

	void run() {
		System.out.println("奔驰商务首选。。。。");
	}
}

class Bsj extends Car {

	Bsj() {

	}

	Bsj(String name, String color) {
		super(name, color);
	}

	void run() {
		System.out.println("泡妞首选。。。。");
	}
}

class Main {
	public static void main(String[] args) {
		int x = 0;
		while (x < 100) {
			Car c = CarFactory();
			c.run();
			x++;
		}
	}

	// 定义静态方法,汽车工厂,随机生产汽车。使用多态定义方法返回值类型。
	// 使用随机数,0.1.2 if 0 bsj 1 bmw 2 bc
	public static Car CarFactory() {
		int x = (int) Math.round(Math.random() * 2);
		if (0 == x) {
			return new Bmw("宝马x6", "红色");
		} else if (1 == x) {
			return new Benz("奔驰", "黑色");
		} else if (2 == x) {
			return new Bsj("保时捷", "棕色");
		} else {
			return new Benz("Smart", "红色");
		}
	}
}

 

(3)多态类型转型

 

类类型转换规则:

①自动:  子类转父类

②强转:  父类转子类

class Father {

	void method1() {
		System.out.println("这是父类1");
	}

	void method2() {
		System.out.println("这是父类2");
	}
}

class Son extends Father {
	void method1() {
		System.out.println("这是子类1");
	}

	void method3() {
		System.out.println("这是子类3");
	}
}

class Main {
	public static void main(String[] args) {
		Father f = new Son();
		f.method1(); // 这是子类1
		f.method2(); // 这是父类2

		// f.method3(); //编译报错。
		// 多态弊端,只能使用父类引用指向父类成员。

		// 类类型转换
		Son s = (Son) f;
		s.method3();
		System.out.println();
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值