Data Scientist 옌

매일 발전하는 IT문제해결사

국비지원교육 (22.01-22.07)/강의노트

22-02-18(금) 017일차 [Java] 상속, 추상클래스, 인터페이스 예제 연습, ArrayList

옌炎 2022. 4. 20. 02:05
728x90

수업내용


1교시 (09:30-10:20)

  • 지난 시간 복습 (상속)
상속 : 자바에서 자원(데이터 : 클래스 묶음 : 변수 + 함수)을 사용하려고 하는 것
--------------
1. 자바 프로그램을 구성하는 최소 단위는 클래스이다.
2. 클래스 구성원 = 변수 + 함수
3. 변수 : 데이터를 담는 상자 : 데이터?
4. 데이터 : 오브젝트 : Object : 자바가 다루는 모든 것 : 최소 단위는 클래스

5. 클래스를 사용하려면 클래스를 메모리에 올려야 한다.
6. 클래스를 메모리에 올리는 방법
   static
   new 인스턴스
   inheritance(상속)
   		클래스의 종류
			일반 클래스
			추상 클래스
			인터페이스 클래스
7. 상속에서 사용하는 키워드
	extends : 클래스 단일 상속, 인터페이스 단일 상속
	implements : 클래스에 인터페이스 다중 상속 : 구현체를 실현하다. : {} 블록에 재정의하기
	@Override : 오버라이드 어노테이션

2교시 (10:30-11:20)

  • 지난 시간 복습 (추상 클래스, 인터페이스)
인터페이스 
--------------
1. 인터페이스는 클래스이다. 키워드 interface로 선언하고 클래스를 만들어서 컴파일하면 .class가 만들어진다.
2. 완벽한 추상 클래스이다. 추상 함수만 와야 한다. 멤버변수는 모두 상수이다.
3. 인터페이스에 선언한 추상함수는 무조건 다 오버라이딩 해야한다. <-- 어댑터 클래스가 만들어진다.
4. 인터페이스를 사용할 때는 부모 인터페이스를 먼저 선언해서 상속한 자식클래스를 인스턴스해서 사용한다.
=================================================================================
5. 인터페이스 클래스가 만들어져 있으면 추상함수를 구현한(실현한) 클래스가 꼭 있어야 한다.
   인터페이스 + 자식클래스
=================================================================================
6. 인터페이스는 new 연산자를 사용하면 안된다.

추상 클래스
--------------
1. 자원을 사용하려고 하는 클래스
2. 일반 클래스와 추상 클래스의 차이는?
3. 추상 함수를 갖는다. : [Acess Modifier] abstract 리턴형 함수이름();
4. 추상 클래스는 abstract 키워드를 가져야 한다.
5. 추상 함수는 상속해서 사용해야 한다.
6. 나머지는 일반 클래스의 특징을 갖는다.
7. new 연산자를 사용하면 안 된다.
8. 추상 클래스를 상속할 때는 extends 키워드를 사용한다.

3교시 (11:30-12:20)

  • 이클립스 zip파일 활용법
    • 압축 해제 후 eclipse 폴더 복사하여 eclipse_java 경로 아래 붙여 넣기
    • eclipse_java_work 경로의 src에 소스 저장

4교시 (12:30-13:20)

  • [교재 222p] ArrayList
  • ArrayListTest_1
package a.b.c.ch4;

import java.util.ArrayList;

import a.b.c.ch3.HelloVO;

// a.b.c.ch3.HelloVO.java

public class ArrayListTest_1 {
	
	public void arrayListTest_1() {
		
		// ArrayList aList = new ArrayList();
		
		// ArrayList 클래스를 선언
		// 데이터는 HelloVO만 사용하라고 제네레이션을 선언했다.
		ArrayList<HelloVO> aList = new ArrayList<HelloVO>();
		
		// for문이 0부터 3번 돈다.
		for (int i=0; i < 3; i++) {
			
			// helloVO 클래스 인스턴스한다.
			HelloVO hvo = new HelloVO();
			// HelloVO 클래스 참조변수 hvo 출력
			System.out.println("\nhvo 참조변수 주소값 >>> : " + hvo + "\n");
			
			// hvo setter() 함수에 값 초기화, 값 바인딩, 값 대입
			hvo.setMid("CHEB");
			hvo.setMpw("CHEB00");
			hvo.setMname("차은비");
			
			// ArrayList 갯수 출력
			System.out.println("aList.size() >>> : " + aList.size());
			// ArrayList 참조변수 출력
			System.out.println("aList >>> : " + aList);
			
			// ArrayList에 aList 참조변수를 이용해서 add() 함수에 HelloVO 객체를 hvo 참조변수로 넣는다.
			// aList 참조변수가 기리키는 ArrayList 객체에 hvo 주소값이 바인딩된다.
			aList.add(hvo);
			
			// 문자열 출력
			System.out.println("ArrayList에 데이터 넣은 후 >>> : ");
			// ArrayList hvo를 넣은 후 사이즈 출력
			System.out.println("aList.size() >>> : " + aList.size());
			// ArrayList hvo를 넣은 후 참조변수 출력
			System.out.println("aList >>> : " + aList);
		}
		System.out.println("aList.get(0) >>> : " + aList.get(0));
		System.out.println("aList.get(1) >>> : " + aList.get(1));
		System.out.println("aList.get(2) >>> : " + aList.get(2));
		
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("Hello");
		new ArrayListTest_1().arrayListTest_1();
	}

}

 

 


