객체의 기능을 구현하기 위해 클래스 내부에 구현되는 함수
멤버 함수 (member function)이라고도 함
메서드를 구현함으로써 객체의 기능이 구현 됨
함수와 메서드의 차이점
메서드란
자바에서 메서드란 객체의 기능을 구현하기 위해서 클래스 내부에
구현되는 메서드
맴버 함수(member function)
결론 : 메서드는 자신의 변수를 활용해서 객체의 기능을 구현하는것
기본데이터 타입은 - 값(리터럴)이 출력된다
참조 탑입은 - 주소값이 출력 된다
class 코드 파일 설계도
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | package ch01; public class Student { // 상태(속성)(맴버 변수) int studentId; String studentName; String address; int studentNum; int studentTop; double height;//0.0 boolean isMarred;//false // 기능 // 공부한다. public void study() { System.out.println(studentName + "학생이 공부를 합니다."); } // 휴식한다 public void breakTime() { System.out.println(studentName + "학생이 휴식을 합니다."); } public void showInfo() { System.out.println("=========상태창========="); System.out.println("학생의 ID값은 : " + studentId); System.out.println("학생의 이름은 : " + studentName); System.out.println("학생의 주소은 : " + address); System.out.println("======================="); } //함수와 메서드의 차이점 //메서드란 //자바에서 메서드란 객체의 기능을 구현하기 위해서 클래스 내부에 구현되는 메서드 //맴버 함수(member function) //결론 : 메서드는 자신의 변수를 활용해서 객체의 기능을 구현하는 것 //학생의 기능(메서드 3가지 정의)해주세요 public void study1() { System.out.println(studentName +"이 학원을 갑니다"); } public void test() { System.out.println(studentName +"이 시험을 칩니다"); } public void showInfo2() { System.out.println("=========상태창========="); System.out.println("학생의 ID값은 : " + studentId); System.out.println("학생의 이름은 : " + studentName); System.out.println("학생의 주소은 : " + address); System.out.println("학생의 점수는 : " + studentNum); System.out.println("학생의 등수는 : " + studentTop); System.out.println("======================="); } }// end of class | cs |
class 설계파일을 이용한 main 코드들
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | package ch01; public class MainTest1 { // 메인함수 public static void main(String[] args) { // The local variable studentKim may not have been initialized // 데이터 타입이 - 사용자 정의 타입이고 대문자이기때문에 참조 타입 Student studentKim = new Student(); studentKim.studentId = 1; studentKim.studentName = "홍길동"; studentKim.address = "부산시 해운대구"; studentKim.study(); studentKim.breakTime(); studentKim.showInfo(); System.out.println("----------------------"); Student studentLee = new Student(); studentLee.studentName = "이순신"; studentLee.studentId = 2; studentLee.address = "부산시 진구"; studentLee.study(); studentLee.showInfo(); }// end of main }// end of class | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | package ch01; public class MainTest2 { public static void main(String[] args) { Student studentA = new Student(); studentA.studentId = 1; studentA.studentName = "김민우"; studentA.address = "부산시 사상구"; studentA.studentNum = 90; studentA.studentTop = 3; studentA.study1(); studentA.test(); studentA.showInfo2(); } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | package ch01; //수입하다 , 가져오다 import java.util.Scanner; public class MainTest3 { //메인함수 public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println(scanner); Student studentKim = new Student(); System.out.println(studentKim);//주소값이 출력이 된다. Student studentLee = new Student(); System.out.println(studentLee); //** //기본 데이터 타입은 -- 값 (리터럴)이 출력이 된다 //참조타입은 - 주소값이 출력이 된다 System.out.println("-----------"); System.out.println(studentLee.studentId); System.out.println(studentLee.studentName); System.out.println(studentLee.address); System.out.println(studentLee.height); System.out.println(studentLee.isMarred); //맴버 변수는 메모리에 올라간뒤 맴버 변수에 값을 할당하지 않으면 //기본값으로 먼저 초기화 해준다(컴파일러가 알아서 초기화 해줌) }//end of main }//end of class | cs |
'자바' 카테고리의 다른 글
생성자 (0) | 2023.02.14 |
---|---|
인스턴스 (instance) 와 힙 메모리 (0) | 2023.02.14 |
함수 호출과 스택 메모리 (0) | 2023.02.14 |
함수 (0) | 2023.02.14 |
객체 지향언어란? (0) | 2023.02.14 |