【程式語言】JAVA的第九堂課 |物件導向程式設計 (披薩物件 inclass) 繼承
Published:
by .ch10.java
class ch10
{
public static void main(String[] aru)
{
pizza p1=new pizza();
pizza p2=new pizza();
p1.addTopping("PineApple");
p2.addTopping("Pepperoni");
p2.addTopping("Mushroom");
p1.showPizza();
p2.showPizza();
}
}
Circle.java
class Circle
{
protected double r;
static double pi;
Circle()
{
pi=3.14159;
}
Circle(double d)
{
this();
r=d;
}
static void showPi()
{
System.out.println("Circle pi="+pi);
}
void setCircle(double ir)
{
r=ir;
}
double getR()
{
return r;
}
double area()
{
return r*r*pi;
}
}
pizza.java
class pizza extends Circle
{
//data member
int price;
String topping;
//
pizza () {
r = 10;
price = 300;
topping = "Cheese ";
}
int getPrice()
{
return 0;
}
void addTopping(String topp) {
this.topping += topp + " ";
price += 50;
}
void showTopping() {
System.out.println("Topping:" + topping);
}
void showPizza() {
System.out.println("price:" + price);
System.out.println("size:" + r);
this.showTopping();
}
}