//期中考第三題 執行子類別或父類別建構元
//(3-1) 1.建立有1個字串的子類別建構元物件 2.接著執行同類別無參數建構元
//(3-2) 1.建立無參數的子類別建構元物件 2.接著執行父類別1字串1小數建構元
public class appsub extends app {
 String color="WHITE";
 public appsub() { super("正方體",2.0);//執行父類別1字串1小數建構元
  System.out.println(color);
 }
 public appsub(String ss) {
  this();//執行第4行同類別無參數建構元
  color=ss;
  System.out.println(color);
 }
 public static void main(String[] args) {
  //appsub oapp=new appsub("BLUE");//執行有1個字串的建構元(第6行)
  appsub oapp2=new appsub();//執行無參數的建構元(第4行)
 }

}

 

---


public class app {
 String shape="圓形";
 double side = 1.0;
 public app(){ System.out.println(shape + "A:" + side*3.1416); }
 public app(String ss, Double dd)
 {  shape=ss;
  side=dd;
  System.out.println(shape + "A:" + side*side); 
 }
 
}

 

----------------------

 

import java.util.*;
import java.math.*;
public class shape {
 //印出圓面積
 public shape(double dd) {System.out.println("圓形面積:"+ dd*dd* 3.1416);}

 public shape(double dd,double ww) {System.out.println("矩形面積:"+ dd*ww);}
 
 public shape(double a,double b,double c)
 {
 double s=(a+b+c)/2;
 double area=Math.sqrt(s*(s-a)*(s-b)*(s-c));
 System.out.println("三角形面積:"+ area);}

 public static void main(String[] args) {
  shape oo1 = new shape(3.0);
  shape oo2 = new shape(3.0,4.0);
  shape oo3 = new shape(3.0,4.0,5.0);
}
}

 

 

 -------------test 1. 執行同類別無參數建構元  2.兩個字串的子類別建構元物件

 

//期中考第三題:執行子類別或父類別建構元
//(3-1) 1.建立有一個字串的子類別建構元物件  2.接著執行同類別無參數建構元
//(3-2) 1.建立無參數的子類別建構元物件  2.接著執行同類別無參數建構元
public class appsub extends app {
 static String color="WHITE", color2="GREEN";
 public appsub() { super("正方體","矩形"); //執行父類1字串1小數建構元
  System.out.println(color);
 }
 public appsub(String ss){
 this();//執行第四行同類別無參數建構元
 color = ss;
 System.out.println(color);
 }
  public static void main(String[] args) {
   //appsub oapp = new appsub ("BLUE");//執行有一個字串的建構元 (第六行)
   appsub oapp2 = new appsub (); //執行無參數的建構元(第四行)
  
   
 }

}

 ---test


public class app {
 String shape = "圓形";
  double side = 1.0;
 public app(){System.out.println(shape + "A: "+ side*3.1416);}
 public app(String ss,String dd)
 { shape = ss;
  //side = dd;
  System.out.println(shape + "A: "+ side*side);
  }

}

 

---------------------

arrow
arrow
    全站熱搜

    玥君 發表在 痞客邦 留言(0) 人氣()