Java/2012.04 강좌
7일차 인스턴스 this, static
Bohemian life
2012. 4. 12. 12:44
public class ThisStudy { static String name="곰돌이"; public ThisStudy(){ System.out.println("name= "+this.name); } public static void main(String[] args) { Cal cal1=new Cal(5,10); System.out.println(cal1.x); cal1.x=100; System.out.println(cal1.x); System.out.println("name= "+name); } static class Cal{ int x; int y; public Cal(){}//생략 가능한 default 생성자 메서드 public Cal(int x,int y){ //1.객체를 생성합니다.x와 y 필드가 생긴다. this.x=x;//2.필드 x에 값을 넣습니다. this.y=y;//3.필드 y에 값을 넣습니다. } } }
public class StaticStudy { public static void main(String[] args) { // 객체 생성없이도 사용이 가능합니다. System.out.println(Test2.total); Test2.hi(); Test2 t1 = new Test2(1,1); Test2 t2 = new Test2(5,1); Test2 t3 = new Test2(10,1); t1.increment(); System.out.printf(" t1객체의 field num=%d total=%d \n",t1.num , t1.total ); t2.increment(); System.out.printf(" t2객체의 field num=%d total=%d \n",t2.num , t2.total ); t3.increment(); System.out.printf(" t3객체의 field num=%d total=%d \n",t3.num , t3.total ); } } class Test2{ int num; static int total=10; // static : 공유 , 은행의 총 잔고 public Test2(){} public Test2(int x, int y){ num = x; total = y ; } public void increment(){ num+=1;//num = num + 1; total+=1;//total = total + 1 ; } public static void hi(){ System.out.println("안녕하세요"); } }