[Java 教学范例拷贝]- 扩充(extends)父类别(1/2)

 

刚才找资料时发现一个的Java 教学网站,赶快发挥(C/P)的长才将它备份来,有需要的同好,欢迎来(C/P)一下^^。

 

拷贝来源:
http://openhome.cc/Gossip/JavaGossip-V1/

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

 

public class Point2D {
private int x, y;
public Point2D() {}
public Point2D(int x, int y) {this.x = x; this.y = y;}
public int getX() {return x;}
public int getY() {return y;}
}

 

public class Point3D extends Point2D { // 扩充Point2D类别 
private int z;  // 新增私用资料 
public Point3D() {
super();
}
// 建构函式,同时指定呼叫父类别建构函式 
Point3D(int x, int y, int z) {
super(x, y);
this.z = z;
}
// 新增函式 
public int getZ() {return z;}
}

 

public class UseExtend {
public static void main(String[] args) {
Point3D p1 = new Point3D(1, 3, 4);
Point3D p2 = new Point3D();
System.out.printf("p1: (%d, %d, %d) \n",
p1.getX(), p1.getY(), p1.getZ());
System.out.printf("p2: (%d, %d, %d) \n",
p2.getX(), p2.getY(), p2.getZ());
}
}

 

相关文章