1.抽象类
public abstract class AbstarctTest
{
}
2.抽象方法
public class Test
{
public static void main(String[] args)
{
}
}
abstract class T
{
public abstract void method();//抽象方法,有声明无实现
public void test()//抽象类中可包含具体方法
{
System.out.println("test");
}
}
class R extends T //继承父类(抽象类)所有的抽象方法;否则,子类也要声明成一个abstract class
{
public void method()
{
System.out.println("method");
}
}
3.抽象类作用
public class Test2
{
public static void main(String[] args)
{
Shape shape = new Triangle(10,6);
int area = shape.computeArea();
System.out.println("triangle:" + area);
shape = new Rectangle(10,10);
area = shape.computeArea();
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 width;
int height;
public Rectangle(int width, int height)
{
this.width = width;
this.height = height;
}
public int computeArea()
{
return width * height;
}
}