码迷,mamicode.com
首页 > 其他好文 > 详细

多态的应用

时间:2017-01-10 23:32:36      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:abs   blog   ret   style   end   返回值   对象   gets   out   

/*
多态的应用:
  1. 多态用于形参类型的时候,可以接收更多类型的数据 。
  2. 多态用于返回值类型的时候,可以返回更多类型的数据。



多态的好处: 提高了代码的拓展性。

需求1: 定义一个函数可以接收任意类型的图形对象,并且打印图形面积与周长。
*/

//图形类
abstract class MyShape{

    public abstract void getArea();

    public abstract void getLength();    
}



class Circle extends MyShape{

    public static final double PI = 3.14;

    double r;

    public Circle(double r){
        this.r =r ;    
    }

    public  void getArea(){
        System.out.println("圆形的面积:"+ PI*r*r);
    }

    public  void getLength(){
        System.out.println("圆形的周长:"+ 2*PI*r);
    }
}


class Rect  extends MyShape{

    int width;

    int height;

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

    public  void getArea(){
        System.out.println("矩形的面积:"+ width*height);
    }

    public  void getLength(){
        System.out.println("矩形的周长:"+ 2*(width+height));
    }
}



class Demo12 {

    public static void main(String[] args) 
    {
        /*
        //System.out.println("Hello World!");
        Circle c = new Circle(4.0);
        print(c);

        Rect r = new Rect(3,4);
        print(r);
        */

        MyShape m = getShape(0); //调用了使用多态的方法,定义的变量类型要与返回值类型一致。
        m.getArea();
        m.getLength();
        

    }


    //需求1: 定义一个函数可以接收任意类型的图形对象,并且打印图形面积与周长。
    public static void print(MyShape s){ // MyShpe s = new Circle(4.0);
        s.getArea();
        s.getLength();
    }


    // 需求2: 定义一个函数可以返回任意类型的图形对象。
    public static MyShape  getShape(int i){
        if (i==0){
            return new Circle(4.0);
        }else{
            return new Rect(3,4);
        }
    }


}

 

多态的应用

标签:abs   blog   ret   style   end   返回值   对象   gets   out   

原文地址:http://www.cnblogs.com/xufengyuan/p/6271172.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!