본문 바로가기
프로그래밍 언어/JAVA(자바) 응용

4.객체의 속성은 멤버 변수, 객체의 기능은 메서드

by lroot 2022. 5. 18.
728x90
반응형

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

- 학생 클래스의 속성을 멤버 변수로 선언하고 메서드로 구현한다.

public class Student {
public int studentId;
public String studentName;
public String address;

public void showStudentInfo() {
System.out.println("학번 : "+studentId);
System.out.println("이름 : "+studentName);
System.out.println("주소 : "+address);
}

public int getStudentId() {
return studentId;
}

public String getStudentName() {
return studentName;
}

public String getAddress() {
return address;
}

}

 

메인 클래스

public class StudentTest {

public static void main(String[] args) {

Student lRoot = new Student();

lRoot.studentId = 20220518;
lRoot.studentName = "lRoot";
lRoot.address = "lRoot블로그";

lRoot.showStudentInfo();
System.out.println();

Student student = new Student();
student.studentId = 20213154;
student.studentName = "학생";
student.address = "주소";

student.showStudentInfo();

}

}

 

'프로그래밍 언어 > JAVA(자바) 응용' 카테고리의 다른 글

6.생성자  (0) 2022.05.19
5.인스턴스 생성과 힙  (0) 2022.05.19
3.함수와 메서드  (0) 2022.05.18
2.클래스 구현  (0) 2022.05.18
1.객체와 객체지향  (0) 2022.05.17

댓글