상속
super
다단계상속 계층적 상속
다중상속 제외
https://coding-factory.tistory.com/865
오류: There is no default constructor available in
super() 안에 부모함수의 생성자 모두 넣어줘야함
override 원리
하위클래스의 필드 출력하기
https://nittaku.tistory.com/87
클래스 확인하기 조건문
https://ko.javascript.info/instanceof
https://velog.io/@hkoo9329/%EC%9E%90%EB%B0%94-extends-implements-%EC%B0%A8%EC%9D%B4
class 를 상속받을 때는 extends
인터페이스를 상속받을 때는 implements
전자를 상속받을 때는 일부만 구현해도 되고
후자는 다 구현해야 한다.
https://st-lab.tistory.com/153
public class RuleOfBiodome04 {
public static void main(String[] args) {
// 동물, 식물, 미생물 객체 생성
AnimalFeature animal1 = new AnimalFeature("귀여움", "포유류", "20년");
AnimalFeature animal2= new AnimalFeature("얼룩말", "동물", "잘 달린다, 포유류, 10년");
PlantFeature plant1= new PlantFeature("보라색", "열매 없음", "7월");
PlantFeature plant2= new PlantFeature("분홍색", "열매 있음", "3월");
MicrobeFeature microbe1= new MicrobeFeature("호흡 및 발효 대사","약산성");
MicrobeFeature microbe2= new MicrobeFeature("호흡 대사", "약산성");
BiologicalEntity<Feature> cat = new BiologicalEntity<>("고양이", "동물", animal1);
BiologicalEntity<Feature> zebra = new BiologicalEntity<>("얼룩말", "동물", animal2);
BiologicalEntity<Feature> rosemary = new BiologicalEntity<>("로즈마리", "식물", plant1);
BiologicalEntity<Feature> blossom = new BiologicalEntity<>("벚꽃", "식물", plant2);
BiologicalEntity<Feature> ecol = new BiologicalEntity<>("이콜라이", "미생물", microbe1);
BiologicalEntity<Feature> bacill = new BiologicalEntity<>("바실러스", "미생물", microbe2);
// BiologicalSystem 객체 생성 및 생물 정보 추가
BiologicalSystem<Feature> system = new BiologicalSystem<>();
system.add(cat);
system.add(zebra);
system.add(rosemary);
system.add(blossom);
system.add(ecol);
system.add(bacill);
// 가장 최근에 등록된 생물 정보 삭제 및 출력
system.delete();
system.show();
// 생물 정보 리스트가 비어있는지 확인
system.isEmpty();
system.clear();
}
}
interface Feature { // 타입 상관없이 생성(껍데기)
}
public class BiologicalEntity<T> {
private String name; // 이름
private String sort; // 분류
private T feature; // 종별 특징
// 생성자
public BiologicalEntity(String name, String sort, T feature) {
this.name = name;
this.sort = sort;
this.feature = feature;
}
// getter, setter
public String getName() {
return name;
}
// toString 메서드
@Override
public String toString() {
return ","+sort+","+feature.toString();
//name + ", " + sort + ", " + feature.toString();
}
// <S> void genericMethod(S o) {// [접근 제어자] <제네릭타입> [반환타입] [메소드명]([제네릭타입] [파라미터])
// // 제네릭 함수
// // - 클래스에서 지정한 제네릭유형과 별도로 메소드에서 독립적으로 제네릭 유형을 선언하여 쓸 수 있다
// // 제네릭이 사용되는 메소드를 정적메소드로 두고 싶은 경우 제네릭 클래스와 별도로 독립적인 제네릭이 사용되어야 한다
// return;
// }
}
class AnimalFeature implements Feature {
String species, feature, lifespan;
public AnimalFeature(String species, String feature, String lifespan) {
this.species=species;
this.feature=feature;
this.lifespan=lifespan;
}
public String toString() {
return species+","+feature+","+lifespan;
}
}
class PlantFeature implements Feature{
String color, fruit, flowering;
public PlantFeature(String color, String fruit, String flowering) {
this.color=color;
this.fruit=fruit;
this.flowering=flowering;
}
public String toString() {
return color+","+fruit+","+flowering;
}
}
class MicrobeFeature implements Feature{
String metabolism, environment;
public MicrobeFeature(String metabolism, String environment) {
this.metabolism=metabolism;
this.environment=environment;
}
public String toString() {
return metabolism+","+environment;
}
}
import java.util.ArrayList;
import java.util.List;
public class BiologicalSystem<T> {
public List<BiologicalEntity<T>> entities;
public BiologicalSystem() {
entities=new ArrayList<>();
}
// 새로운 생물 정보 추가
public void add(BiologicalEntity<T> entity) {
entities.add(entity);
System.out.println("새로운 생물이 등록되었습니다 : " + entity.getName());
}
public void delete() {
if (!entities.isEmpty()) { // 리스트가 비어있지 않을 때 실행
BiologicalEntity<T> entity = entities.remove(entities.size() - 1);
System.out.println("생물이 삭제 되었습니다 : " + entity.getName());
} else {
return;
}
}
public void clear() {
entities.clear();
System.out.println("모든 정보를 삭제했습니다.");
}
public void show() {
if (!entities.isEmpty()) {
System.out.println("최신 등록 생물 : "+ entities.get(entities.size() - 1).getName());
} else {
System.out.println("없습니다.");
}
}
public void isEmpty() {
if (entities.isEmpty()) {
System.out.println("생물 정보 리스트는 비어있습니다.");
} else {
System.out.println("생물 정보 리스트가 비어있지 않습니다.");
}
}
public void sortByName() {
}
}
class Comparable {
}