markdown
#說明
這裡使用到 static 的方法 他會去固定變數的值,也就是建構過一次後去呼叫時就會是這個值,而且這個值是共享的。
#操作流程
##Code
```
package staticc;
class Test{
 public int x;
 public static int y;
 
 public Test(int x, int y) {
  this.x=x;
  this.y=y; 
 }
 public String toString() {
  return "(x,y) :( "+ x + ","+ y+")";
   }
}
public class static2 {
 public static void main (String argv[]) {
  Test a= new Test(100,200);
  Test b= new Test(150,50);
  Test c= new Test(200,60);
  
  System.out.println("object a = " +a) ;
  System.out.println("object b = " +b) ;
  System.out.println("object c = " +c) ;
 }
}
```
##Demo
最後建構的 y 值 = 60, 他是一個共享的狀況,所以到 print 在呼叫的時候 y 都是等於60

留言
張貼留言