長方形クラスRectangleを定義します:(ナレッジポイント:オブジェクトの作成と使用)
1次の3つのメソッドを定義します:コンソールのgetArea()、周囲のgetPer()、showAll()出力の長さ、幅、面積、周囲長い。
2 2つの属性があります:長い長さ、広い幅
3 Rectangleオブジェクトを作成し、関連情報を出力します
1 パッケージのdfishf; 2 3 公開 クラスの宿題{ 4 int length; 5 int 幅; 6 7 public void getArea(){ 8 System.out.println(length * width); 9 } 10 11 public void getPer(){ 12 System.out.println((length + width)* 2 ); 13 } 14 15 public void showAll(){ 16 System.out.println( " 长是 " + length); 17 System.out.println( "Width is" + width); 18 System.out.println( "Area is" ); 19 getArea(); 20 System.out.println( "Perimeter is" ); 21 getPer( ); 22 } 23 }
1 パッケージのdfishf; 2 3 public class 宿題{ 4 5 public static void main(String [] args){ 6 7 Rectangle r1 = new Rectangle(); 8 r1.width = 10 ; 9 r1.length = 50 ; 10 r1.getArea(); 11 r1.getPer(); 12 r1.showAll(); 13 } 14 }