Java定义抽象类

抽象类简介

在Java语言中,可以通过把类或者类的某些方法声明为abstract(abstract只能用来修饰类或者方法,不能用来修饰属性)来表示一个类是抽象类。抽象类不能被实例化

举例

定义一个抽象类,作为父类,定义两个抽象方法,后期调用,这里以圆形和矩形为例

代码

package pro01;
abstract class Geometric{
  String color = "block";
  int weight =2;
  abstract float getArea();
  abstract float getPerimeter();
}

class Circle extends Geometric{
  float radius;
  Circle(float number){
    radius = number;
  }
  protected float getArea(){
    return 3.14f*radius*radius;
  }
  protected float getPerimeter(){
    return 2*3.14f*radius;
  }
}

class Rectangle extends Geometric{
  float width;
  float height;
  Rectangle(float width,float height){
    this.width=width;
    this.height=height;
  }  
  float getArea(){
    return width*height;
  float getPerimeter(){
    return 2*(width*height);
  }
}

public class LoginCheck{
  public static void main(Str
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值