5교시 (14:30-15:20)

  • ArrayList에 대한 설명
  • 배열과 ArrayList의 차이

  • ArrayList와 Encapsulation

  • 깡통에 데이터 담기

  • ArrayListTest_1 (계속)
package a.b.c.ch4;

import java.util.ArrayList;

import a.b.c.ch3.HelloVO;

// a.b.c.ch3.HelloVO.java

public class ArrayListTest_1 {
	
	public void arrayListTest_1() {
		
		// ArrayList aList = new ArrayList();
		
		// ArrayList 클래스를 선언
		// 데이터는 HelloVO만 사용하라고 제네레이션을 선언했다.
		ArrayList<HelloVO> aList = new ArrayList<HelloVO>();
		
		// for문이 0부터 3번 돈다.
		for (int i=0; i < 3; i++) {
			
			// helloVO 클래스 인스턴스한다.
			HelloVO hvo = new HelloVO();
			// HelloVO 클래스 참조변수 hvo 출력
			System.out.println("\nhvo 참조변수 주소값 >>> : " + hvo + "\n");
			
			// hvo setter() 함수에 값 초기화, 값 바인딩, 값 대입
			hvo.setMid("CHEB_" + i);
			hvo.setMpw("CHEB00_" + i);
			hvo.setMname("차은비_" + i);
			
			// ArrayList 갯수 출력
			System.out.println("aList.size() >>> : " + aList.size());
			// ArrayList 참조변수 출력
			System.out.println("aList >>> : " + aList);
			
			// ArrayList에 aList 참조변수를 이용해서 add() 함수에 HelloVO 객체를 hvo 참조변수로 넣는다.
			// aList 참조변수가 기리키는 ArrayList 객체에 hvo 주소값이 바인딩된다.
			aList.add(hvo);
			
			// 문자열 출력
			System.out.println("ArrayList에 데이터 넣은 후 >>> : ");
			// ArrayList hvo를 넣은 후 사이즈 출력
			System.out.println("aList.size() >>> : " + aList.size());
			// ArrayList hvo를 넣은 후 참조변수 출력
			System.out.println("aList >>> : " + aList);
		}
		
		for (int i=0; i < aList.size(); i++) {
			System.out.println("aList.get(" + i + ") >>> : " + aList.get(i));
			
			HelloVO _hvo = aList.get(i);
			System.out.print(_hvo.getMid() + " ");
			System.out.print(_hvo.getMpw() + " ");
			System.out.println(_hvo.getMname());
		}

	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("Hello");
		new ArrayListTest_1().arrayListTest_1();
	}

}

6교시 (15:30-16:20)

  • 배열_Test
package a.b.c.ch4;

public class 배열_Test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		int iV[][] = new int[][] {{1, 2, 3}, {4, 5, 6}};
		
		System.out.println("iV 참조변수 주소값 >>> : " + iV);
		
		
		for (int i=0; i < iV.length; i++) {
			// 1차원 배열은 주소값만 보임
			System.out.println(">>> : " + iV[i]);
			
			for (int j =0; j < iV[i].length; j++) {
				// 2차원 배열에서는 값을 볼 수 있음
				System.out.println(">>> : " + iV[i][j]);
			}
		}

	}

}
/*
	iV 참조변수 주소값 >>> : [[I@15db9742
	>>> : [I@6d06d69c
	>>> : 1
	>>> : 2
	>>> : 3
	>>> : [I@7852e922
	>>> : 4
	>>> : 5
	>>> : 6
*/

7교시 (16:30-17:20)

  • ArrayListTest_2
package a.b.c.ch4;

import java.util.ArrayList;

import a.b.c.ch3.HelloVO;

public class ArrayListTest_2 {
	
	public void arrayListTest_1(ArrayList<HelloVO> aList) {
		
		for (int i=0; i < aList.size(); i++) {
			System.out.print("aList.get(" + i + ") >>> : " + aList.get(i));
			
			HelloVO _hvo = aList.get(i);
			System.out.print(_hvo.getMid() + " ");
			System.out.print(_hvo.getMpw() + " ");
			System.out.println(_hvo.getMname());
		}
	}
	
	// 자바에서는 이런 식으로 깡통 내부의 데이터를 인자로 받아 하나씩 접근하지 않음
	public void arrayListTest_2(String mid, String mpw, String mname) {
		
		System.out.print(mid + " ");
		System.out.print(mpw + " ");
		System.out.println(mname);
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		// 멤버변수, 데이터 타입 String, 참조변수
		String mid = "CHEB_";
		String mpw = "CHEB00_";
		String mname = "차은빈_";
		
		// 멤버변수, class/데이터, 참조변수
		ArrayList<HelloVO> aList = new ArrayList<HelloVO>();
		
		for (int i=0; i < 3; i++) {
			HelloVO hvo = new HelloVO();
			
			hvo.setMid(mid + i);
			hvo.setMpw(mpw + i);
			hvo.setMname(mname + i);
			
			aList.add(hvo);
		}
	
		ArrayListTest_2 al2 = new ArrayListTest_2();
		al2.arrayListTest_1(aList);
		
		ArrayListTest_2 al3 = new ArrayListTest_2();
		al3.arrayListTest_2(mid, mpw, mname);
	
	}

}
  • ArrayListTest_3
