search
Homephp教程php手册基于PHP 面向对象之成员方法详解

基于PHP 面向对象之成员方法详解

Jun 13, 2016 am 11:52 AM
phpmoveCanbased onrightmemberhandwritingmethoduseOwnDetailed explanationFor

用一个列子来读解成员方法:大家可以自己动手写一写,加深理解。

这样一个需求; 希望人可以说话, 做算术题.......,这样就需要使用到成员方法:
1、添加speak 成员方法,输出 我是小明
2、添加jisuan 成员方法,可以计算从 1+..+1000的结果
3、修改jisuan 成员方法,该方法可以接收一个数n,计算 1+..+n 的结果
4、添加add 成员方法,可以计算两个数的和

参考代码:

复制代码 代码如下:


    class Person{

        public $name;
        public $age;

        //添加成员方法
        public function speak(){

            echo "我是小明";
        }

        public function jisuan(){
            //计算从 1+..+1000的结果
            $result =0;
            for($i=1;$i                $result+=$i;
            }
            //把计算结果ruturn 返回
            return $result;
        }

        public function jisuan2($n){
            //计算从 1+..+n的结果
            $result=0;
            for($i=1;$i                $result+=$i;
            }
            //return
            return $result;
        }

        //计算2个数的和
        public function add($num1,$num2){
            return $num1+$num2;
        }
    }

    $person1=new Person;
    //人说话
    $person1->speak().'
';
    //计算
    echo '
'.$person1->jisuan();
    //echo '
'.$person1->jisuan(100);
    //带参数的
    echo '计算的结果是:'.$person1->jisuan2(5);
    //计算2个数的和
    echo "
50+50=".$person1->add(50,51);
?>


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

mPDF

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),

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment