ES6 类
ES6 类
面向对象是一种遵循真实世界建模的软件开发方式。面向对象.是将一个程序看作是通过称为方法的机制来相互通信的对象的集合。ES6也支持这些面向对象的特性。
面向对象的编程概念
首先,让我们先理解
对象—对象是任意实体的实时表示。据格雷迪胸针说,每个物体都有3个特征
--状态:对象的属性描述。
--行为:描述对象将如何行动。
--标识符:一个唯一的值,它将对象与一组类似的对象区分开来。
类—基于OOP的类是创建对象的蓝图,类封装对象的数据。
方法-方法促进对象之间的通信。
让我们把这些面向对象的概念翻译成现实世界中的概念。例如:一辆汽车是一个有数据(制造商、模型、车门、车辆号码等)和功能(加速、换档、打开车门、打开前照灯等)的对象。
在ES6之前,创建一个类是一件繁琐的事情。现在可以使用ES6中的类关键字创建类。
类可以通过在代码中声明它们或使用类表达式来创建。
语法: 声明一个类
class Class_name { }
语法:类表达式
var var_name = new Class_name { }
class关键字后面跟着类名。在命名类时必须考虑标识符的规则(已讨论)。
一个类定义可以包括以下内容
构造函数—负责为类的对象分配内存。
函数—函数表示对象可以采取的操作。他们有时也被称为方法。
这些内容组合在一起被称为类的成员。
注意—类主体只能包含方法,但不能包含数据属性。
例子:声明类
class Polygon { constructor(height, width) { this.height = height; this.width = width; } }
例子:类表达式
var Polygon = class { constructor(height, width) { this.height = height; this.width = width; } }
上面的代码片段表示一个未命名的类表达式。一个命名的类表达式可以写成。
var Polygon = class Polygon { constructor(height, width) { this.height = height; this.width = width; } }
创建对象
要创建类的实例,请使用new关键字后跟类名。以下是语法。
var object_name= new class_name([ arguments ])
上面代码中
new关键字负责实例化。
表达式的右边调用构造函数。如果构造函数是参数化的,则应该传递值。
例子:初始化类
var obj = new Polygon(10,12)
访问函数
一个类的属性和函数可以通过对象访问。使用"."点表示法来访问类的成员。
//accessing a function obj.function_name()
例子: 把他们放在一起
'use strict' class Polygon { constructor(height, width) { this.h = height; this.w = width; } test() { console.log("The height of the polygon: ", this.h) console.log("The width of the polygon: ",this. w) } } //creating an instance var polyObj = new Polygon(10,20); polyObj.test();
上面给出的示例声明了一个类Polygon。类的构造函数分别采用两个参数:height和width。this关键字是指类的当前实例。换句话说,上面的构造函数用传递给构造函数的参数值初始化了两个变量h和w。在类中的test ()函数打印height和width的值。
为了这个定义的类,创建了类Polygon的实例对象,该对象由polyobj变量引用,然后通过此对象调用函数。
上述程序产生如下输出:
The height of the polygon: 10 The width of the polygon: 20
Static关键字
static关键字可以应用于类中的函数,静态成员由类名引用。
例子
'use strict' class StaticMem { static disp() { console.log("Static Function called") } } StaticMem.disp() //invoke the static metho
注意:不强制声明构造函数定义。默认情况下,每个类都有一个无参数的构造函数。
上述代码产生如下输出
Static Function called
instanceof操作符
如果对象属于指定类型,则instanceof运算符返回true。
例子
'use strict' class Person{ } var obj = new Person() var isPerson = obj instanceof Person; console.log(" obj is an instance of Person " + isPerson);
上述代码产生如下输出
obj is an instance of Person True
类的继承
ES6支持继承的概念。继承是一个程序从一个现有实体创建新实体的能力-这里是指一个类。用于创建较新的类的类称为父类。新创建的类称为子类。
一个类可以使用extends关键字从另一个类继承。子类继承除父类的构造函数外的所有属性和方法。
以下是语法。
class child_class_name extends parent_class_name
例子: 类继承
'use strict' class Shape { constructor(a) { this.Area = a } } class Circle extends Shape { disp() { console.log("Area of the circle: "+this.Area) } } var obj = new Circle(223); obj.disp()
上面的示例声明了一个类Circle。该类由Shape类扩展。因为,类之间有一个继承关系,子类即Circle获取对其父类属性即Shape的隐式访问。
在成功执行上述代码时显示以下输出。
Area of Circle: 223
继承可分为以下几类
单个—每个类最多可以从一个父类扩展。
多个—类可以从多个类继承。ES6不支持多重继承。
多级-考虑以下示例。
'use strict' class Root { test() { console.log("call from parent class") } } class Child extends Root {} class Leaf extends Child //indirectly inherits from Root by virtue of inheritance {} var obj = new Leaf(); obj.test()
类Leaf通过多级继承来派生自Root和Child类的属性。
上述代码产生如下输出:
call from parent class
类的继承和方法重写
方法重写是子类重新定义超类方法的机制。下面是实例:
'use strict' class PrinterClass { doPrint() { console.log("doPrint() from Parent called… } class StringPrinter extends PrinterClass { doPrint() { console.log("doPrint() is printing a string…") } } var obj = new StringPrinter() obj.doPrint() }
在上面的示例中,子类更改了超类函数的实现。
在成功执行上述代码时显示以下输出。
doPrint() is printing a string…
Super关键字
ES6使子类能够调用其父类数据成员。这是通过使用Super关键字实现的。Super关键字用于引用类的直接父类。
考虑以下示例:
'use strict' class PrinterClass { doPrint() { console.log("doPrint() from Parent called…") } } class StringPrinter extends PrinterClass { doPrint() { super.doPrint() console.log("doPrint() is printing a string…") } } var obj = new StringPrinter() obj.doPrint()
doPrint()在类StringWriter重新定义,同时发出对其父类版本的调用。换句话说,super关键字用于在父类中调用doPrint()函数定义.
上述代码的输出结果:
doPrint() from Parent called. doPrint() is printing a string.