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();//抽象方法 子类
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值