• 객체지향 프로그래밍
      • 조금 더 현실 세계에 빗대어 체계적으로 해보자는 발상이 객체지향 프로그래밍의 핵심
    •  Class(클래스)
      • 현실과 비슷한 개념(객체)을 나타내기 위한 자바의 도구
      • 클래스 내 정보를 멤버 변수라고 함
      • 생성자
          • 클래스명과 똑같은 이름을 가진 메소드
          • 클래스 변수를 새롭게 만들 때 사용
          • 기본생성자: 아무런 파라미터가 없는 생성자
        // Course.java
        public class Course {
            public String title;
            public String tutor;
            public int days;
        
        	// 기본생성자
            public Course() {
        
            }
        	// 생성자
            public Course(String title, String tutor, int days) {
                this.title = title;
                this.tutor = tutor;
                this.days = days;
            }
        }
        
        // Prac.java
        Course course = new Course();
        course.title = "Spring";
        • 빵틀(Course), 빵(course)
        • this(클래스 변수)라고 표시함으로써 빵틀 전체의 값을 바꾸는 게 아니라 빵 하나의 값만 바꾸는 것!
    • private / public
      • 클래스는 민감한 정보들도 담고 있기 때문에 모든 정보를 손쉽게 바꿀 수 있으면 안됨
      • 밖에서 드러내도 되는 것: public / 함부로 바꾸면 안되는 것: private
    • Getter / Setter
      • Getter: 정보를 가져오는 메소드 / Setter: 정보를 바꾸는 메소드
        /* Getter, Setter 만들기 */
        // Getter
        public String getTitle() {
        	return this.title;
        }
        
        // Getter
        public String getTutor() {
        	return this.tutor;
        }
        
        // Getter
        public int getDays() {
        	return this.days;
        }
        
        // Setter
        public void setTitle(String title) {
        	this.title = title;
        }
        
        // Setter
        public void setTutor(String tutor) {
        	this.tutor = tutor;
        }
        
        // Setter
        public void setDays(int days) {
        	this.days = days;
        }
        /* Getter, Setter 사용하기 */
        Course course = new Course();
        
        System.out.println(course.getTitle());
        System.out.println(course.getTutor());
        System.out.println(course.getDays());
        
        course.setTitle("Spring");
        course.setTutor("tom");
        course.setDays(7);
        
        System.out.println(course.getTitle());
        System.out.println(course.getTutor());
        System.out.println(course.getDays());
        public Class Tutor() {
        	// 멤버 변수
            private String name;
            private int bio;
            
            // 기본 생성자
            public Tutor() {
            
            }
            
            // 생성자
            public Tutor(String name, int bio) {
            	this.name = name;
                this.bio = bio;
            }
            
            // Getter
            public String getName() {
            	return this.name;
            }
            public int getBio() {
            	return this.bio;
            }
            
            // Setter
            public void setName(String name) {
            	this.name = name;
            }
            public void setBio(int Bio) {
            	this.bio = bio;
            }
        }
    • 브라우저에 나타내기
      • 데이터로 응답하려면, RestController 사용
      • Rest
        • 서버의 응답이 JSON 형식임을 나타냄
        • HTML, CSS 등을 주고받을 때는 Rest 붙이지 않음
      • Controller
        • 클라이언트의 요청(Request)을 전달받는 코드
        • JSON 만을 돌려주는 것: RestController
    • RestController 만들기
      • src > main > com.sparta.week01 에 controller 패키지 생성
      • CourseController.java 파일 생성
        public class CourseController {
            @GetMapping("/courses")
            public Course getCourse() {
                Course cousre = new Course();
                course.setTitle("Spring");
                course.setDays(7);
                course.setTutor("Tom");
                return course;
            }
        }
        • @GetMapping
          • GET 방식: 브라우저에서 주소를 치는 행위로 정보를 요청
          • 스프링 주소(http://localhost:8080) 뒤의 주소가 /courses 일 경우, getCourse 메소드를 실행함을 나타냄
    • Gradle
      • 오픈소스 빌드 도구
      • 추가하기
        • Maven Repository에서 원하는 라이브러리 찾기
        • build.gradle에 원하는 프로젝트 파일 넣기
        • dependencies 옆 Run 버튼 클릭
        • 우측 Gradle 탭의 새로고침 버튼 클릭
        • 대상 프로젝트가 추가된 것 확인

    'Spring' 카테고리의 다른 글

    [IntelliJ] GitHub 연동 및 프로젝트 업로드  (0) 2024.07.09
    [IntelliJ] 실행 오류  (0) 2024.07.09
    [Spring] JAVA 기초 문법(1)  (0) 2024.07.05
    [Spring] 스프링 작동시키기  (0) 2024.07.05
    [Spring] 웹의 기본 개념  (0) 2024.07.05

    + Recent posts