Java/정리

멤버변수와 메서드

낭구리 2021. 8. 25. 13:54

학생 클래스를 정의 하고 이를 사용해 보자

public class Student {
	
	public int studentID;
	public String studentName;  
	public String address;
			
	public void showStudentInfo() {
		System.out.println(studentName + "," + address);
	}
	
	public String getStudentName() {
		return studentName;
	}
}

생성된 학생 객체(인스턴스)에 각각 다른 이름과 주소를 대입한다

public class StudentMainTest {

	public static void main(String[] args) {
		
		Student studentLee = new Student();
		studentLee.studentName = "이순신";
		studentLee.address = "서울";
		
		
		studentLee.showStudentInfo();
		
		Student studentKim = new Student();
		studentKim.studentName = "김유신";
		studentKim.address = "경주";
		
		studentKim.showStudentInfo();
		
		System.out.println(studentLee);
		System.out.println(studentKim);
	}

}

'Java > 정리' 카테고리의 다른 글

생성자  (0) 2021.08.25
인스턴스와 heap  (0) 2021.08.25
함수와 메서드  (0) 2021.08.23
객체 지향 언어  (0) 2021.08.23
switch-case 문  (0) 2021.08.23