구체적 Vs 추상적 Vs 인터페이스 - [OOP & 자바#4]
                                            
                                                
                                                
                                                
                                                
                                                
                                                 15875 단어  oopjavacomputerscience
                    
만약 우리가 노트북 컴퓨터를 만들려고 한다면
class Laptop {
  String screen;
  int batteryHour;
  Laptop(String screen, int batteryHour) {
    this.screen = screen;
    this.batteryHour = batteryHour;
  }
  void moveCursor(){
    System.out.println("Using the trackpad to move the cursor");
  }
}
Laptop클래스는 화면 해상도를 나타내는 문자열과 배터리가 사용할 수 있는 시간을 나타내는 정수를 포함하는 구조 함수를 포함한다.그것은 뒤에 설명할 수 있는 moveCursor 방법이 하나 더 있다.Laptop ex1 = new Laptop("1080P",5);
Laptop ex2 = new Laptop("4k", 8);
데스크탑에도 화면이 있다는 점을 감안하여 다음과 같이 노트북 클래스를 데스크탑 클래스의 상위 클래스로 간단명료하게 만들 수 있습니다.
class Desktop extends Laptop {
  Desktop(String screen, int batteryHour){
    super(screen, batteryHour);
  }
}
이것이 바로 이런 예다.따라서 어떤 상황에서도 대상이 특정한 속성을 공유한다고 해서 하위 분류화하지 말아야 한다.이러한 솔루션이 LSP(Liskov 대체 원칙)를 위반할 수 있는 이유는 여러 가지가 있습니다.뚜렷한 문제
batteryHour는 aDesktop에게 의미가 없다는 것이다.노트북과 데스크톱은 모두 컴퓨터다.그들은 비슷한 점이 많다.그러나 데스크탑이 노트북이라고 말하지 않고, 반대로도 마찬가지라는 내 의견에 동의할 것이다.이것은 그들이 친자 관계에 적합하지 않다는 것을 설명한다.
콘크리트류
모두 컴퓨터인 것을 알고 우리는 첫 번째 효과적인 설계를 찾았다.
// Have Computer as a Concrete parent class
class Computer {
  String screen;
  void moveCursor(){
    System.out.println("moving the cursor?");
  }
}
class Laptop extends Computer {
  int batteryHour;
  Laptop(String screen, int batteryHour) {
    super.screen = screen;
    this.batteryHour = batteryHour;
  }
  @Override
  void moveCursor(){
    System.out.println("Using the trackpad to move the cursor");
  }
}
class Desktop extends Computer {
  Desktop(String screen){
    super.screen = screen;
  }
  @Override
  void moveCursor(){
    System.out.println("Using the mouse to move the cursor");
  }   
}
Computer클래스를 이 두 부류moveCursor 방법은 Desktop의 하위 클래스에서 다시 써야 한다.그러나 재작성을 생략하는 것은 컴파일러가 완전히 받아들일 수 있는 행위이다.new Computer().moveCursor()을 한다면 출력은 의미가 없을 수도 있다.추상류
두 번째 방법은
Computer를 추상류로 만드는 것이다.abstract class Computer {
  String screen;
  abstract void moveCursor();
}
class Laptop extends Computer {
  int batteryHour;
  Laptop(String screen, int batteryHour) {
    super.screen = screen;
    this.batteryHour = batteryHour;
  }
  @Override
  void moveCursor(){
    System.out.println("Using the trackpad to move the cursor");
  }
}
class Desktop extends Computer {
  Desktop(String screen){
    super.screen = screen;
  }
  @Override
  void moveCursor(){
    System.out.println("Using the mouse to move the cursor");
  }   
}
추상류
이것은 기본적으로 우리가 구체적인 과정에서 직면한 몇 가지 문제를 해결했다.부류를 실례화할 필요가 없다면 부류를 추상화하는 것이 통상적으로 더 좋다고 확신할 수 있다.현재 두 번째 방법의 단점 (적어도 자바에 대해서 말하자면) 은 하나의 클래스는 하나의 부모 클래스에서만 계승할 수 있다는 것이다.
충전할 수 있는 물체에 사용할
Chargeable 종류를 원한다면:abstract class Chargeable {
  abstract Chargeable charge();
}
다중 계승에 대한 방주
PineapplePen는 논리적인 대상이 될까요?class PineapplePen extends Pineapple, Pen{
//...
}
이음매
다음 옵션은 인터페이스입니다.
abstract class Computer {
  String screen;
  abstract void moveCursor();
}
interface Chargeable {
  void charge();
}
class Laptop extends Computer implements Chargeable {
  int batteryHour;
  Laptop(String screen, int batteryHour) {
    super.screen = screen;
    this.batteryHour = batteryHour;
  }
  @Override
  void moveCursor(){
    System.out.println("Using the trackpad to move the cursor");
  }
  @Override
  public void charge(){
    // pretend that it does something
    System.out.println("Charging the laptop");
  }
}
class Desktop extends Computer implements Chargeable {
  Desktop(String screen){
    this.screen = screen;
  }
  @Override
  void moveCursor(){
    System.out.println("Using the mouse to move the cursor");
  }
  @Override
  public void charge(){
    // pretend that it does something
    System.out.println("Providing power to the desktop");
  }
}   
요약
콘크리트류
추상류
이음매
구현
리얼
실제 요약
요약
실례화 가능
예, 그렇습니다.
아니오.
아니오.
다음까지...
Reference
이 문제에 관하여(구체적 Vs 추상적 Vs 인터페이스 - [OOP & 자바#4]), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/tlylt/concrete-vs-abstract-vs-interface-oop-java-4-1ln5텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)