試編程在A包中定義矩形類Rect,類中有求矩形面積的方法,在B包中定義Test類,在該類的main()函式中呼叫A包中求面積的方法。
uj5u.com熱心網友回復:
public class Rectangle {private float length; //定義長變數
private float width; // 寬變數
public Rectangle(float length,float width){
this.length=length;
this.width=width;
}
public float getGirth(){
return (length+width)*2;
} //求周長方法
public float getArea(){
return length*width;
} //求面積方法
public static void main (String[] args) {
Scanner in=new Scanner(System.in);//呼叫輸入方法
System.out.println ("請輸入矩形的長:");
float a=in.nextFloat();
System.out.println ("請輸入矩形的寬:");
float b=in.nextFloat();
System.out.println ("矩形周長為:"+new Rectangle(a,b).getGirth());
System.out.println ("矩形面積為:"+new Rectangle(a,b).getArea());
}
}
uj5u.com熱心網友回復:
直接在百度拿的,你把main方法拿出來放到test類中轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/280424.html
標籤:Java相關
上一篇:springboot專案
