PHP 추상 클래스 구현 방법에 대한 자세한 설명

伊谢尔伦
풀어 주다: 2023-03-12 12:46:02
원래의
1976명이 탐색했습니다.

php에는 추상 클래스의 두 가지 예가 있는데, 하나는 간단하고 다른 하나는 PHP 추상 클래스를 배우기에 좋은 예입니다.

php 추상 클래스

<?php
//定义一个抽象类
  abstract class Staff
   {
      abstract function hire();
      abstract function fire();
      abstract function promote();
      abstract function demote();
   }
?>
로그인 후 복사

php 추상 클래스의 예

<?php
class Employee {
    private $title;
    private $lastName;
    private $firstName;
    protected $salary;
    private $ratio = 0; 
    public function construct($title, $firstName, $mainName, $salary ) { 
        $this->title     = $title;
        $this->firstName = $firstName;
        $this->lastName  = $mainName;
        $this->salary     = $salary;
    }
    public function firstName() {
        return $this->firstName;
    }
    public function getlastName() {
        return $this->lastName;
    }
    public function setRatio( $num ) {
        $this->ratio=$num;
    }
    public function getRatio() {
        return $this->ratio;
    }
    
    public function getTitle() {
        return $this->title;
    }
    public function getSalary() {
        return ($this->salary - $this->ratio);
    }
    public function getFullName() {
        return "{$this->firstName}" . " {$this->lastName}";
    }
    function getSummaryLine() {
        $base  = "$this->title ( $this->lastName, ";
        $base .= "$this->firstName )"; 
        return $base;
    }
}
//定义抽象类
abstract class EmployeeWriter {
    abstract static function write( Employee $shopProduct );
}
class TextEmployeeWriter extends EmployeeWriter {
    static function write( Employee $shopEmployee ) {
        $str  = "{$shopEmployee->getTitle()}: ";   
        $str .= $shopEmployee->getFullName();
        $str .= " ({$shopEmployee->getSalary()})\n";
        print $str;
    }
}
$developer1 = new Employee("A", "A1", "A2", 5.99 );
TextEmployeeWriter::write( $developer1 );
?>
로그인 후 복사






위 내용은 PHP 추상 클래스 구현 방법에 대한 자세한 설명의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
최신 이슈
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!