JS / PHP object-oriented knowledge

js Object Oriented Object Oriented --PHP

  

Object-oriented:

Object-oriented class is essentially a package obtained by the function (or functions of the class

  1. Each class (function) has a natural property of a prototype, this prototype is an object, this object has a constructor (the constructor) attribute, the attribute value is the class itself.
  2. All of our new class, when, in fact, is calling its constructor. Constructor is private property, constructor of this is an instance of an object.
  3. Each object is born with a __proto__, pointing prototype class.
    1.    
      function person(){
       
      }
      console.log(person.prototype==Function)
      was zhangsan = new Person ();
      console.log(zhangsan.__proto__==person.prototype)
       

      zhangsan this object first look at the name is private, is used directly, instead of on the adoption of __proto__ to his class prototype look on, there is the direct use, not to keep looking up, until you find the base class Object , not is undefined there would be used directly. This discovery mechanism called the prototype chain.

  Mentioned object-oriented, we first reaction is encapsulation, inheritance and polymorphism. He explained as follows:

1. Packaging

The data and method of operation of the bundled data, external hidden, only provides an interface can operate.

Inheritance js

2.  prototypal inheritance  assign an object instance of the parent class to subclass prototype; private public are inherited by the public.

   

function Person(name){
        this.name=name;
    }
    Person.prototype.fn=function(){
        console.log(1)
    }
    var p1=new Person("zhang")
    console.dir(p1)

    function Son(){
        this.age=12;
    }
    Son.prototype = p1;
    where s1 = new Son;
    console.dir(s1.name)

 

 

2. call inherited method is a method call function / class comes naturally. The parent class is a subclass of private inheritance private.

     

    function Person(name){
        this.name=name;
    }
    Person.prototype.fn=function(){
        console.log(1)
    }
    var p1=new Person("zhang")
    console.log(p1)
    for(var k in p1){
        console.log(k)//name fn
    }
    function Son(){
        this.age=12;
        Person.call(this,"lisi")
    }
    where s1 = new Son;
    console.dir(s1)
    s1={
        name:"lisi",
        age:12
    }

 

3. pretending object inherits    the parent class public and private are inherited by subclasses private.  Use for in

for in  can traverse the public properties of the object  fn is publicly owned

Posing as object inheritance cases

function Person(name){this.name=name;}

Person.prototype.fn=function(){console.log(1)}

var p1=new Person("zhang")

function Son(){

this.age=56;

// this is an instance of an object subclass

for(var key in p1){

this[key]=p1[key]

}

}

var s1=new Son;

console.log(s1)

4.混合继承 私有的继承为私有的 私有的和公有的再次继承为公有的。

Call继承和原型继承的结

 

var p1=new Person("zhang")
    console.log(p1)
    for(var k in p1){
        console.log(k)//name fn
    }

 

5.组合继承 私有继承为私有 公有继承为公有

私有继承私有 借助call  公有继承为公有 不能是原型赋值给原型。因为原型是对象,是复合数据类型,是地址赋值给了前者,导致二者指向同一个原型。得克隆。

Js的克隆如何实现     var obj2= Object.create(obj1)

 

var obj1={name:"zhangsan"}
    var obj2=Object.create(obj1,{"age":{value:123}})
    console.log(obj2)


    function Person (){
        this.mame = "zhang"
    }
    Person.prototype.fn=function(){
        console.log(111);
    }
    var p=new Person;   
    function son(){
        Person.call(this)
    }
    son.prototype = Object.create(Person.prototype);

    var s = new son;
    s.__proto__.fn = function(){
        console.log(222);
    }
    s.fn();
    p.fn();

 

 

6.中间类继承

 

function fn(a,b,c){
arguments.__proto__=Array.prototype;
arguments.push(89)
console.log(arguments)
}
fn(45,23,23)

 

类:私有的,公有的。

3. 多态

Php是弱类型语言,没有多态这个概念,有重载。重载:子类和父类的方法名相同,子类会覆盖掉父类的方法,这就叫重载。

多态是强类型语言才有的,

 

 

PHP面向对象

 

面向对象是一种程序设计模式,简称oop

1,  通过关键字class定义类,类名首字母要大写。

2,对象  是类的实例,通过new关键字,得到一个实例对象,  new classname;

3,通过访问修饰符给类添加成员,

Pubic  添加的成员是公有的,在类内  类外  子类都可以去访问他。

Protected  受保护的,只能在类的里面和子类都可以去访问他。

Private  私有的,只能在类里面访问

如果成员前面没有加修饰符,默认是putblic

4,实例对象使用->访问类里面的成员。

 

对象->属性的时候,属性前面不加$符号。

 

构造函数, constructor

New 实例对象的本质,其实就是调用对应内的构造函数

$this 是类的实例对象,

 

析够函数

我们new实例的时候其实是调用构造函数,函数调用的时候会开辟内存空间,调用完之后会自动销毁。销毁前会自动调用,__destuct这个析构方法

 

 

继承extends

子类继承父类。 使用extends  关键字 子类继承父类中的属性和方法。

子类继承父类的属性和方法是有限继承,

Public public 修饰的属性和方法  子类都能继承

Protected protected 修饰的属性和方法,子类都可以继承。

Private Private 修饰的属性子类可以继承,private 修饰的方法子类不能继承。

静态属性/

使用static关键字定义的属性和方法,直接属于类,不会进入到类上

 

类如何防止静态属性,类名::静态属性   1.::范围解析操作符   2.静态属性前加$

 

 

静态方法的使用和静态属性一样

 

静态方法中不能使用$this关键字,$this是实例对象,而对象不能访问静态成员。

 

$self 指向类本身。

类里面想要操作静态成员,使用关键字 self

 

常量

类中使用关键字,const 定义常量成员,类中访问常量使用self

 

对象不能操作常量

总结

类中的成员

Public protected private  static  const;

对象克隆

1,对象和变量之间的赋值,不是克隆,而是将对象的内存地址赋值个变量,二者还是同一个对象。

2,Js通过Object.create()克隆对象

 

PHP中克隆,通过关键字clone   克隆一个对象。

 

 

通过关键字clone对象的时候,其实是调用类中的隐藏函数function  __clone()

 

如果不让类生成的实例对象被被人克隆 在隐藏函数  function __clone  前面修改访问修饰符为protected就可以了

 

 

 

Interface接口

使用关键字  interface  定义接口,接口也是一个类

1,接口中的方法必须是公有的

2,接口中的方法是空的,不能定义,由去实现接口的类去定义。

3,使用implements关键字实现接口,并且要实现接口中的所有方法。

 

 

抽象类

通过关键字  abstract 定义的类都是抽象类

只要类中的方法被定义为抽象方法那么这个类一定是抽象类,抽象方法必须是空,不能定义他具体的功能实现,

注意

1,注意抽象类不能被实例化

2,子类去继承这些抽象类的时候,一定要去实现抽象类的抽象方法,且和抽象方法的访问权限必须一致。

3,子类使用关键字  extends去继承抽象类

 

抽象类方法中的参数

1,可选参数,带有默认值的参数为可选参数

2,子类继承抽象类,如果抽象类中的抽象方法有参数,子类方法必须也有这个参数可以不同名。

3,子类方法中可以有父类抽象方法中没有的可选参数。

 

Final

final关键字(放在父类中的方法前面,子类无法覆盖父类的这个方法)

 

Guess you like

Origin www.cnblogs.com/inundate/p/peiqi_1589058729.html