class
クラス名 { 変数の宣言 コンストラクタの宣言 メソッドの宣言 } |
01
public class Point { 02 double x, y; // 変数 03 Point(double x, double y) { // コンストラクタ 03 this.x = x; 04 this.y = y; 05 } 06 Point() { this(0.0,0.0); } 07 /* メソッドの宣言 */ 08 public void setPoint(double x, y) { 09 this.x = x; 10 this.y = y; 11 } 12 public void translatePoint(double a, double b) { 13 x += a; 14 y += b; 15 } 16 public double getX() { return x; } 17 public double getY() { return y; } 18 } |
01 public static class
Example2 { 02 static void main(String[] args) { 03 Point p1 = new Point(); 04 System.out.println("p1: x="+p1.getX()+", y="+p1.getY()); 05 p1.setPoint(2.0, 1.0); 06 System.out.println("p1: x="+p1.getX()+", y="+p1.getY()); 07 Point p2 = new Point(1.0,2.0); 08 System.out.println("p2: x="+p2.getX()+", y="+p2.getY()); 09 p2.translatePoint(2.0,5.0); 10 System.out.println("p2: x="+p2.getX()+", y="+p2.getY()); 11 } 12 } |
01 Point p1 =
new Point(); 02 Point p2 = new Point(2.0,1.0); 03 Point p3;
04 Point p4 = p2; |