본문 바로가기

java

객체

객체 클래스를 만드는 방법 예제

public class Student {
	public int studentId;
	public String studentName;
	public String address;
	
	public void showStudentInfo() {
		System.out.println(studentId + "학번의 이름은 " + studentName + "이고, 주소는 " + address + "입니다.");
	}
	
	public String getStudentName() {
		return studentName;
	}
	
	public void setStudentName(String name) {
		studentName = name; // 학생 이름 셋팅
	}
	
}

 

public class StudentTest {
	public static void main(String[] args) {
		Student studentLee = new Student(); //student 하나를 생성해라 
		//클래스를 기반으로 여러개의 인스턴스가 생김
		
		studentLee.studentId = 12345; //id생성
		studentLee.setStudentName("Lee"); // 이름생성
		studentLee.address = "서울 강남구";
		// 멤버변수들을 이용해서 값 세팅해줌
		
		studentLee.showStudentInfo();
		//12345 학생의 이름은 Lee이고, 주소는 서울 강남구입니다.
		
	}
}

 

'java' 카테고리의 다른 글

접근 제어 지시자와 정보은닉  (0) 2021.07.16
생성자란  (0) 2021.07.08
함수와 메서드  (0) 2021.07.07
객체 지향 입문  (0) 2021.07.07
자바 프로그래밍 시작하기  (0) 2021.07.07