如何写好一个类
学了一段时间类,也看了一些别人写的类,自己也在被窝里想了一下类,可动真格的还是有点对它摸不透,当然很多书上都讲类,这个不用大家再复制了。
类这玩意主要是里面的构造函数,属性,以及那些方法之间的逻辑,即为什么要写它,写它的犯罪动机是啥,
这方面很多书里没有,大家可以复制。
类到底怎么写呢?
我的意思是这样的,就比电影里演的一伙人去抢淫行,事先肯定要采点,然后画图分析地形,接着分工,李三做啥,李四做哪个,王二麻子放风,然后谁来开车接应,诸如此类,那么,类相对应的是啥呢
回复讨论(解决方案)
这个比喻好那什么。。。。你对抢银行还蛮了解的
通过这个比喻我说想让大家说说写程序的动机,呵呵,可能有点不恰当,比喻嘛。
写程序的动机 就是让计算机按照自己的想法干活,自己可以去玩...
首先得先明白,类存在的意义有哪些,它起到了哪些作用,给程序带来哪些便利。
至于怎么写类,你可以从最简易的开始,把一些有关联的方法、变量封装到一个class里面,可以加以利用成为一个解决问题的功能。最重要的还是要有面向对象的思想,才可以更好的利用类!
和你举个例子吧。就拿电脑和你举例,电脑是由哪些部件组成的和之前的关系。电脑好比一个类,电脑由:显示器、键盘、鼠标、主机箱、主板、CPU、显卡、网卡、声卡等,这些部件就是电脑的属性和方法
如果要去抢很多家银行,人员分工是相同的,但时间、地点是不同的
你的类就是干这事,每个人是类的方法,时间地点就是参数,并转换为属性,构造函数就是根据不同的参数得出不同的结果??金额
and 警察就是解构函数……destory!
如果要去抢很多家银行,人员分工是相同的,但时间、地点是不同的
你的类就是干这事,每个人是类的方法,时间地点就是参数,并转换为属性,构造函数就是根据不同的参数得出不同的结果??金额
and 警察就是解构函数……destory!
嗯,不错,你肯定能获得分了。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.
