Copy codeThe Code is as follows:
<? Php
/** The php language supports object-oriented programming. Anyone who has learned java and C ++ knows about object-oriented programming!
* If you do not know, just ask baidu.
*/
// Let's define a class. The key word of the defined class is "class"
Class computer {
// You can create methods and variables in the class.
// Define an addition
Function add ($ n1, $ n2 ){
$ Sum = $ n1 + $ n2;
Return $ sum;
}
// Define a subtraction
Function jf ($ n1, $ n2 ){
$ Sum = $ n1-$ n2;
Return $ sum;
}
// Define a multiplication
Function cf ($ n1, $ n2 ){
$ Sum = $ n1 * $ n2;
Return $ sum;
}
}
/**
To use methods in a class, you must create class objects and call methods with objects.
The following describes how to call a class.
*/
// $ Com is the computer class object. The new keyword is used to create the object.
$ Com = new computer ();
// Call the method in the class with the Object Name> method name (parameter, parameter ...)
Echo $ com-> add (1, 2 );
Echo $ com-> jf (1, 2 );
Echo $ com-> cf (1, 2 );
// If these methods are called by buttons, you can simply compile a calculator,
// Each button corresponds to a method
?>