现在的位置: 首页 > 综合 > 正文

关于抽象类 与Polymorphism ⑦

2018年04月10日 ⁄ 综合 ⁄ 共 645字 ⁄ 字号 评论关闭

 

public class Test2
{
 public static void main(String[] args)
 {
  Shape shape = new Triangle(10,6);
  int area = shape.computerArea();
  System.out.println("triangle:"+area);
  shape = new Rectangle(10,10);
  area = shape.computerArea();
  System.out.println("rectangle:"+area);
 }
}
abstract class shape
{
 public abstract int computeArea();//计算形状面积
}
class Triangle extends shape
{
 int width;
 int height;
 public Triangle(int width,int height)
 {
  this.width = width;
  this.height = height;
 }
 public int computeArea()
 {
  return (width *height)/2;
 }
}
class Rectangle extends Shape
{
 int wigth;
 int height;

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

 }
 public int computeArea()

抱歉!评论已关闭.