본문 바로가기
Language/JAVA

JAVA 객체(3) - 연습문제

by 파2RI 2023. 7. 18.

Student.java

 

package classpractice;

	class Student {  // 틀이 될 class인 'Student'

		String name;
		int age;
		int height;
		String number;  // 객체 변수들인 name, age, height, number
		
		public void setStudent(String name, int age, int height, String number) {
			
			this.name = name;
			this.age = age;
			this.height = height;
			this.number = number;  
            
         	   // setStudent 메서드 : 각각 객체마다, 객체변수들에 값을 집어넣어라
			
		}
		
}

 

 

StudentPractice.java;

 

package classpractice;

public class MainStudent {

	
	public static void main(String[] args) {

	Student chulsoo = new Student();
	chulsoo.setStudent("김철수", 17, 161, "22");
	
	Student younghee = new Student();
	younghee.setStudent("정영희", 18, 179, "11");
	
	Student chunja = new Student();
	chunja.setStudent("조춘자", 16, 168, "87");
	
    // chulsoo, younghee, chunja 객체에, 각각의 객체변수를 집어넣는 setStudent 메서드 사용
    
	System.out.println(chulsoo.name+" "+chulsoo.age+" "+chulsoo.height+" "+chulsoo.number);
	System.out.println(younghee.name+" "+younghee.age+" "+younghee.height+" "+younghee.number);
	System.out.println(chunja.name+" "+chunja.age+" "+chunja.height+" "+chunja.number);
	
	}
	
}

 

 

결과

 

김철수 17 161 22
정영희 18 179 11
조춘자 16 168 87

'Language > JAVA' 카테고리의 다른 글

JAVA 객체(5) - 메서드에서 빠져나가기  (0) 2023.07.21
JAVA 객체(4) - 메서드 두 번째  (0) 2023.07.20
JAVA 객체(2) - 메서드  (0) 2023.07.18
JAVA 객체(1) - 클래스  (0) 2023.07.18
JAVA for each 문  (0) 2023.07.17