对多态类的初步运用

package 多态类运用;
 
abstract class MyShape{
  public abstract void getArea();
  public abstract void getLength(); 
}
class Circla extends MyShape{
 public static final double pi = 3.14;
 double r;
 public Circla(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 heith;
  public Rect(int width,int heith) {
   this.width=width;
   this.heith=heith;
  }
  public void getArea() {
   System.out.println("矩形的面积是:"+width*heith);
  }
  public void getLength() {
   System.out.println("矩形的周长是:"+2*width+2*heith);
  }
}
 

public class Dome9 {
  public static void main(String[]args) {
   MyShape c1 = new Circla(4.0);
   c1.getArea();
   c1.getLength();
   MyShape r1 = new Rect(3,4);
   r1.getArea();
   r1.getLength();
  }
}

猜你喜欢

转载自www.cnblogs.com/0929-luoyang/p/10873222.html
0条评论
添加一条新回复
  
今日推荐