java中的抽象方法 抽象类 模板

抽象方法:
目的: 使用abstract修饰的方法 无方法体 子类中必须覆盖,其修饰符不能是 final static private
抽象方法存在于抽象类或者接口中
抽象类:
使用abstract修饰的
a 不能创建对象 (即使创建了抽象类 调用抽象方法无意义 )
b 抽象类可以不包含抽象方法 抽象类是有构造器的
c 抽象类必须有子类 其修饰符不能是 final static private
d 起名一般使用Abstract为前缀
有些工具类也是抽象类 防止外界创建对象
模板:
指做事的步骤差不多
模板方法设计是指 在父类中 动议一个总体算法的骨架,而将某一些不走延迟到子类中,因为不同的子类而不同的实现细节 可以在不改变算法结构的情况下重新定义算法中的算法步骤
子类不能改变算法结构 加final修饰
父类的抽象方法加 private doxx();只能继承

。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

abstract class Graph
{
public abstract Double getArea();//抽象方法

}

class Circle extends Graph
{
private Integer r;
Circle(Integer r)
{
this.r=r;
}
public Double getArea()
{
return 3.14*r*r;
}

}

class RectangleD extends Graph
{
private Integer high;
private Integer wide;
RectangleD(Integer high,Integer wide)
{
this.high=high;
this.wide=wide;
}
public Double getArea()
{
return high.doubleValue()*wide.doubleValue();
}

}

class GraphDemo
{
public static void main(String[] args)
{
System.out.println(new Circle(10).getArea());
}
}

abstract class TemplateDemo
{
public final void getHello()
{
//共同步骤
//子类的的不同步骤
doooxx();
//共同步骤
}
abstract private void doooxx();//抽象方法 子类
}

猜你喜欢

转载自blog.csdn.net/everythingxhd_/article/details/80112821