2016-10-02 3 views
2

난 그냥 이런 식으로 코드를 작성 :이 오류의 첫 번째 부분을 이해추상 클래스 메소드 선언

Fatal error: Class test contains 2 abstract methods and must therefore be declared abstract or implement the remaining methods (test::getValue, test::prefixValue) in C:\wamp64\www\study\abstract.php on line 12

:이 코드를 실행하면

<?php 
class test 
{ 
// Force Extending class to define this method 
abstract protected function getValue(); 
abstract protected function prefixValue($prefix); 

// Common method 
public function printOut() { 
    print $this->getValue() . "\n"; 
} 
} 
class testabs extends test{ 

public function getValue() 
{ 

} 
public function prefixValue($f) 
{ 

} 
} 
$obj = new testabs(); 
?> 

, 나는 아래의 오류가 발생했습니다. 클래스 테스트를 추상으로 변경했는데 오류가 사라졌지 만 or 부분은 이해할 수 없습니다.

+0

_or_ 부분은 클래스가 'test' 클래스를 확장하는 관점에서입니다. 그것은 양방향으로 진행됩니다. 당신의'test' 클래스를 추상화 시키거나 상속 된 추상 선언을 구현하지 않기 때문에'test' 추상을 확장하는 다른 클래스를 만드십시오. – dbf

답변

4

추상 메소드를 추가하려면 클래스 abstract도 만들어야합니다. 그렇게하면 클래스를 인스턴스화 할 수 없으며 비 추상적 인 하위 클래스 만이 될 수 있습니다.

visibility (두 번째 하위 섹션 Method Visiblilty 참조)은 하위 클래스에서 동일하지 않습니다. 하위 클래스 외부의 코드에서 메서드를 호출 할 것인지 여부에 따라 testpublic 클래스의 (추상) 메서드를 만들거나 하위 클래스 메서드를 protected으로 만들 수 있습니다.

그리고이 설명 Class Abstraction page에서 두 번째 단락주의 :

When inheriting from an abstract class, all methods marked abstract in the parent's class declaration must be defined by the child; additionally, these methods must be defined with the same (or a less restricted) visibility. For example, if the abstract method is defined as protected, the function implementation must be defined as either protected or public, but not private

<?php 
abstract class test{ 
    // Force Extending class to define this method 
    abstract protected function getValue(); 
    abstract protected function prefixValue($prefix); 

    // Common method 
    public function printOut() { 
     print $this->getValue() . "\n"; 
    } 
} 
class testabs extends test{ 

    protected function getValue() 
    { 

    } 
    /** 
    * this method can be called from other methods with this class 
    * or sub-classes, but not called directly by code outside of this  class 
    **/ 
    protected function prefixValue($f) 
    { 

    } 
} 
$obj = new testabs(); 
// this method cannot be called here because its visibility is protected 
$obj->prefixValues();// Fatal Error 
?> 
+0

개체를 만들 때 오류가 발생했습니다. –

+0

좋아요, 대답을 업데이트했습니다. 새 첫 단락을 참조하십시오. –

1

클래스는 추상적 인 기능을 가지고 있지만 두 가지 선택이있다, 그래서 추상적으로 선언되지 않습니다. 클래스를 abstract으로 선언하거나 추상 함수의 구현을 제공하십시오.

첫 번째 옵션 (시도한)을 사용하면 클래스가 존재하며 함수를 구현하는 구체적인 하위 클래스에서 사용할 수 있습니다. 두 번째 옵션은 클래스가 완전히 정의되어있는 그대로 사용할 수 있음을 의미합니다.

0

classabstract 개의 메서드가있는 경우 abstract으로 선언해야합니다. 그래서 다음은 올바른 : 추상 클래스인터페이스 사이

<?php 
abstract class test 
{ 
    // Force Extending class to define this method 
    abstract protected function getValue(); 
    abstract protected function prefixValue($prefix); 

    // Common method 
    public function printOut() { 
    print $this->getValue() . "\n"; 
    } 
} 
0

주요 기술적 인 차이점은 다음과 같습니다

  • 추상 클래스 (메소드없이 상수, 회원, 메소드 스텁을 가질 수 있습니다 본문) 및 정의 된 메소드를 사용할 수 있지만 인터페이스에는 상수 및 메소드 스텁 만있을 수 있습니다.
  • 추상 클래스의 메서드 및 멤버는 모든 표시 유형으로 정의 할 수 있지만 인터페이스의 모든 메서드는 public으로 정의해야합니다 (기본적으로 public으로 정의되어 있음).
  • 추상 클래스를 상속하는 경우 구체적인 자식 클래스는 추상 메서드를 정의해야하지만 추상 클래스는 다른 추상 클래스를 확장 할 수 있으며 부모 클래스의 추상 메서드는 정의 할 필요가 없습니다.
  • 마찬가지로, 다른 인터페이스를 확장하는 인터페이스는 부모 인터페이스에서 메소드를 구현할 책임이 없습니다. 이는 인터페이스가 구현을 정의 할 수 없기 때문입니다.
  • 하위 클래스는 하나의 클래스 (추상 또는 콘크리트) 만 확장 할 수 있지만 인터페이스는 확장 할 수 있고 클래스는 여러 개의 다른 인터페이스를 구현할 수 있습니다.
  • 하위 클래스는 동일하거나 덜 제한적인 가시성을 가진 추상 메소드를 정의 할 수 있지만 인터페이스를 구현하는 클래스는 동일한 가시성 (공용)을 갖는 메소드를 정의해야합니다.