【程式語言】JAVA的第十堂課 |物件導向程式設計(抽象類別)
Published:
by .ch11.java
class ch11 {
public static void main(String[] aa) {
Coin c1=new Coin(1);
Coin c5=new Coin(5);
Coin c10=new Coin(10);
c1.show();
c5.show();
c10.show();
System.out.println(c10.toString());
c1.show_area();
}
}
Circle.java
abstract class Circle{
protected double r;
protected static double pi = 3.14;
Circle() {
}
Circle (double a) {
r = a;
}
abstract void show();
abstract void show_area();
void setR(int a) {
r = a;
}
}
Coin.java
class Coin extends Circle{
private int value;
Coin(int v){
value = v;
if (v == 1) {
setR(1);
}
else if (v == 5) {
setR(5);
}
else if (v == 10) {
setR(10);
}
else if (v == 50) {
setR(50);
}
}
void show() {
System.out.println("R=" + r);
System.out.println("value=" + value);
}
void show_area() {
System.out.println("Area=" + value*value*pi);
}
//override
public String toString() {
String str = "toString is called!";
return str;
}
}