728x90
학생 클래스를 정의하고 이를 사용해보자
- 학생 클래스의 속성을 멤버 변수로 선언하고 메서드를 구현한다
public calss Student { public int studentID; public String studentName; public String address; public void showStudentInfo() { System.out.println(studentName + "," + addresss); } public String getStudentName() { return studentName; } }
- 학생 클래스를 생성하여 생성된 객체(인스턴스)에 각각 다른 이름과 주소를 대입한다.
public class StudentTest { 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); } }
728x90
'Be Smart > JAVA' 카테고리의 다른 글
[Java] JAR WAR 차이점 및 특징 (+ EAR) (0) | 2021.12.21 |
---|---|
[Java] 2.05. 인스턴스 생성과 힙 메모리 (heap memory) (0) | 2021.08.12 |
[Java] 2-03. 함수와 메서드 (0) | 2021.08.12 |
[Java] 2-02. 생활 속에서 객체 찾아 클래스로 구현해 보기 (0) | 2021.08.11 |
[Java] 2-01. 객체와 객체 지향 프로그래밍 (0) | 2021.08.11 |
댓글