Java 강의를 복습하며 오 이런식으로 코드 짜보진 않았는데 되게 배워야겠다고 느낀 부분을 기록해 두기로 했다.
String name;
int age; int speed;
int x, y;
public Human(String name, int age, int speed, int x, int y) {
this.name = name;
this.age = age;
this.speed = speed;
this.x = x;
this.y = y;
}
public Human(String name, int age, int speed) {
this(name, age, speed, 0, 0)
}
위 코드에서 두번째 생성자가 첫번째 생성자를 사용하는 형태
Walkable - interface
Swimmable - interface
Runnable - interface
Human - parent class
human class 는 위 세개의 인터페이스를 상속하지 않음
human class를 상속한 3개의 offspring class들은 각각 위 인터페이스들 중 몇 개를 상속함.
Human grandpa = new Grandparents();
Human parent = new parents();
Human child = new child();
Human[] humans = { grandpa, parent, child };
for(Human human : humans) {
if(human instanceof Walkable) {
((Walkable) human).walk(1, 1);
}
if(human instanceof Runnable) {
((Runnable) human).run(1, 1);
}
if(human instanceof Swimmable) {
((Swimmable) human).swim(1, 1);
}
}
instanceof를 사용하여 enhanced for loop을 구현한 형태
날짜시간 구하기
// 날짜시간 출력(패턴지정)
DateTimeFormatter myFormatter2 = DateTimeFormatter.ofPattern("yyyy/MM/dd/HH:mm:ss");
String mydatetime = myFormatter2.format(LocalDateTime.now());
System.out.println(mydatetime);
// 시간 지정
DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedTime((FormatStyle.MEDIUM));
String shortFormat = formatter.format(LocalTime.now());
System.out.println(shortFormat);
//날짜 지정
DateTimeFormatter myFormatter = DateTimeFormatter.ofPattern("yyyy년MM월dd일");
String mydate = myFormatter.format(LocalDate.now());
System.out.println(mydate);
// 특정 날짜로 부터 얼마나(달, 일, 년 따로) 지났는지 출력
LocalDate today = LocalDate.now();
System.out.println(today);
LocalDate birthday = LocalDate.of(2020,1,1);
Period period = Period.between(today, birthday);
System.out.println(period.getMonths());
System.out.println(period.getDays());
stream 함수(람다 형식) 사용
List<String> list = new ArrayList<>();
list.add("korea");
list.add("japan");
list.add("france");
Stream<String> stream = list.stream();
stream.map(str -> str.toUpperCase(Locale.ROOT)).forEach(System.out::println);
stream = list.stream();
stream.map(str -> {
System.out.println(str);
return str.toUpperCase(Locale.ROOT);
}).forEach(System.out::println);
List<String> result = list.stream()
.limit(2)
.collect(Collectors.toList());
System.out.println(result);
Set<String> set = list.stream()
.filter(it -> "korea".equals(it))
.collect(Collectors.toSet());
//sout + Enter 단축키
System.out.println(set);
String[] arr = {"SQL", "Java", "Python"};
Stream<String> stringStream = Arrays.stream(arr);
stringStream.forEach(System.out::println);
List<Integer> list2 = Arrays.asList(1,2,3,4,5,6,7,8,9,10);
System.out.println(list2.stream().reduce(0, Integer::max));
List<String> names = Arrays.asList("김정우", "김호정", "이하늘", "이정희", "박정우", "박지현", "정우석", "이지수");
System.out.println(names.stream().filter(str -> str.startsWith("이")).count());
//http는 통신을 하기 위한 상태의 약속
//API는 데이터를 주고받는 형식에 대한 약속