본문 바로가기

언어/자바

객체의 이해...

클래스 : 객체 대한 설계도, 디자인
클래스의 구성 속성(맴버 변수) + 메서드(기능 + 동작)
객체는 클래스(구조)를 바탕으로 만들어져 힙 메모리에 담긴다.


[이해한 부분]

객체는 인스턴스화, 즉 실체화 되는 과정
추상화는 공통된 성질을 모아둔 것, 즉 User라른 단어를 보면, 모오하고 추상적이다. 

User는 객체이다.

여러 유저 골드 유저, 실버 유저, 일반 유저등이 존재할 것이다. 그런 공통된 속성들을 모아둔 것..

즉, User에게 필요한 이름, 나이, 등급 등... 공통으로 골드 유저, 실버 유저, 일반 유저에서 사용되는 것들을 모아둔 것이 

클래스이다.

그래서 특정 시점이나, 고객에 웹사이트에 들어와서 회원가입시 수시로 객체를 생성 즉, 인스턴스를 한다.

 

// 맴버변수
int studentNumber = 0;
String name = "";
String eamil = "";
String address = "";
int mileage = 10000;
int age = 0;
int money = 0;

// 기본 default 생성자
public _01_Student() {}

// 생성자
public _01_Student(int studentNumber, String name, String eamil, String address, int age, int money) {
this.studentNumber = studentNumber;
this.name = name;
this.eamil = eamil;
this.address = address;
this.age = age;
this.money = money;
}

// Getter와 Setter
public int getMileage() {
return mileage;
}


public void setMileage(int mileage) {
this.mileage = mileage;
}

// 메서드
public void showStudentInfo() {
System.out.println("학번 : " + studentNumber + "\n"
+ "이름 : " + name + "\n"
+ "이메일 : " + eamil + "\n"
+ "주소 : " + address + "\n"
+ "마일리지 : " + mileage + "\n"
+ "나이 : " + age + "\n"
+ "용돈 : " + money);
}

// 마일리지 적립 메서드
public void add(int mileage) {
this.mileage += mileage;
}

// 마일리지에 따른 등급메기기
public String showGrade(int mileage) {

String grade = "";

if(mileage >15000) {
grade = "최상위";
}
package ch03;


public class _01_StudentMain {


public static void main(String[] args) {

// 클래스 : 맴버변수와 맴버 메서드로 구성된 설계도이다.
// 객체 : 클래스로 만들어진 힙메모리 공간이다. 인스턴스화하는 것

// s1이라는 객체 생성하여 메모리에 할당
// _01_Student 클래스에서 선언한 메서드와 속성은 메모리에 올라간다.

// 스텍은 매개변수와 지역변수가 들어간다.
// (1). 먼저 객체 생성 후 메모리 공간에 객체가 올라간다.
// (2). 하지만, 아직 아무런 값이 담기지 않는다.

// 힙 영역(s1은 참조 변수)
_01_Student st1 = new _01_Student();

// 주소값 뽑아보기
System.out.println("주소값 : " + st1);

st1.studentNumber = 192326;
st1.name = "이충현";
st1.eamil = "lch3067@naver.com";
st1.address = "경남 남해";
st1.age = 35;
st1.money = 24243;
st1.add(-2000);
st1.showGrade(st1.getMileage());
System.out.println(st1.showGrade(st1.getMileage()));
st1.showStudentInfo();

System.out.print("\n");

System.out.println("------------------------------");

_01_Student st2 = new _01_Student();

System.out.println("주소값 : " + st2);


st2.studentNumber = 12498;
st2.name = "김복길";
st2.eamil = "lch3067@naver.com";
st2.address = "경남 진주";
st2.age = 29;
st2.money = 2311414;
st2.add(1000);
st2.showGrade(st1.getMileage());
System.out.println(st2.showGrade(st2.getMileage()));
st2.showStudentInfo();

System.out.print("\n");

System.out.println("------------------------------");

// 생성자를 통한 값 할당
_01_Student st3 = new _01_Student(192310, "김국진", "ktf3067@naver.com", "길당", 24, 2900000);

System.out.println("주소값 : " + st3);

st3.add(9000);
System.out.println(st3.showGrade(st3.getMileage()));
st3.showStudentInfo();

}

}

'언어 > 자바' 카테고리의 다른 글

다운케스팅, 다형성  (0) 2025.06.02
overide vs overoad vs 상속  (0) 2025.05.30
static - 핵심  (0) 2025.05.29
Call by Value vs Call by Reference  (0) 2025.05.29
참조변수  (0) 2025.05.28