• PHP之abstract


     1 <?php
     2     //抽象类 : 用 abstract修饰的类 或 方法 。称为 抽象类或者抽象方法。
     3     //也不能实例化。
     4     //抽象方法没有方法体,,只有方法的声明。
     5     //抽象方法必须存在抽象类中。
     6     //子类继承抽象类 必须实现其抽象方法,也就是重写。
     7     //使用抽象方法的原因: 就是子类 一定存在,并且 在不同的实现,
     8     abstract class Goods{
     9         public $_name;
    10         public function __construct($_name){
    11             $this->_name=$_name;    
    12         }
    13         public abstract function getName();
    14     }
    15 
    16     class Book extends Goods{
    17         public $_author;
    18 
    19         //实现其父类的方法体
    20         public function getName(){
    21             $this->_name=$_name;
    22             $this->_author=$_author;
    23 
    24         }
    25     }
    26 
    27     class Phone extends Goods{
    28         public $_brand;
    29         //实现其父类的方法体
    30         public function __construct($_name, $_brand){
    31             parent::__construct($_name);
    32             $this->_brand=$_brand;
    33         }
    34 
    35         public function getName(){
    36             $this->_name=$_name;
    37             $this->_brand=$_brand;
    38         }
    39     }
    40 
    41     //实例化对象
    42     $book = new Book('php从入门到精通','mingrikeji');
    43     $book->getName();
    44     $p = new Phone('aa','aa');
    45     $p->getName();
  • 相关阅读:
    mybatis 使用缓存策略
    mybatis 使用事务处理
    mybatis 使用接口绑定
    mybatis 配置文件全解
    mybatis mapper映射文件全解
    mybatis中使用log4j
    初次使用Mybatis
    Servlet 实现文件上传与下载
    log4j v2版本的配置和使用
    Servlet 转发请求与重定向,以及路径问题
  • 原文地址:https://www.cnblogs.com/sharecorner/p/6126581.html
Copyright © 2020-2023  润新知