카테고리 없음
9.Java 클래스 멤버변수에 데이터 셋팅하기(객체 생성)
oioioa
2024. 12. 15. 18:47
클래스 멤버변수에 데이터를 셋팅하는 방법 3가지
첫번째 방법은 Constructor(생성자)를 이용해 메모리 공간 확보와 데이터 저장을 한줄로 입력한다.
package entity;
public class Member {
String name;
String tel;
String address;
public Member() {
}
public Member(String name, String tel, String address) {
this.name = name;
this.tel = tel;
this.address = address;
}
}
import entity.Member;
public class TestMain {
public static void main(String[] args) {
Member c2 = new Member("홍길동","010-1234-5678","경기도 성남시");
}
}
.
두번째 방법은 각 멤버변수에 데이터를 입력한다.
public class Member {
String name;
String tel;
String address;
}
public class TestMain {
public static void main(String[] args) {
Member c2 = new Member();
c2.name ="홍길동";
c2.tel ="010-1234-5678";
c2.address ="경기도 성남시";
}
}
세번째 방법은 클래스에 메소드(/함수)를 만들어 데이터를 입력한다.
public class Member {
String name;
String tel;
String address;
public Member() {
}
public Member(String name, String tel, String address) {
this.name = name;
this.tel = tel;
this.address = address;
}
}
public class TestMain {
public static void main(String[] args) {
Member c2 = new Member();
c2.name ="홍길동";
c2.tel ="010-1234-5678";
c2.address ="경기도 성남시";
}
}