4. How to abstract a class?
As mentioned above, the unit of object-oriented program is the object, but the object is instantiated by the class, so
The first thing we have to do is how to declare the class. It is easy to make a class. , as long as you master the basic programming grammar definition rules, you can
do it, so what is the difficulty? How many classes and objects should be used in a project? Where the class needs to be defined, define
what kind of class it is, how many objects this class instantiates, how many attributes and how many methods there are in the class. Wait, this requires readers to analyze, design and summarize practical problems in actual development.
Definition of class:
class 类名{ }
is Once it’s defined, you just need to write code in it, but what should you write in it? What can I write? How to write a complete
class? As mentioned above, the purpose of using a class is to instantiate objects for us to use. This requires knowing what kind of object you want. Like what we mentioned above on an installation configuration sheet, The machine you put on is whatever it is. For example, a person is a target. How do you recommend a person you like to your leader? Of course, the more detailed the better:
First, you will introduce the person's name, gender, age, height, weight, phone number, home address, etc.
Then, you have to introduce what this person can do, whether he can drive, speak English, use a computer, etc.
As long as you introduce more, others will know more about this person. This is how we describe a person. Now let’s summarize
. All objects we use classes to describe are similar. From the above You can see from the human description that when you make a class, it is divided into two parts from the perspective of definition. The first is a static description, and the second is a dynamic description. The static description is what we call it.
Attributes, as we saw above, the person's name, gender, age, height, weight, phone number, home address, etc. Dynamically speaking, it is also the function of this human object. For example, this person can drive, speak English, can use a computer, etc. When abstracted into a program,
we write the dynamic as a function or method, function And the method is the same. Therefore, all classes are written in terms of attributes and methods. Attributes are also called member attributes of this class, and methods are called member methods of this class.
class 人{ 成员属性:姓名、性别、年龄、身高、体重、电话、家庭住址 成员方法:可以开车, 会说英语, 可以使用电脑 }
var $somevar; 方法(成员函数): 通过在类定义中声明函数,即创建了类的方法。 如: function somefun(参数列表) { ... ... } <?php class Person { //下面是人的成员属性 var $name; //人的名字 var $sex; //人的性别 var $age; //人的年龄 //下面是人的成员方法 function say() //这个人可以说话的方法 { echo "这个人在说话"; }f unction run() //这个人可以走路的方法 { echo "这个人在走路"; } } ?>
In order to strengthen your understanding of classes, let’s make another class, a shape class. The range of shapes is a bit wider, let’s make a
rectangle. Let’s analyze it first and think about it from two aspects. Analysis, what are the properties of a rectangle? What are the functions of a rectangle?
class 矩形 { //矩形的属性 矩形的长; 矩形的宽; //矩形的方法 矩形的周长; 矩形的面积; } <?php class Rect { var $kuan; var $gao; function zhouChang() { 计算矩形的周长; }f unction mianJi() { 计算矩形的面积; } } ?>