class PersonInformationWithMethod {
	// 멤버 변수 선언
	 String name;
	 int age;
	 String address;
// ( 생성자는 없음 ) -> 자동으로 디폴트 생성자 만듦(JVM이) -> 전역변수 초기화 (String=null, int=0)
	 // 멤버 변수의 값을 출력하는 method
	 public void printInformation() {
	 	System.out.println( name + "은 " + age + "세 이고, " + address + "에 삽니다" );
	 }
 } 
 public class PrintInformationWithMethod {
	 public static void main( String[] args ) {
		 // 인스턴스 생성. 인스턴스 명은 john
		 PersonInformationWithMethod john = new PersonInformationWithMethod();
		 john.name = "John";
		 john.age = 30;
		 john.address = "New York";
		 // 인스턴스 생성. 인스턴스 명은 john
		 PersonInformationWithMethod tom = new PersonInformationWithMethod();
		 tom.name = "Tom";
		 tom.age = 40;
		 tom.address = "Chicago";
		 // method를 호출해서 결과 값을 출력
		 john.printInformation();
		 tom.printInformation();
	 }
 }
'새로워지기 > 서른의 생활코딩' 카테고리의 다른 글
| ex9) java_oop (0) | 2012.02.16 | 
|---|---|
| ex8) java_oop (0) | 2012.02.16 | 
| ex6) java_oop (0) | 2012.02.16 | 
| ex5) java_oop (0) | 2012.02.16 | 
| java _20120216 (0) | 2012.02.16 | 
댓글