java
객체
yoneeee
2021. 7. 7. 23:43
객체 클래스를 만드는 방법 예제
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이고, 주소는 서울 강남구입니다.
}
}