星际争霸允许玩家作弊,当然这是在人和电脑对战的时候。而且作弊有个特点,比如快速建造,能量无限是对所有的玩家(包括电脑)都生效,如果关闭了作弊,对所有的玩家的作用都同时消失。
这也就是说如果我们把作弊状态作为一个类,他只能有一个对象。
待解决的问题:确保某个类只能有一个对象。
思路:把对外新建对象的权利都收回,包括new,clone。为了防止通过子类来覆盖父类的方法和成员,将类设置为final。用static成员来保存唯一的对象
单件模式示例:
<?php //将类设置为final,禁止其他类继承它 final class cheat { //快速建造的生效状态,用private保护 private $fastBuild = false; //用static成员来保存唯一的对象 private static $instance; //设置快速建造的生效状态的方法,用public为了能够公开调用 public function setStatus($input) { //如果输入的秘籍正确,operation cwal是快速建造的秘籍 if($input === 'operation cwal') { //像开关一样,逆反状态 $this->fastBuild = !$this->fastBuild ; } } //读取快速建造的生效状态的方法,用public为了能够公开调用 public function getStatus() { return $this->fastBuild ; } //获取唯一对象的唯一方法 public function getInstance() { //如果对象没有被新建,则新建它 if(!isset(self::$instance)) { self::$instance = new cheat() ; } return self::$instance ; } //用private来禁止在本类以外new对象 private function

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

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download
The most popular open source editor