2013年12月26日 星期四

[Java 教學範例拷貝]- 被保護的(protected)成員

[Java 教學範例拷貝]- 被保護的(protected)成員


 


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


 


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


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


 









public class Rectangle { 
// 受保護的member
protected int x, y;
protected int width, height;

public Rectangle() {
x = y = 0;
width = height = 0;
}

public Rectangle(int x, int y, int width, int height) {
this.x = x; this.y = y;
this.width = width; this.height = height;
}

public int getX() { return x; }
public int getY() { return y; }
public int getWidth() { return width; }
public int getHeight() { return height; }
public int getArea() { return width*height; }
}


 









public class Cubic extends Rectangle { 
protected int z;
protected int length;

public Cubic() {
z = 0; length = 0;
}

public Cubic(int x, int y, int z,
int length, int width, int height) {
super(x, y, width, height);
this.z = z;
this.length = length;
}

public int getLength() { return length; }
public int getVolumn() { return length*width*height; }
}


 









public class UseProtected { 
public static void main(String[] args) {
Cubic c1 = new Cubic(0, 0, 0, 10, 20, 30);

System.out.println("c1 volumn: " + c1.getVolumn());
}
}


沒有留言:

張貼留言