📄 abstractsample.java
字号:
package chapter4;
/**
* 抽象类的使用举例
*/
abstract class Shape {
private static int n;
/** 抽象类中的抽象方法 */
abstract float getArea();
/** 抽象类中也可以包含非抽象的方法 */
public int getCount() {
return n++;
}
}
/**
* 由抽象类Shape派生的具体子类--圆类
*/
class Circle extends Shape {
public int r;
Circle(int r) {
this.r=r;
}
public float getArea() {
return 3.14f*r*r;
}
}
/**
* 由抽象类Shape派生的具体子类--矩形类
*/
class Rectangle extends Shape {
public int width,height;
Rectangle(int w, int h) {
width=w;
height=h;
}
public float getArea() {
return width*height;
}
}
public class AbstractSample {
public static void main(String[] args) {
Shape s1 = new Circle(2);
Shape s2 = new Rectangle(2,3);
float area1 = s1.getArea();
float area2 = s2.getArea();
System.out.println("Circle's Area="+area1);
System.out.println("Rectangle's Area="+area2);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -