关于抽象类 与Polymorphism ⑦

 

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()

你可能感兴趣的:(String,Class)