2013年12月31日 星期二

[Java 教學範例拷貝]- 內部類別(Inner class)

[Java 教學範例拷貝]- 內部類別(Inner class)


 


剛才找資料時發現一個的Java 教學網站,趕快發揮(C/P)的長才將它備份來,有需要的同好,歡迎來(C/P)一下^^。


 


拷貝來源:
http://openhome.cc/Gossip/JavaGossip-V1/


http://openhome.cc/Gossip/JavaGossip-V1/InnerClass.htm


 









public class OutClass { 
// 內部類別
private class Point {
private int x, y;

public Point() {
x = 0; y = 0;
}

public void setPoint(int x, int y) {
this.x = x;
this.y = y;
}

public int getX() {
return x;
}

public int getY() {
return y;
}
}

private Point[] points;

public OutClass(int length) {
points = new Point[length];

for(int i = 0; i < points.length; i++) {
points[i] = new Point();
points[i].setPoint(i*5, i*5);
}
}

public void showPoints() {
for(int i = 0; i < points.length; i++) {
System.out.printf("Point[%d]: x = %d, y = %d%n",
i, points[i].getX(), points[i].getY());
}
}
}
public class UseInnerClass {
public static void main(String[] args) {
OutClass out = new OutClass(10);

out.showPoints();
}
}
///////////
public class UseInnerClass {
public static void main(String[] args) {
Object obj = new Object() {
public String toString() {
return "匿名類別物件";
}
};
System.out.println(obj.toString());
}
}
/////////
public class UseInnerClass {
private static class Point {
private int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}

public int getX() {
return x;
}

public int getY() {
return y;
}
}

public static void main(String[] args) {
Point p = new Point(10, 20);

System.out.printf("x = %d, y = %d%n",
p.getX(), p.getY());
}
}
////////////
public class UseInnerClass {
private class Point {
private int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}

public int getX() {
return x;
}

public int getY() {
return y;
}
}

public static void main(String[] args) {
UseInnerClass inner = new UseInnerClass();
Point p = inner.new Point(10, 20);

System.out.printf("x = %d, y = %d%n",
p.getX(), p.getY());
}
}


沒有留言:

張貼留言