84669 person learning
152542 person learning
20005 person learning
5487 person learning
7821 person learning
359900 person learning
3350 person learning
180660 person learning
48569 person learning
18603 person learning
40936 person learning
1549 person learning
1183 person learning
32909 person learning
类的定义感觉看了不少很明白,哪位大神能详细解读一下下面每行代码的意思?
class Car { var $name = '汽车'; function getName() { return $this->name; } }
Don’t understand the definition of class? -PHP Chinese website Q&A-Don’t understand the definition of class? -PHP Chinese website Q&A
Please watch and learn.
class Car中class是关键字表示创建的是类,Car是类名。var $name='汽车',这是类的一个属性,并且把name属性赋值为汽车,var和public等差不多。 function getName(){}这是定义在类Car里的一个方法,可以通过$car =new Car() $car->getName()来调用。 return $this->name表示返回一个值即$this->name, $this表示类实例化的具体对象,如果想在类中用这个类的属性($name),就要用this,即this->name表示这类的属性的值。
Don’t understand the definition of class? -PHP Chinese website Q&A-Don’t understand the definition of class? -PHP Chinese website Q&A
Please watch and learn.
class Car中class是关键字表示创建的是类,Car是类名。var $name='汽车',这是类的一个属性,并且把name属性赋值为汽车,var和public等差不多。 function getName(){}这是定义在类Car里的一个方法,可以通过$car =new Car() $car->getName()来调用。 return $this->name表示返回一个值即$this->name, $this表示类实例化的具体对象,如果想在类中用这个类的属性($name),就要用this,即this->name表示这类的属性的值。