抽象方法、抽象类的用法

1、抽象方法:由abstract修饰(只有方法的定义),没有方法体的

2、抽象类:由abstract 修饰,可以有抽象方法和普通方法

3、包含抽象方法的类,必须是抽象类

4、抽象类不能被实例化,一般需要继承。

5、继承后1)、子类也声明为抽象类

2)、子类重写抽象类中的所有抽象方法

6、类中没有抽象方法。也可以将类声明抽象类


abstract class Shape1{

double c ;

abstract double area();


class Square1 extends Shape {

Square1(double c){

this.c = c;

}

double area(){

return 0.0625*c*c;

}

}


class Circle1 extends Shape{

Circle1(double c){

this.c = c;

}

double area(){

return 0.0796*c*c;

}

}

这是个小例子,最大数的面积

public static void main(String[] args) {

Shape[] shapes = new Shape[2];

shapes[0] = new Circle(2);

shapes[1] = new Square1(4);

max(shapes);

}

public static void max(Shape[] shapes){

double max = shapes[0].area();

int maxIndex = 0;

for(int i = 0;i<shapes.length;i++){

double area = shapes[i].area();

if(area>max){

maxIndex = i;

}

}                                                     

System.out.println("max:"+max);

}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值