OOPS 概念摘要和定稿

OOPS Concept Abstract and final

我们可以在抽象中创建一个 final 方法 class 吗?

我们可以在 final 中创建一个抽象方法 class 吗?

请给我解释编码。

提前感谢您的回答。

  1. 是的,你可以。
  2. 不,你不能。只能在抽象class中声明抽象方法。有关详细信息,请参阅 php documentation
1) can we create the final method in abstract class .
 yes we can .
 see the below code for further reference                                                                    /* final method in abstract class */
abstract class base {
    public $name;
    abstract public function getName();

    final public function setName($inputName) {
        $this->name = $inputName;
    }
}

class child extends base {

    public function getName() {
        echo $this->name;
    }
}



$obj = new child();
$obj->setName('Software Engineer');
$obj->getName();                          
2 . can we create a abstract method in final class ?                                                                                                                   No. we can not create the abstract method in final class,

不 不这样做 .

   class PersonalLoan{
     public final String getName(){
         return "personal loan";
     }
    }
    class CheapPersonalLoan extends PersonalLoan{
        @Override
        public final String getName(){
            return "cheap personal loan"; //compilation error: overridden method is final
        }
    }

但是抽象 class 有抽象方法。抽象方法必须在继承函数中定义,所以它不是