Home > Backend Development > PHP Tutorial > Reaction Equation of Manganese Dioxide and Concentrated Hydrochloric Acid Section 8 - Access Method

Reaction Equation of Manganese Dioxide and Concentrated Hydrochloric Acid Section 8 - Access Method

WBOY
Release: 2016-07-29 08:34:57
Original
1344 people have browsed it

/*
+------------------------------------------------ ----------------------------------+
| = This article is read by Haohappy<> ;
| = Notes from the Chapter Classes and Objects
| = Translation + personal experience
| = To avoid unnecessary trouble that may occur, please do not reprint, thank you
| = Welcome to criticize and correct, hope and all PHP enthusiasts Make progress together!
| = PHP5 Research Center: http://blog.csdn.net/haohappy2004
+-------------------------- -------------------------------------------------- ---+
*/
Section 8--Access Method
The access method of PHP5 allows restricting access to class members. This is a new feature in PHP5, but it has already existed in many object-oriented languages. Yes Only by knowing the access methods can we develop a reliable object-oriented application and build a reusable object-oriented class library.
Like C++ and Java, PHP has three access methods: public, private and protected. For the access methods of a class member , can be one of them. If you do not specify the access method, the default access method is public. You can also specify an access method for static members and put the access method before the static keyword (such as public static).
Public Members can be accessed without restrictions. Any code outside the class can read and write public properties. You can call a public method from anywhere in the script. In previous versions of PHP, all methods and properties were public. This makes objects feel like well-structured arrays.
Private (private) members are only visible inside the class. You cannot change or read the value of a private property outside the class method in which it resides. Likewise, only Methods in the same class can call a private method. Inherited subclasses cannot access private members in the parent class.
Note that any member of the class and instances of the class can access private members. See Example 6.8, The equals method compares two widgets. The == operator compares two objects of the same class, but in this example each object instance has a unique ID. The equals method only compares name and price. Note how the equals method accesses another object. A private property of a Widget instance. Both Java and C allow such operations.
Listing 6.8 Private members

Copy code The code is as follows:

   class Widget  
   {  
       private $name;  
       private $price;  
       private $id;  
       public function __construct($name, $price)  
       {  
           $this->name = $name;  
           $this->price = floatval($price);  
           $this->id = uniqid();  
       }  
       //checks if two widgets are the same 检查两个widget是否相同  
       public function equals($widget)  
       {  
           return(($this->name == $widget->name)AND  
               ($this->price == $widget->price));  
       }  
   }  
   $w1 = new Widget('Cog', 5.00);  
   $w2 = new Widget('Cog', 5.00);  
   $w3 = new Widget('Gear', 7.00);  
   //TRUE  
   if($w1->equals($w2))  
   {  
       print("w1 and w2 are the same
n");  
   }  
   //FALSE  
   if($w1->equals($w3))  
   {  
       print("w1 and w3 are the same
n");  
   }  
   //FALSE, == includes id in comparison  
   if($w1 == $w2) //不等,因为ID不同  
   {  
       print("w1 and w2 are the same
n");  
   }  
?>  

如果你对面向对象编程不熟悉,你可能想知道用private成员的目的是什么. 你可以回忆一下封装和耦合的想法,这在本章开头我们有讨论过. Private成员有助于封装数据. 他们可以隐藏在一个类内部而不被类外部的代码接触到. 同时他们还有助于实现松散的耦合. 如果数据结构外的代码不能直接访问内部属性,那么就不会产生一个隐性的关联性.
当然,大部分private属性仍然可以被外部代码共享. 解决方法是用一对public方法,一个是get(获取属性的值),另一个是set(设置属性的值). 构造函数也接受属性的初始值. 这使得成员间的交流通过一个狭窄的,经过良好限定的接口来进行. 这也提供改变传递给方法的值的机会. 注意在例子6.8中,构造函数如何强制使price成为一个float数(floadval()).
Protected(受保护的) 成员能被同个类中的所有方法和继承出的类的中所有方法访问到. Public属性有违封装的精神,因为它们允许子类依赖于一个特定的属性来书写.protected方法则不会带来这方面的担忧.一个使用protected方法的子类需要很清楚它的父类的结构才行.
例子6.9由例子6.8改进而得到,包含了一个Widget的子类Thing. 注意Widget现在有一个叫作getName的protected方法. 如果Widget的实例试图调用protected方法将会出错: $w1->getName()产生了一个错误. 但子类Thing中的getName方法可以调用这个protected方法.当然对于证明Widget::getName方法是protected,这个例子显得过于简单. 在实际情况下,使用protected方法要依赖于对对象的内部结构的理解.
Listing 6.9 Protected members 

复制代码 代码如下:

   class Widget  
   {  
       private $name;  
       private $price;  
       private $id;  
       public function __construct($name, $price)  
       {  
           $this->name = $name;  
           $this->price = floatval($price);  
           $this->id = uniqid();  
       }  
       //checks if two widgets are the same  
       public function equals($widget)  
       {  
           return(($this->name == $widget->name)AND  
               ($this->price == $widget->price));  
       }  
       protected function getName()  
       {  
           return($this->name);  
       }  
   }  
   class Thing extends Widget  
   {  
       private $color;  
       public function setColor($color)  
       {  
           $this->color = $color;  
       }  
       public function getColor()  
       {  
           return($this->color);  
       }  
       public function getName()  
       {  
           return(parent::getName());  
       }  
   }  
   $w1 = new Widget('Cog', 5.00);  
   $w2 = new Thing('Cog', 5.00);  
   $w2->setColor('Yellow');  
   //TRUE (still!) 结果仍然为真  
   if($w1->equals($w2))  
   {  
       print("w1 and w2 are the same
n");  
   }  
   //print Cog 输出 Cog  
   print($w2->getName());  
?> 


一个子类可能改变通过覆写父类方法来改变方法的访问方式,尽管如此,仍然有一些限制. 如果你覆写了一个public类成员,他子类中必须保持public. 如果你覆写了一个protected成员,它可保持protected或变成public.Private成员仍然只在当前类中可见. 声明一个与父类的private成员同名的成员将简单地在当前类中建立一个与原来不同的成员. 因此,在技术上你不能覆写一个private成员. 
Final关键字是限制访问成员方法的另一个方法. 子类不能覆写父类中标识为final的方法. Final关键字不能用于属性.

以上就介绍了二氧化锰和浓盐酸反应方程式 第八节--访问方式,包括了二氧化锰和浓盐酸反应方程式方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template