※J.Y Chen 的個人部落格 ※

Just Follow Our Heart And We will shine!

63 瀏覽人次

【程式語言】JAVA的第九堂課 |物件導向程式設計 (披薩物件 inclass) 繼承

Published: in JAVA 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();
  }
  
}

Result:

©2019 - 2024 Henry Chen