File tree Expand file tree Collapse file tree 2 files changed +25
-1
lines changed Expand file tree Collapse file tree 2 files changed +25
-1
lines changed Original file line number Diff line number Diff line change @@ -561,3 +561,26 @@ class Demo {
561
561
const demo = Demo .getInstance ()
562
562
```
563
563
  首先我们要做的第一件事情就是控制住类的构造函数不能在外部调用,所以我们把类的构造函数设置成私有属性,这个时候该如何实例化一个类呢:thinking:,我们在类中定义一个方法提供给外部使用,由于我们没办法实例化类该怎么调用实例化类上的方法,所以我们要用`static`,直接将方法挂载到类上而不是挂载到实例化对象上,这样我们就可以通过`demo.getInstance()`来实例化`demo`这个类了,但是换句话说了,这样还不是照样可以无限实例化类嘛:cold_sweat:,实例化出来的对象指针还都不是一样的,我们接着往下看,我们在类上在通过`static`的方式挂载一个属性,将它设置为私有属性,在`getInstance`方法中判断,如果是初始化第一次实例化这个类,我们就讲实例化对象绑定在这个`instance`属性上,最后返回出去,如果有的话,我们直接将`instance`返回出去,这样我们就实现了一个最简单的单例模式:100:。
564
+ ## 抽象类
565
+ 抽象类的概念就是将类里面公用的东西提炼出来,在组成一个抽象类,抽象类里不仅可以有抽象方法还可以有具体的属性和方法,他与interface不同的是,interface是将接口中公用的东西提炼出来,而抽象类针对的是类,抽象类不能直接被实例化,但是他可以被继承,我们来看一个例子:chestnut : :
566
+ ``` ts
567
+ abstract class Gemo {
568
+ abstract getArea(): number // 因为每个图形的具体实现面积的方法是不一样的,但是他们共同点是都应该有这个方法,所以我们把它修改为抽象方法,一旦你讲这个方法定义为抽象方法就以为你不能写方法的实现,你只能定义下这个方法
569
+ }
570
+ class Circle extends Gemo {
571
+ getArea () {
572
+ return 123
573
+ }
574
+ }
575
+ class Square extends Gemo {
576
+ getArea () {
577
+ return 456
578
+ }
579
+ }
580
+ class Triangle extends Gemo {
581
+ getArea () {
582
+ return 789
583
+ }
584
+ }
585
+ const demo = Demo .getInstance ()
586
+ ```
Original file line number Diff line number Diff line change 1
1
{
2
2
"scripts" : {
3
3
"docs:dev" : " vuepress dev docs" ,
4
- "docs:build" : " vuepress build docs"
4
+ "docs:build" : " vuepress build docs" ,
5
+ "deploy" : " bash deploy.sh"
5
6
},
6
7
"devDependencies" : {
7
8
"@vuepress/plugin-active-header-links" : " ^1.3.1" ,
You can’t perform that action at this time.
0 commit comments