读书笔记一

来源:互联网 发布:能飞背单词软件下载 编辑:程序博客网 时间:2024/06/05 04:00

一面向对象
(一)类
1 属性:为对象存储数据
公共属性public(不带关键字 默认public)
私有属性private
受保护属性protected

访问类shopProduct中的title属性
model=newshopProduct();title = $model->title;

2.方法:为对象执行动作
① 方法类型

公共方法public(不带关键字 默认public):任何地方都可以访问受保护方法protected :只能在当前类或者子类访问私有方法private      :只能在当前类访问                   析构方法 function  __construct ($title$model = new shopProducter("hello");

任何给点的参数都会被传递给析构方法,当使用new时候 构造方法自动调用
②如果方法的参数前面有类名 那么他的参数只能传入该类的实例。
如果方法的参数前面有array 那么他的参数只能传入数组。
例如
`class test1{
public a=5;publicfunctionwrite(test2a,array b)  
    {  
        echo
a->b;
}调用这个类的write方法 只能这样

b=newtest1();w = $b->wrtie(new test2(),[1]);

否则会报错
Catchable fatal error: Argument 1 passed to test1::wrtie() must be an instance of test2, instance of test3 given, called in

3继承
①子类可以继承父类所以的public和protected方法(private不能继承)
②子类可以覆盖父类的方法,当两者方法名一样的时候(可以这样理解,调用方法默认从子类中,找不到再去父类中找)
那么问题来了,怎么在子类中调用被覆盖的父类方法?
答:直接在子类中 parent: :functionName(functionName为被覆盖的方法名)

1 0