T. 김동식 + 서치보완
접근제어자 (한정자, Modifier)
퍼프디피, 접근의 용이 순서
public, protected, 생략, private
getter, setter
set+변수명: 값 초기화
get+변수명: 접근하지 못하는 변수에 접근할수있도록 허용 = public
변수유형
local, 지역변수
함수에서 기능구현을 위해 잠시 사용
field (멤버변수, 인스턴스 변수)
클래스 속성을 나타내고 각 인스턴스마다 다른 값을 가진다.
static
여러 인스턴스에서 공유해 사용하도록 한번만 생성
Eclipse 단축키
shift F10 도구모음, alt shift S 소스
JAVA API
https://docs.oracle.com/javase/8/docs/api/
생성자 overloading
생성자가 두개이상, 매개변수가 여러개 적재
https://sanghee.tistory.com/55
JAVA
// length
int arr2[] = {1, 2, 3, 4, 5};
System.out.println(arr2.length);
// array + for
String arr[] = {"안녕1","안녕2", "안녕3", "안녕4", "안녕5"};
for(int i = 0; i<arr.length;i++) {
System.out.println(arr[i]);
}
System.out.println(arr);
System.out.println(arr[1]);
array + private
private String bookname;
private String author;
public Book(String bookname, String author) {
this.bookname = bookname;
this.author = author;
}
public String getBookname() {
return bookname;
}
public void setBookname(String bookname) {
this.bookname = bookname;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
// 필드가 퍼블릭일경우 접근해 출력가능
// System.out.println(bookArr[0].bookname);
Book bookArr[] = new Book[5];
bookArr[0] =new Book("책이름1", "저자1");
bookArr[1] =new Book("책이름2", "저자2");
bookArr[2] =new Book("책이름3", "저자3");
bookArr[3] =new Book("책이름4", "저자4");
bookArr[4] =new Book("책이름5", "저자5");
// 필드가 프라이빗일 경우 get으로 접근해 출력
System.out.println(bookArr[0].getBookname());
for (int i = 0; i < bookArr.length; i++) {
System.out.println(bookArr[i].getBookname() + ", " + bookArr[i].getAuthor());
}
싱글톤패턴
객체를 하나만 나오게 하는 패턴, 이후 서블릿에서 사용됨
자주사용되지않음, 안정성을 요구할때 이용.
*서블릿: 서버+애플릿, 서버쪽을 건드리는 웹 애플릿(작은 어플리케이션)
class Company
1. 변수를 하나 만들어 접근제한 + 고정한다.
private static Company instance = new Company();
2. 생성자를 private로 지정한다.
private Company() {}
3. getter를 작성해 return해준다.
public static Company getInstance(){
if(instance == null) {
instance = new Company();
}
return instance;
};
현재 값이 없음으로 if문을 써 호출되도록 하고, 변수에 리턴한다.
class CompanyTest
Company company = new Company(); //오류가 나는 것을 확인 할 수 있다.
Company mycompany1 =Company.getInstance();
Company mycompany2 =Company.getInstance();
System.out.println(mycompany1 == mycompany2); //true, 똑같다는것을 확인할 수 있다.
System.out.println(System.identityHashCode(mycompany1)); //주소값이 mycompany2와 같은것을 확인할 수 있다.
System.out.println(System.identityHashCode(mycompany2)); //주소값이 mycompany1와 같은것을 확인할 수 있다.
Python
인터프린터 방식: 한줄한줄 쳐서 실행한다
파이썬은 데이터타입은 생략가능하나, 값이 있어야 한다.
systax: 컴퓨터의 문법
1. 변수
Traceback (most recent call last): // 최근 활동:
File "<stdin>", line 1, in <module> // 표준입력 키보드,
NameError: name 'test1' is not defined // 이름 정의하지않았다.
name = '홍길동'
type(name)
<class 'str'>
2. 형변환
type(5.4)
<class 'float'>
>>> int(5.4) // 형 변환
5
>>> float(5) // 형 변환
5.0
>>> age = input()
23
>>> age
'23'
>>> type(age)
<class 'str'> // 기본값이 string
>>> int(age)+1 // 형변환을 해줘야
24
>>> age = int(input("당신의 나이는")) // 처음부터 형변환시켜도 ok
당신의 나이는23
>>> age+10
33
4. 포맷
%(formatting): 문자열에서 데이터타입으로 형식을 맞춰나오게 하는것.
>>> print("나는 오늘 %d시에 밥을 먹었다."%3) // digit 0~9까지 숫자
나는 오늘 3시에 밥을 먹었다.
>>> print("나는 오늘 %f시에 밥을 먹었다."%3) // float
나는 오늘 3.000000시에 밥을 먹었다.
>>> print("나는 오늘 %s시에 밥을 먹었다."%3) // String
나는 오늘 3시에 밥을 먹었다.
5. in
>>> 1 in [1, 2, 'g', "gg", True] //in:안에 있는가, 멤버쉽연산자
True
6. 리스트
*배열 vs 리스트
배열은 동일한 데이터 타입, 고정된 공간, 빠른계산으로 인공지능에 적합
리스트는 포인터구조(각 주소가 연결), 값을 추가하거나 삭제하기 쉽다.
>>> li = 1, 2, 3
>>> type(li)
<class 'tuple'> //tuple = 행,집합
>>> li = [1, 2, 3]
>>> type(li)
<class 'list'> //list
7. 사칙연산
>>> 3/2 //나누기
1.5
>>> 3//2 //몫
1
>>> 3%2 //나머지
1
>>> 3**2 //제곱
9
8. for문
>>> for i in range(5): //i = int i,
... print('hi')
...
hi
hi
hi
hi
hi
9. if문
>>> score = 96
>>> if score >= 90: // Python if문
... print("A학점")
... elif score >=80:// elif = else if
... print("B학점")
... else:
... print("C학점")
...
A학점
+ 유용한 앱: 알고리즘 도감, 가볍게 보기 좋다.
ANACONDA
파이썬우선 삭제,
아나콘다 prompt 실행후, 작업할 경로로 cd이동, jupyter notebook치면 아래와 같이 브라우저로 실행됨.