객체 : 존재하거나 생각할 수 있는 것. 인간이 생각하고 표현할 수 있는 모든 것이 객체
객체지향기법 : 실세계의 문제를 사람이 생각하는 방식대로 자연스럽게 표현(모델링)하여 컴퓨터로 옮기는 방식
객체화한다 : 메모리에 변수와 함수를 배치하고 사용
클래스 : 클래스에 포함되는 요소. 멤버(멤버 변수, 멤버 함수)라고도 함. 설계도
클래스 기본형식
class 클래스명
{ 멤버 리스트(변수와 함수)
속성(변수) 작성
기능(메서드) 작성
};
※패키지이름은 소문자, 클래스 이름은 대문자, 변수와 메서드 이름은 소문자로 시작하여 중간에 새 단어로 대문자+소문자
인스턴스(객체)
실제로 사용할 수 있도록 생성된 클래스.
클래스에 main() 함수 포함
public class Student1 {
int studentID; //학번을 담아두는 멤버 변수
String studentName; //학생이름을 담아두는 멤버 변수
int grade; //학년을 담아두는 변수
String address; //주소를 담아두는 변수
public static void main(String[] args) {
//기본 : 자료형 변수명 = 값;
int a = 10;
//객체 생성(인스턴스 생성)
//자료형 참조(변수명) or 객체형 = new 자료형();
//new? heap 영역에 배치시키고 stack 영역의 변수에 찾아갈 주소를 담아줌.
Student1 st = new Student1();
//Student1 st; st= new Student1();
System.out.println(a);
System.out.println(st);//참조변수명(객체형)에는 주소가 들어있음
System.out.println(st.studentID);
System.out.println(st.studentName);
System.out.println(st.grade);
System.out.println(st.address);
public class Student1 {
int studentID; //학번을 담아두는 멤버 변수
String studentName; //학생이름을 담아두는 멤버 변수
int grade; //학년을 담아두는 변수
String address; //주소를 담아두는 변수
public static void main(String[] args) {
//기본 : 자료형 변수명 = 값;
int a = 10;
Student1 st = new Student1();
System.out.println(a);
System.out.println(st);
System.out.println(st.studentID);
System.out.println(st.studentName);
System.out.println(st.grade);
System.out.println(st.address);
//멤버 변수에 데이터값 넣기
st.studentID = 231127;
st.studentName = "홍길동";
st.grade = 1;
st.address = "서울특별시";
System.out.println(st.studentID);
System.out.println(st.studentName);
System.out.println(st.grade);
System.out.println(st.address);
public class Student1 {
int studentID; //학번을 담아두는 멤버 변수
String studentName; //학생이름을 담아두는 멤버 변수
int grade; //학년을 담아두는 변수
String address; //주소를 담아두는 변수
void show() {
// 변수명만 써준다.
//4개의 멤버 변수에 저장되어 있는 데이터값을 출력하는 메서드
System.out.println("학번 : " + studentID);
System.out.println("이름 : " + studentName);
System.out.println("학년 : " + grade);
System.out.println("주소 : " + address);
}
//매개변수값이 없고, 반환값이 있는 경우
//맴버 변수에 이름이 저장되어 있는 데이터값 출력
public String getStudentName() {
return studentName; //멤버 변수 이름에 저장되어 있는 값만 호출한 곳으로 반환
}
public static void main(String[] args) {
//기본 : 자료형 변수명 = 값;
int a = 10;
//객체 생성(인스턴스 생성)
//자료형 참조(변수명) or 객체형 = new 자료형();
//new? heap 영역에 배치시키고 stack 영역의 변수에 찾아갈 주소를 담아줌
Student1 st = new Student1();
//Student1 st; st= new Student1();
System.out.println(a);
System.out.println(st);//참조변수명(객체형)에는 주소가 들어있음
System.out.println(st.studentID);
System.out.println(st.studentName);
System.out.println(st.grade);
System.out.println(st.address);
System.out.println("-------------");
//멤버 변수에 데이터값 넣기
st.studentID = 231127;
st.studentName = "홍길동";
st.grade = 1;
st.address = "서울특별시";
System.out.println(st.studentID);
System.out.println(st.studentName);
System.out.println(st.grade);
System.out.println(st.address);
System.out.println("-------------");
st.show();
System.out.println("-------------");
st.getStudentName();//함수 호출과 결과값을 가지고 있음.
String name = st.getStudentName();
System.out.println(st.getStudentName());
st.studentName = "자바";
System.out.println(st.studentName);//멤버 변수명으로 출력
System.out.println(st.getStudentName());//만들어 놓은 메서드 결과값 출력
실행클래스 따로 만들기
클래스가 복잡할 때
package test5;
//설계도만 있는 클래스
//main 함수를 포함한 실행 클래스를 따로 작성한 클래스
public class Student2 {
int studentID;
String studentName;
int grade;
String address;
void show() {
System.out.println("학번 : " + studentID);
System.out.println("이름 : " + studentName);
System.out.println("학년 : " + grade);
System.out.println("주소 : " + address);
}
public String getStudentName() {
return studentName;
}
//main 함수가 없음. 객체 생성할 수 없음.
}
package test5;
//수행만 하는 클래스
//설계만 해놓은 클래스
public class StudentTest {
public static void main(String[] args) {
Student2 st = new Student2();
System.out.println(st);
System.out.println("------------");
st.show();
System.out.println("------------");
st.studentName = "자바";
System.out.println("이름 : " + st.studentName);
st.getStudentName(); //호출+결과값을 가지고 있음
System.out.println("이름 : " + st.getStudentName());
}
}
2번째 객체 만들기
package test5;
public class StudentTest {
public static void main(String[] args) {
System.out.println("------------");
Student2 st1 = new Student2();
System.out.println("st1 : " + st1);
System.out.println("------------");
st1.show();
System.out.println("------------");
package test5;
public class StudentTest {
public static void main(String[] args) {
st1.studentName = "두번째";
System.out.println("이름 : " + st1.studentName);
st1.getStudentName();
System.out.println("이름 : " + st1.getStudentName());
패키지
서로 관련있는 클래스나 인터페이스들을 컴파일 해놓은 .class 파일을 한 곳에 묶어 놓은 것
클래스 파일들을 분류해서 저장해 놓은 디렉토리(폴더)와 같은 개념
하나의 패키지는 하나의 디렉토리에 저장된 클래스 파일
1. 패키지 만들기(my.book)
2. 클래스 만들기(Book)
3. 설계 클래스
package my.book;
public class Book {
//멤버 변수
public String bookName = "두근 두근 내 인생";
//접근 제어자를 붙여줘야함
//멤버 메소드
//접근 제어자를 붙여줘야함
public void say() {
System.out.println("my.book 패키지의 Book 클래스를 사용");
}
}
4. 수행 클래스 만들기(test5 패키지에 만들기)
package test5;
//※public 접근 제어자. 다른 패키지와 모든 클래스에서 접근 가능하도록 허락. 어디서나 접근이 가능(같은 패키지, 다른 패키지 클래스 사용 가능)
public class Book_Main {
//my.book 패키지 내 설계해놓은 Book 클래스 수행하는 클래스
public static void main(String[] args) {
//경로가 다를 경우 반드시 패키지를 다 써줘야함
//설계해놓은 클래스가 현재 수행하는 클래스와 다른 곳에 위치하면
//객체 생성 시 패키지명.클래스명 변수명 = new 패키지명.클래스명();
my.book.Book pac = new my.book.Book();
pac.say();
System.out.println(pac.bookName);
}
}
5. 패키지명을 안붙힐 경우
Book pac1 = new Book();
Ctrl + Shift + O
package test5;
import my.book.Book;
//※public 접근 제어자. 다른 패키지와 모든 클래스에서 접근 가능하도록 허락. 어디서나 접근이 가능(같은 패키지, 다른 패키지 클래스 사용 가능)
public class Book_Main {
//my.book 패키지 내 설계해놓은 Book 클래스 수행하는 클래스
public static void main(String[] args) {
my.book.Book pac = new my.book.Book();
pac.say();
System.out.println(pac.bookName);
System.out.println("------------");
//패키지명을 안 붙힘. import 해서 객체 생성
//소스 제일 상단에 패키지명과 import my.book.Book;
//Ctrl + Shift + O : 자동으로 패키지 경로 찾기
Book pac1 = new Book();
pac1.say();
System.out.println(pac.bookName);
System.out.println("------------");
}
}
설계해놓은 패키지와 실행하려는 패키지 위치 확인 및 접근 제어자 확인
생성자
오버로드(overload) : 메서드 이름이 같고 매개 변수만 다른 경우
package test5;
//메서드 오버로딩 : 메서드 중복
//메서드의 이름은 같고 매개변수의 자료형이나 개수가 다른 여러 개의 함수를 작성해 놓고 사용할 수 있음.
public class Overloading1 {
//a
public static int sum(int a, int b) {
System.out.println("인자가 2개일 경우");
return a + b;
}
//b : 매개변수 개수가 다른 경우
public static int sum(int a, int b, int c) {
System.out.println("인자가 3개일 경우");
return a + b + c;
}
//c : 자료형이 다른 경우
public static double sum(double a, double b, double c) {
System.out.println("인자가 3개이고 자료형이 다른 경우");
return a + b + c;
}
public static void main(String[] args) {
//a
System.out.println("sum(10,5)=" + sum(10,5));
//b
System.out.println("sum(10,5,1)=" + sum(10,5,1));
//c
System.out.println("sum(10.5,5.5,1.5)=" + sum(10.5,5.5,1.5));
}
}
그림 15를 보면 println도 많은 것을 알 수 있다. 메소드 오버로딩된 것임.
'KDT > Java' 카테고리의 다른 글
231130 Java (0) | 2023.11.30 |
---|---|
231129 Java (0) | 2023.11.29 |
231123 Java (0) | 2023.11.23 |
231122 Java (0) | 2023.11.22 |
231120 Java (0) | 2023.11.20 |