학원에서 학습한 내용을 개인정리한 글입니다.
public void basicTest() {
A_Parent parent = new A_Parent();
A_Child child = new A_Child();
A_Child2 child2 = new A_Child2();
//부모 클래스 타입의 변수는 자식 객체를 저장할 수 있다?
parent = new A_Child();
//X 상속관계가 아님!
// parent = new_A_Child();
//X 자식클래스 타입의 변수에 부모 객체를 저장할 수 있다?
// child = new A_Parnet();
}

//Sonata 클래스는 Car 클래스의 후손
Car c = new Sonata();
//Sonata클래스형에서 Car클래스형으로 바뀜
//Sonata 클래스는 Car 클래스의 후손
Car c = new Sonata();
((Sonata)c).moveSonata();
//Parent Class
public class A_Parent {
private String parentData;
public A_Parent() {
}
public A_Parent(String parentData) {
super();
this.parentData = parentData;
}
public String getParentData() {
return parentData;
}
public void setParentData(String parentData) {
this.parentData = parentData;
}
@Override
public String toString() {
return "parentData=" + parentData;
}
public void dynamicTest() {
System.out.println("부모 메소드");
}
}
//Child Class
public class A_Child extends A_Parent {
private int childAge;
public A_Child() {
}
public A_Child(String parentData, int childAge) {
super(parentData);
this.childAge = childAge;
}
public int getChildAge() {
return childAge;
}
public void setChildAge(int childAge) {
this.childAge = childAge;
}
@Override
public String toString() {
return "child=" + childAge;
}
public void dynamicTest() {
System.out.println("자식 메소드");
}
}
//Controller
//부모 클래스 타입의 변수에 자식 객체가 저장됐을 때,
//부모에 설정된 내용만 접근이 가능
System.out.println(parent);
parent.setParentData("부모");
System.out.println(parent.getParentData());
//원본 자식객체로 변경하려면 강제형변환을 해야한다
child = (A_Child)parent;
System.out.println(child.getParentData());
((A_Child)parent).setChildAge(100);;
System.out.println(((A_Child)parent).getChildAge());
//Object 클래스 타입의 변수는 자바에서 사용하는 모든 객체를 저장할 수 있다
Object o;
o = new String("test");
o = new Scanner(System.in);
o = new A_Parent();
o = new A_Child();
//객체배열 이용하기
Employee[] employee = new Employee[5];
Teacher[] teacher = new Teacher[5];
A_Person[] person = new A_Person[10];
person[0] = new Employee("ㅇㅇㅇ", 28, '여', 100, "개발");
person[1] = new Teacher("ㅁㅁㅁ", 28, '여', "영어", 3);
person[2] = new Employee("ㅅㅅㅅ", 28, '여', 100, "개발");
person[3] = new Student("ㅁㅁㅁ", 28, '여', new String[] {"자바"}, 3);
int sCount = 0, eCount = 0, tCount = 0;
int addResult = 0, totalCount = 0;
for (int i = 0; i <person.length; i++) {
//각 타입에 맞춰서 데이터 출력
//instanceof 연산자 이용
if(person[i] == null) {
continue;
}
if(person[i] instanceof Employee) {
Employee e = (Employee)person[i];
System.out.println(e.toString());
eCount++;
} else if (person[i] instanceof Teacher){
Teacher t = (Teacher)person[i];
System.out.println(t.toString());
tCount++;
if(t.getMajor().equals("영어")) {
System.out.println("선생이고 영어과목: " + t.toString());
}
} else if (person[i] instanceof Student) {
Student s = (Student)person[i];
System.out.println(s.toString());
sCount++;
}
addResult += person[i].getAge();
totalCount ++;
}
public A_Person makePerson(String title) {
switch(title) {
case "student":
return new Student();
case "teacher":
return new Teacher();
case "employee":
return new Employee();
default:
return null;
}
}
//추상 클래스 이용
//생성이 불가능함
// _AbstractParent ap = new B_AbstractParent();
//타입으로 선언은 가능
//자기 자신은 생성이 불가능
//자식은 선언 가능
B_AbstractParent ap;
//추상 클래스는 자식클래스가 상속해서 이용하는 클래스
ap = new B_AbstractParentChild();
System.out.println(ap.getTest());
//클래스에 선언된 메소드 중 추상메소드가 있으면
//반드시 추상클래스로 선언해야한다
package com.poly.model.vo;
public abstract class B_AbstractIncludeAbstractMethod {
public abstract void test();
}
public class B_AbstractImplement2 extends B_AbstractIncludeAbstractMethod{
@Override
public void test() {
// TODO Auto-generated method stub
System.out.println("2에서 구현!");
}
}
package com.poly.model.vo;
public class B_AbstractImplement extends B_AbstractIncludeAbstractMethod{
@Override
public void test() {
System.out.println("구현내용~");
}
B_AbstractIncludeAbstractMethod aiam;
aiam = new B_AbstractImplement();
aiam.test();
aiam = new B_AbstractImplement2();
aiam.test();
public void testCheck(B_AbstractIncludeAbstractMethod param) {
param.test();
}


() -> {} ==> public void ooo(){}
() -> {return 10} ==> public int ooo{}
Data()-> {} ==> public void ooo(paran){}
MyFunctionalInter test = new MyFunctionalInter() {
@Override
public boolean test(String a) {
return a.length()>5;
}
};
test = (msg) -> {return msg.charAt(0) == '자';};
testString((s) -> s.charAt(0) == 't');
public void testString(MyFunctionalInter func) {
if(func.test("test")) {
System.out.println("yes");
}else {
System.out.println("no");
}
}

