zoukankan      html  css  js  c++  java
  • 面向对象的认识----类的转化

    ---恢复内容开始---

     

    概述:

      为了更好的掌握面向对象的编程思维,推荐几种常用的小技巧,来快速提升面向对象的编程。

        1. 告别常量

        2. 告别变量

        3. 告别静态变量

        4. 告别函数

        5. 告别全局变量

        6. 告别Map数组  

    其它文章

      同类文章介绍篇:面向对象的认识----新生的初识面向对象的番外----思想的梦游篇(1)面向对象的认识---如何找出类

      面向对象实践篇:手把手教你做关键词匹配项目(搜索引擎)---- 第一天 、手把手教你做关键词匹配项目(搜索引擎)---- 第二十一天(新)

      负载均衡配置篇:负载均衡----概念认识篇负载均衡----实现配置篇(Nginx)负载均衡----文件服务策略

    技巧介绍

    1.常量转变成常类型

      常量实例:

    define("LEVEL_ERROR",'error');
    define("LEVEL_WARNING",'warning');
    define("LEVEL_INFO",'info');
    define("LEVEL_TRACE",'trace');

      常类型实例:

    class Level {
        const ERROR = 'error';
        const WARNING = 'warning';
        const INFO = 'info';
        const TRACE = 'trace';
    }

    2.变量转成属性

      变量实例:

    $userName = "张三";
    $userAge = 18;
    $userAddress = "xx省xx市xx镇xx村";

      属性实例:

    class User {
        private $name;
        private $age;
        private $address;
    
        /**
         * @param mixed $address
         */
        public function setAddress($address)
        {
            $this->address = $address;
        }
    
        /**
         * @return mixed
         */
        public function getAddress()
        {
            return $this->address;
        }
    
        /**
         * @param mixed $age
         */
        public function setAge($age)
        {
            $this->age = $age;
        }
    
        /**
         * @return mixed
         */
        public function getAge()
        {
            return $this->age;
        }
    
        /**
         * @param mixed $name
         */
        public function setName($name)
        {
            $this->name = $name;
        }
    
        /**
         * @return mixed
         */
        public function getName()
        {
            return $this->name;
        }
    
    
        public function __construct($name="",$age=0,$address=""){
            $this->name = $name;
            $this->age = $age;
            $this->address = $address;
        }
    }
    $user = new User("张三",18, "xx省xx市xx镇xx村");

    3.静态变量转成静态属性

      静态变量实例:

    static $app;
    if($app == null){
        $app = new App();
    }

      静态属性实例:

    class App {
        private static $app = null;
        public function instance(){
            if(self::$app == null){
                self::$app = new self;
            }
            return self::$app;
        }
    }

    4. 静态函数转成静态方法

      静态函数实例: 

    function version(){
        return "1.0.0.0";
    }

      静态方法实例:

    class App {
        public static function version(){
            return "1.0.0.0";
        }
    }

    5. 全局变量转成属性

      全局变量实例:

    function login($password){
        global $user;
        if($user->password === $password){
            return true;
        }
        throw new Exception("invalid password!");
    }

      属性实例: 

    class UserService {
    
        private $user;
    
        /**
         * @param mixed $user
         */
        public function setUser($user)
        {
            $this->user = $user;
        }
    
        /**
         * @return mixed
         */
        public function getUser()
        {
            return $this->user;
        }
    
        public function login($password){
            if($this->getUser()->password == $password){
                return true;
            }
            throw new Exception("invalid password!");
        }
    }

    6. Map数组转成对象

      Map数组实例:

    $orderItems = array();
    function addItem($product,$num,&$orderItems){
        $orderItems[] = array("product"=>$product,"num"=>$num);
    }

      对象实例:

    class Order {
        private $items;
    
        public function addItem($product,$num){
            $this->items[] = new OrderItem($product,$num);
        }
    }
    
    class OrderItem {
        private $product;
        private $num;
    
        public function __construct($product,$num){
            $this->product = $product;
            $this->num = $num;
        }
    }

    总结

      这些都是一些常用的技巧,熟练掌握他们把,也许对你们在以后的编程生涯中会带来乐趣也说不定。 

  • 相关阅读:
    经典博客
    Django中间介
    Django知识总结
    django认证系统
    Django-model基础知识,ORM,表操作
    Django-MTV
    web框架
    html总结(常用插件,补充知识,经典例题)
    快速搭建vue脚手架
    Djngo Rest Framework
  • 原文地址:https://www.cnblogs.com/oshine/p/3966212.html
Copyright © 2011-2022 走看看