package a.b.c.ch4;

import java.util.ArrayList;
import java.util.Scanner;

import a.b.c.ch3.HelloVO;

public class ArrayListTest_3 {

	public ArrayList<HelloVO> arrayListTest_1(HelloVO hvo) {
		
		// 비즈니스 로직을 거쳐서 데이터베이스에 다녀오는 로직이 들어올 예정이다.
		
		ArrayList<HelloVO> aList = null;
		aList = new ArrayList<HelloVO>();
		
		aList.add(hvo);
		
		return aList;
	}
		
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		String mid = "";
		String mpw = "";
		String mname = "";
		
		System.out.println("데이터를 입력하시오 >>> : ");
		Scanner sc = new Scanner(System.in);
		
		System.out.println("아이디 >>> : ");
		// next() 문자열 받는 함수
		mid = sc.next();
		System.out.println("패스워드 >>> : ");
		mpw = sc.next();
		System.out.println("이름 >>> : ");
		mname = sc.next();
		
		HelloVO hvo = new HelloVO();
		hvo.setMid(mid);
		hvo.setMpw(mpw);
		hvo.setMname(mname);
		
		ArrayListTest_3 al3 = new ArrayListTest_3();
		
		// public ArrayList<HelloVO> arrayListTest_1(HelloVO hvo)
		// 함수에 데이터 대입
		ArrayList<HelloVO> aList = al3.arrayListTest_1(hvo);
		
		if (aList != null && aList.size() > 0) {
			
			for (int i=0; i < aList.size(); i++) {
				System.out.println("aList.get(" + i + ") >>> : " + aList.get(i));
				
				HelloVO _hvo = aList.get(i);
				System.out.print(_hvo.getMid() + " ");
				System.out.print(_hvo.getMpw() + " ");
				System.out.println(_hvo.getMname());
			}
		}
		else {
			System.out.println("데이터의 입력과 출력 중 어디인지 모르겠지만 잘못되었네요");
		}
	}

}

8교시 (17:30-18:30)

  • LoginService
package a.b.c.ch4;

import java.util.ArrayList;

import a.b.c.ch3.HelloVO;

public interface LoginService {

	public ArrayList<HelloVO> loginTest(HelloVO hvo);
}
  • LoginServiceImpl
package a.b.c.ch4;

import java.util.ArrayList;

import a.b.c.ch3.HelloVO;

public class LoginServiceImpl implements LoginService {

	@Override
	public ArrayList<HelloVO> loginTest(HelloVO hvo) {
		// TODO Auto-generated method stub
		
		// 비즈니스 로직을 거쳐서 데이터베이스에 다녀오는 로직이 들어올 예정이다.
		ArrayList<HelloVO> aList = null;
		aList = new ArrayList<HelloVO>();
		
		aList.add(hvo);
		
		return aList;
	}

}
  • LoginTest
package a.b.c.ch4;

import java.util.ArrayList;
import java.util.Scanner;

import a.b.c.ch3.HelloVO;

public class LoginTest {

	public int loginTest(HelloVO hvo) {
		
		int nCnt = 0;
		
		LoginService ls = new LoginServiceImpl();
		
		ArrayList<HelloVO> aList = ls.loginTest(hvo);
		
		if (aList != null && aList.size() > 0 ) {
			
			for (int i=0; i < aList.size(); i++) {
				
				HelloVO _hvo = aList.get(i);
				
				if (   "KID".equals(_hvo.getMid().toUpperCase())
					&& "KPW".equals(_hvo.getMpw().toUpperCase())) {
					
					nCnt ++;
				}
			}
		}
		
		return nCnt;
	}
	
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		String mid = "";
		String mpw = "";
		
		System.out.println("데이터를 입력하시오 >>> : ");
		Scanner sc = new Scanner(System.in);
		
		System.out.println("아이디 >>> : ");
		mid = sc.next();
		System.out.println("패스워드 >>> : ");
		mpw = sc.next();
		
		HelloVO hvo = new HelloVO();
		hvo.setMid(mid);
		hvo.setMpw(mpw);
		
		LoginTest lt = new LoginTest();
		
		// public int loginTest(HelloVO hvo)
		int nCnt = lt.loginTest(hvo);
		System.out.println("nCnt >>> : " + nCnt);
		
		if (nCnt == 1) {
			System.out.println("로그인 성공 >>> : ");
		} else {
			System.out.println("로그인 실패 >>> : ");
		}
	}
}

Notes

  • 이클립스에서 함수 클릭하고 F3 누르면 해당 함수 작성 부분으로 감
  • LoginService, LoginServiceImpl, LoginTest 주석 달아오기

 

728x90