
package _14_01_inheritance;
class Animal {
String name;
public void cry() {
System.out.println(name + " is crying.");
}
}
class Dog extends Animal {
Dog(String name) {
this.name = name;
}
public void swim() {
System.out.println(name + " is swimming");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog("코코");
dog.cry(); //Animal에 지정된 함수
dog.swim();
Animal dog2 = new Dog("미미");
/* 실제 객체는 Dog지만, 변수 선언 type은 Animal이여서 , Dog2가 Dog에 해당하는 내용을 가지고 있지만, Animal에 있는 기능밖에 사용 불가 */
dog2.cry();
// dog2.swim();
}
}
// 여러 class를 상속 받을 수 없음. 1개만 가능.
package _14_02_overloading;
/*
한 class 내에서 동일한 이름의 methode를 여러개 갖는 것.
이름이 같다고 무조건 overloading은 아님
조건1. methode 이름이 동일
조건2. 매개변수 갯수 | type 달라야 함
*/
public class Main {
public static void main(String[] args) {
}
int add(int x, int y, int z){
return x+y+z;
}
long add(int a, int b){
return a+b;
}
}
/*
long add(int a, int b, int c){
return a+b+c;
}
함수 이름, 매개 변수의 개수와 type이 같으니 overloding이 아니다.
(return type이 다른 것은 관계 없다.)
*/
/* 기존에 없는 새로운 method를 정의하는데 같은 이름을 가지는 함수를 정의 */
package _14_03_overriding;
class Animal {
String name;
String color;
public Animal(String name, String color) {
this.name = name;
this.color = color;
}
public void cry() {
System.out.println(name + " is crying");
}
}
class Dog extends Animal { //Animal을 Dog가 상속 받는다.
public Dog(String name, String color) {
super(name, color);
}
@Override //Override 한 것을 나타내기 위해 표시 해준다. (안 해도 됨)
public void cry() {
System.out.println(name + " is barking"); //Animal의 cry() 함수를 override
}
}
public class Main {
public static void main(String[] args) {
Animal dog = new Dog("코코", "갈색");
dog.cry();
}
}
/* 부모에 있는 똑같은 함수를 자식이 구현해서 부모에 있는 함수를 없는 체 하는 것 */
package _15_01_accessModifier; //JAVA는 패키지 이름까지 포함한 클래스 이름을 인식함.
/*
접근 제어자 (access modifier)
접근 제어자는 멤버변수, 함수 혹은 클래스 앞에 붙어 사용.
외부에서의 접근을 제한하는 역할을 합니다.
크게 4종류 [범위 1(좁은) -> 4(넓은)]
1.private : 같은 클래스 내 접근
2.default(nothing) : 같은 패키지 내에서 접근
3.protected : 같은 패키지 내 | 다른 패키지의 자손 클래스(자신이 상속 받은 클래스)에서 접근
4.public : 접근제한 없음
*/
import _15_01_accessModifier.pkg.ModifierTest;
import java.util.Scanner;
class Child extends ModifierTest{
void callParentProtected() { // ⓛ 앞에 아무것도 안붙었기 때문에 '패키지 default' 이다.
System.out.println("call my parent's protected method");
super.messageProtectied(); //super.는 내가 상속받은 이 부모클래스(ModifierTest)를 가르키는 키워드.
}
}
public class Main {
public static void main(String[] args) {
ModifierTest modifierTest = new ModifierTest();
modifierTest.messageOuteside();
// modifierTest.messageInside();
// modifierTest.messageProtectied();
// modifierTest.messagePackagePrivate(); ⓛ 다른 패키지 안에 있어서 호출 안됨
Child child = new Child(); // ⓛ Child 클래스가 같은 패키지 안, 같은 최상위 패키지 안에 있기에 호출 됨.
child.callParentProtected(); //자식 클래스(Child)가 부모클래스(ModifierTest)를 상속 받아, 자식클래스 형태로 내려 주면 호출가능.
}
}
/*
modifierTest.messageOuteside(); 의 출력값
This is public modifier
This is private modifier
//access modifier가 private지만 같은 클래스 내의 messageOuteside는 public 이여서 안에 담겨 나오는 듯 ?
*/
/*
접근제어자 사용 이유
데이터 감추기(data hiding), 객체지향개념의 캡슐화(encapsulation)
클래스내부의 선언된 데이터를 부적절한 사용으로부터 보호.
내부적으로 사용되는 부분을 감추기 위해.
접근제어자는 캡슐화(encapsulation)가 가능하도록 돕는 자바 언어의 도구
*/
package _15_01_accessModifier.pkg;
public class ModifierTest {
private void messageInside(){
System.out.println("This is private modifier");
}
public void messageOuteside() {
System.out.println("This is public modifier");
messageInside();
}
protected void messageProtectied() {
System.out.println("This is protected modifier");
}
void messagePackagePrivate() {
System.out.println("This is package private modifier");
}
}
package _16_01_abstractClass;
abstract class Bird {
private int x,y,z;
void fly(int x, int y, int z){
printLocation();
System.out.println("이동합니다.");
this.x = x;
this.y = y;
if(flyable(z)){ //2.진짜 함수처럼 사용.
this.z = z;
} else {
System.out.println("그 높이로는 날 수 없습니다.");
}
printLocation();
}
abstract boolean flyable(int z); // 1.구현체가 없음
public void printLocation() {
System.out.println("현재위치 {"+x+ ", " +y+ ", "+ z +")");
}
}
class Pigeon extends Bird {
@Override
boolean flyable(int z) { //3.자식한테가서 구현.
return z<10000;
}
}
class Peacock extends Bird {
@Override
boolean flyable(int z) {
return false;
}
}
public class Main {
public static void main(String[] args) {
Bird pigeon = new Pigeon(); //4.자식객체가 초기화(?) 된후에 하나의 코드처럼 실행.
Bird peacock = new Peacock();
System.out.println("---비둘기---");
pigeon.fly(1,1,3);
System.out.println("---공작새---");
peacock.fly(1,1,3);
System.out.println("---비둘기---");
pigeon.fly(1,1,30000);
}
}
/*
추상클래스
추상클래스란 추상 메소드를 선언 할수 있는 클래스를 의미해요
그리고 이 추상클래스는 클래스랑 다르게 상속받는 그 자식 클래스 없이
그 자체로 인스턴스를 생성 할 수 없습니다
앞에서 했던
Animal = New Dog()
Animal type = New Animal() 이 가능하지만
Animal이 추상클래스라면
Animal = New Dog() 가능
Animal type = New Animal() 불가능
설계만 되어있고 구현체가 없는 것.
method의 시그니쳐, return type은 뭐고, 함수 이름은 뭐고, 파라미터는 뭘 받을거야.는 선언하지만
중괄호 안에 있는 블럭 구현체는 없다는 것이고 그것은 자식클래스에서 모두 구현해야 된다는 것을의미.
*/
package _16_02_interface;
interface Flyable {
void fly(int x, int y, int z);
}
class Pigeon implements Flyable {
private int x, y, z;
@Override
public void fly(int x, int y, int z) {
printLocation();
System.out.println("날아갑니다.");
this.x = x;
this.y = y;
this.z = z;
printLocation();
}
public void printLocation() {
System.out.println("현재위치 {" + x + ", " + y + ", " + z + ")");
}
}
public class Main {
public static void main(String[] args) {
Flyable pigeon = new Pigeon();
pigeon.fly(1,2,3);
}
}
/*
인터페이스는 가장 간단한 문법인데요
객체의 특정행동의 특징을 정의
함수의 특징이 method 시그니쳐 접근제어자, return type, method 이름만 정의
함수의 내용은 abstract method 처럼 없다.
인터페이스를 구현하는 클래스는
인터페이스에 존재하는 상세내용을 반드시 구현하여야합니다.
interface MyInterface {
void myMethod(int x);
}
*/
/*
인터페이스는
구현하는 객체의 동작에 명세, 다중상속 가능, implement라는 키워드를 이용하여 구현
method 시그니처만 선언이 가능하다
메서드 시그니처는 메서드 명과 파라미터의 순서, 타입, 개수를 의미
*/
/*
추상클래스는 일반클래스와 같은데
그것으로 객체를 생성할 순 없다.
extend 키워드를 쓰고 다중상속 불가능
추상 method에 대해서만 자식클래스에서 무조건 구현해야 된다.
*/
뭔가 이상하다..
강의는 분명 10~20분짜린데 막힘 없이 이해 해도, 따라 치고 내용정리하고 하면 4~50분이 걸리고,
이해 못해서 검색 하면서 하면 1시간~ 최대 몇시간 까지도 걸리는 신기한 마법.
엄청 잘 못하고 있는 느낌인데......... 열심히 알고 넘어가려해도 퀴즈만 나오면 막힌다...
대체.. 스피드는 왜 지정을 하는걸까?.. 스피드가 있지만 이동거리엔 전혀 반영되어있지 않는 문제..
1시간만에 5강의를 들어버리시는 분도 있는데... 난 왜지 ㅠㅠ..
'TIL' 카테고리의 다른 글
| 22.11.17_TIL_내배캠 (1) | 2022.11.18 |
|---|---|
| 22.11.16_TIL_내배캠 (2) | 2022.11.16 |
| 22.11.14_TIL_내배캠 (0) | 2022.11.14 |
| 22.11.13_TIL (0) | 2022.11.13 |
| 22.11.11_TIL_내배캠 (0) | 2022.11.11 |