關於java中函數功能的封裝 – JAVA編程語言程序開發技術文章

[java] 
//Triangle類,用於封裝Triangle的屬性及功能。 
package qing; 
 
public class Triangle { 
    double sideA,sideB,sideC,area,length; 
       boolean boo; 
       Triangle () 
       { 
           this.sideA = 3; 
           this.sideB = 4; 
           this.sideC = 5; 
       } 
       public boolean Trangle(double a,double b,double c)  
       {  
        this.sideA = a; 
        this.sideB = b; 
        this.sideC = c; 
        //參數a,b,c分別賦值給sideA,sideB,sideC 
         if(this.sideA + this.sideB > this.sideC && this.sideA + this.sideC > this.sideB && this.sideB + this.sideC > this.sideA) //a,b,c構成三角形的條件表達式 
         {  
            this.boo = true;//給boo賦值。 
         }     
        else 
         {  
            this.boo = false;//給boo賦值。 
         } 
         return boo; 
       } 
       public double getLength()  
       {    
           if(this.sideA + this.sideB > this.sideC && this.sideA + this.sideC > this.sideB && this.sideB + this.sideC > this.sideA) 
            {  
              this.length  = this.sideA + this.sideB + this.sideC; 
            } 
          else 
            {  
              System.out.println("不是一個三角形,不能計算周長"); 
              this.length = 0; 
            } 
           return this.length; 
       } 
       public double  getArea()  
       {   
          if(this.sideA + this.sideB > this.sideC && this.sideA + this.sideC > this.sideB && this.sideB + this.sideC > this.sideA) 
            {  
              double p=(sideA+sideB+sideC)/2.0; 
              area=Math.sqrt(p*(p-sideA)*(p-sideB)*(p-sideC)) ; 
              return area; 
            } 
          else 
            {  
              System.out.println("不是一個三角形,不能計算面積"); 
              return 0; 
            } 
       }  
       public void setABC(double a,double b,double c) 
       {  
           this.sideA = a; 
           this.sideB = b; 
           this.sideC = c;//參數a,b,c分別賦值給sideA,sideB,sideC 
           if(this.sideA + this.sideB > this.sideC && this.sideA + this.sideC > this.sideB && this.sideB + this.sideC > this.sideA) //a,b,c構成三角形的條件表達式 
             {  
                this.boo = true;//給boo賦值。 
             }     
            else 
             {  
                this.boo = false;//給boo賦值。 
             } 
       } 
 

 

[java]
//Lader類,用於封裝Lader的屬性及功能。 
 
package qing; 
 
public class Lader { 
    double above,bottom,height,area;  
    Lader(double a,double b,double h) 
    { 
       this.above = a; 
       this.bottom = b; 
       this.height = h;//方法體,將參數a,b,c分別賦值給above,bottom,height 
    } 
    public double getArea() 
    { 
      this.area = (this.above + this.bottom) * this.height/2;//方法體,,要求計算出area返回 
      return this.area; 
    } 
 

 

[java] 
//Circle類,用於封裝Circle的屬性及功能。 
 
package qing; 
 
public class Circle { 
    double radius,area; 
    Circle(double r) 
    {  
        this.radius = r; //方法體 
    } 
    public double getArea() //getArea方法體的代碼 
    {   
        this.area = Math.PI * this.radius * this.radius; //方法體,要求計算出area返回 
       return this.area; 
    } 
    public double getLength()  
    {   
       double length; 
       length = 2 * Math.PI * this.radius;//要求計算出length返回 
       return length; 
    } 
    void setRadius(double newRadius) 
    {   
       radius=newRadius; 
    } 
    double getRadius()  
    {  
        return radius; 
    } 
 

 

[java] 
//Test類,用於測試各個類內的功能是否能實現。 
package qing; 
 
public class Test { 
    /**
     * @param args
     */ 
    public static void main(String[] args) { 
        // TODO Auto-generated method stub 
 
           double length,area; 
           Circle circle=null; 
           Triangle trangle; 
           Lader lader; 
           circle = new Circle(3); //創建對象circle 
           trangle = new Triangle (); //創建對象trangle。 
           lader = new Lader(3,4,5); //創建對象lader 
           length = circle.getLength(); // circle調用方法返回周長並賦值給length 
           System.out.println("圓的周長:"+length);  
           area = circle.getArea(); // circle調用方法返回面積並賦值給area 
           System.out.println("圓的面積:"+area);  
           length = trangle.getLength(); // trangle調用方法返回周長並賦值給length 
           System.out.println("三角形的周長:"+length);  
           area = trangle.getArea();// trangle調用方法返回面積並賦值給area 
           System.out.println("三角形的面積:"+area);  
           area = lader.getArea() ; // lader調用方法返回面積並賦值給area 
           System.out.println("梯形的面積:"+area);  
           trangle.setABC(12, 34, 1); // trangle調用方法設置三個邊,要求將三個邊修改為12,34,1。 
           area =  trangle.getArea();// trangle調用方法返回面積並賦值給area 
           System.out.println("三角形的面積:"+area);  
           length = trangle.getLength(); // trangle調用方法返回周長並賦值給length 
           System.out.println("三角形的周長:"+length); 
    } 
 
<img src=/wp-content/images1/20181102/2012101803455547787.jpg" alt="">} 

 

發佈留言