[TOC]
>[success] # TS -- 類
~~~
1.'TS' 中的類和'ES6' 的類整體一樣的,有部分細節差異,比如'TS'中的類會
提供修飾符一類的,下面的案例會說明
2.面向對象編程的三大特點
2.1.'封裝(Encapsulation)':將對數據的操作細節隱藏起來,只暴露對外的接口。外界調用端不需要
(也不可能)知道細節,就能通過對外提供的接口來訪問該對象,
2.2.'繼承(Inheritance)':子類繼承父類,子類除了擁有父類的所有特性外,還有一些更具體的特性。
2.3.'多態(Polymorphism)':由繼承而產生了相關的不同的類,對同一個方法可以有不同的響應。
~~~
>[danger] ##### 創建一個簡單的ts類
~~~
1.和es6不同,下面的代碼分成三個部分,第一部分定義的參數,第二部分構造
函數,第三個部分就是 類的方法
~~~
~~~
class Point{
public x:number
public y:number
constructor(x:number,y:number){
this.x = x
this.y = y
}
public getPosition(){
return `(${this.x},${this.y})`
}
}
const point = new Point(2,3)
console.log(point.getPosition()) // (2,3)
~~~
>[danger] ##### 繼承
~~~
1.'TS' 的繼承和'ES6' 的繼承寫法和特性也相似都是使用'extends'
2.繼承的子類也有'super()'
~~~
~~~
class Parent {
public name: string
constructor(name: string) {
this.name = name
}
}
class Child extends Parent {
constructor(name: string) {
super(name)
}
}
~~~
>[info] ## public / private / protected-- 公共/私有/受保護的
~~~
~~~
>[danger] ##### public -- 公共
~~~
1.一些強類型語言是必須要寫這些修飾符的,在'TS'默認是'public',也就是說
'public' 修飾的是可以省略的
2.通過 "super" 關鍵字只能訪問基類的公共方法和受保護方法
,屬性是無法訪問的
~~~
~~~
class Parent {
public age: number
constructor(age: number) {
this.age = age
}
}
class Child extends Parent {
constructor(age: number) {
super(age)
// console.log(super.age) // 錯誤提示 :通過 "super" 關鍵字只能訪問基類的公共方法和受保護方法
}
}
const child = new Child(19)
~~~
>[danger] ##### private -- 私有的
~~~
1.當成員被標記成private時,它就不能在聲明它的類的外部訪問,簡單的說,只有
自己的class內部可以訪問,即使是自己的'實例','繼承的子類' 都無法訪問被'private'
修飾的內容
~~~
~~~
class Parent {
private name: string
constructor(name: string) {
this.name = name // 我能調用private修飾的
}
public getName(){
return `我的名字${this.name}` // 我能調用private修飾的
}
}
class Child extends Parent{
constructor(name:string){
super(name)
}
getTest(){
console.log(this.name) // 兒子也不讓用 錯誤提示:屬性“name”為私有屬性,只能在類“Parent”中訪問。
}
}
const son = new Parent('wang')
// son.name = "ss" // 提示錯誤:屬性“name”為私有屬性,只能在類“Parent”中訪問。
console.log(son.getName()) // 可以訪問,打印結果:我的名字wang
const child = new Child('wang')
// child.getTest() // 這里是錯誤的 因為Child 類也無法使用繼承父類的私有熟悉或者方法
~~~
* 私有屬性修飾在構造函數
~~~
1.是無法創建實例的,但是可以通過靜態方法變相創建實例
~~~
~~~
class Student extends Person {
private constructor (name: string, age: number) {
super(name, age)
console.log(this.gender)
}
static create (name: string, age: number) {
return new Student(name, age)
}
}
const tom = new Person('tom', 18)
console.log(tom.name)
~~~
>[danger] ##### protected -- 受保護的(可以用來禁止創建實例)
~~~
1.修飾的屬性或方法是受保護的,它和 private 類似,區別是它在子類中也是允許
被訪問的,簡單的 說'子類是可以訪問protected 修飾的' 實例是不可以的
2.修飾的是'constructor' 則當前類不能創建實例
3.使用protected 不能直接創建'對象使用屬性',和private不同他可以在子類中使用,相同點都可以
在自身定義類中使用
~~~
~~~
class Parent {
// private age: number
protected age: number
protected constructor(age: number) {
this.age = age
}
protected getAge() {
return this.age
}
}
// 解釋第三條
// const p = new Parent(18) //protected 修飾constructor不能創建實例 報錯提示:類“Parent”的構造函數是受保護的,僅可在類聲明中訪問。
class Child extends Parent {
constructor(age: number) {
console.log(super.getAge()) // 可以訪問父類中的protected方法
}
}
const child = new Child(19)
~~~
>[info] ## 其他特性
>[danger] ##### readonly -- 只讀屬性
~~~
1.你可以使用readonly關鍵字將屬性設置為只讀的。 只讀屬性必須在聲明時或構造函數里被初始化。
~~~
~~~
class Octopus {
public readonly name: string;
public readonly numberOfLegs: number = 8;
constructor (theName: string) {
this.name = theName;
}
}
let dad = new Octopus("Man with the 8 strong legs");
dad.name = "Man with the 3-piece suit"; // 錯誤! name 是只讀的.
~~~
>[danger] ##### 參數屬性
-- 在參數前加修飾符自動幫我們創建this屬性
~~~
1.在'constructor' 的參數中加修飾符,相當于省略了下面的代碼注釋部分
~~~
~~~
class A {
// public name:string
constructor(public name: string) {
// this.name = name
}
}
const a1 = new A('lison')
console.log(a1.name)
~~~
>[danger] ##### 靜態屬性 -- static修飾
~~~
1.和'es6'一樣'ts' 也有靜態'方法',但是'ts' 提供了 靜態屬性,屬性和方法都是'static' 修飾
2.左右也是一樣的只能類來使用靜態屬性
~~~
~~~
class Parent {
public static getAge() {
return Parent.age
}
private static age: number = 18
constructor() {}
}
const p = new Parent()
// console.log(p.age) // age 是靜態的屬性 所以實例是不能訪問的
// console.log(Parent.age) // 雖然有了類但是也是不能訪問的,因為用了private修飾用public可以訪問
~~~
>[danger] ##### 類選填屬性 -- '?'
~~~
1.被標記的選填屬性如果不填則'undefined'
~~~
~~~
class Info {
public name: string
public age?: number
constructor(name: string, age?: number, public sex?: string) {
this.name = name
this.age = age
}
}
const info1 = new Info('lison')
console.log(info1) // {sex: undefined, name: "lison", age: undefined}
const info3 = new Info('lison', 18)
console.log(info3) // {sex: undefined, name: "lison", age: 18}
const info4 = new Info('lison', 18, 'man')
console.log(info4) // {sex: "man", name: "lison", age: 18}
~~~
>[danger] ##### 也支持es6 --getter /setter
~~~
1.下面案例來自官網
~~~
~~~
let passcode = "secret passcode";
class Employee {
private _fullName: string;
get fullName(): string {
return this._fullName;
}
set fullName(newName: string) {
if (passcode && passcode == "secret passcode") {
this._fullName = newName;
}
else {
console.log("Error: Unauthorized update of employee!");
}
}
}
let employee = new Employee();
employee.fullName = "Bob Smith";
if (employee.fullName) {
alert(employee.fullName);
}
~~~
>[danger] ##### 抽象類abstract
~~~
1.抽象類是不允許被實例化的
2.繼承抽象類的類必須去實現實例中的抽象類中的'抽象方法'和'抽象屬性'
3.http://www.importnew.com/12399.html
~~~
~~~
abstract class People {
public sex:string
public abstract _name: string
public constructor(sex){
this.sex = sex
}
abstract get insideName(): string
abstract set insideName(value: string)
abstract speak(word:string):string
getSex(){
return this.sex
}
}
class P extends People {
public _name: string
public insideName: string
constructor(_name:string,sex:string){
super(sex)
this._name =_name
this.insideName = this.insideName
}
speak(word:string):string {
return word
}
}
~~~
>[danger] ##### 接口 和類
~~~
1.接口可以多繼承,抽象類不行
~~~
~~~
interface FoodInterface {
type: string
}
class FoodClass implements FoodInterface {
public type: string
}
class A {
protected name: string
}
interface I extends A {}
class B extends A implements I {
public name: string
}
~~~
>[danger] ##### 接口配合類
~~~
1.接口 的作用是配合不同類,但是他們具有相同約束,下面為例 人和動物是兩個類,
但是他們有相同的行為可以通過接口提取出來,下面的例子不太準確表示的,人屬于動物類
的動物是可以做人的父類,但是如果是兩個毫無相關且有同樣行為的類就可以嘗試用接口進行約束
~~~
~~~
interface Eat {
eat (food: string): void
}
interface Run {
run (distance: number): void
}
class Person implements Eat, Run {
eat (food: string): void {
console.log(`優雅的進餐: ${food}`)
}
run (distance: number) {
console.log(`直立行走: ${distance}`)
}
}
class Animal implements Eat, Run {
eat (food: string): void {
console.log(`呼嚕呼嚕的吃: ${food}`)
}
run (distance: number) {
console.log(`爬行: ${distance}`)
}
}
~~~
>[danger] ##### 定義一個類創建實例為參數
~~~
1.使用 new()=> 類 ,來規定傳入的是一個類作為參數
~~~
~~~
const create = <T>(c:new()=>T):T=>{
return new c()
}
class Infos{
public age:number
constructor(){
this.age = 1
}
}
create(Infos)
~~~
- Vue--基礎篇章
- Vue -- 介紹
- Vue -- MVVM
- Vue -- 創建Vue實例
- Vue -- 模板語法
- Vue -- 指令用法
- v-cloak -- 遮蓋
- v-bind -- 標簽屬性動態綁定
- v-on -- 綁定事件
- v-model -- 雙向數據綁定
- v-for -- 只是循環沒那么簡單
- 小知識點 -- 計劃內屬性
- key -- 屬性為什么要加
- 案例說明
- v-if/v-show -- 顯示隱藏
- v-for 和 v-if 同時使用
- v-pre -- 不渲染大大胡語法
- v-once -- 只渲染一次
- Vue -- class和style綁定
- Vue -- filter 過濾器
- Vue--watch/computed/fun
- watch -- 巧妙利用watch思想
- Vue -- 自定義指令
- Vue -- $方法
- Vue--生命周期
- Vue -- 專屬ajax
- Vue -- transition過渡動畫
- 前面章節的案例
- 案例 -- 跑馬燈效果
- 案例 -- 選項卡內容切換
- 案例-- 篩選商品
- 案例 -- 搜索/刪除/更改
- 案例 -- 用computed做多選
- 案例 -- checked 多選
- Vue--組件篇章
- component -- 介紹
- component -- 使用全局組件
- component -- 使用局部組件
- component -- 組件深入
- component -- 組件傳值父傳子
- component -- 組件傳值子傳父
- component -- 子傳父語法糖拆解
- component -- 父組件操作子組件
- component -- is 動態切換組件
- component -- 用v-if/v-show控制子組件
- component -- 組件切換的動畫效果
- component -- slot 插槽
- component -- 插槽2.6
- component -- 組件的生命周期
- component -- 基礎組件全局注冊
- VueRouter--獲取路由參數
- VueRouter -- 介紹路由
- VueRouter -- 安裝
- VueRouter -- 使用
- VueRouter--router-link簡單參數
- VueRouter--router-link樣式問題
- VueRouter--router-view動畫效果
- VueRouter -- 匹配優先級
- vueRouter -- 動態路由
- VueRouter -- 命名路由
- VueRouter -- 命名視圖
- VueRouter--$router 獲取函數
- VueRouter--$route獲取參數
- VueRouter--路由嵌套
- VueRouter -- 導航守衛
- VueRouter -- 寫在最后
- Vue--模塊化方式結構
- webpack--自定義配置
- webpack -- 自定義Vue操作
- VueCli -- 3.0可視化配置
- VueCli -- 3.0 項目目錄
- Vue -- 組件升級篇
- Vue -- 組件種類與組件組成
- Vue -- 組件prop、event、slot 技巧
- Vue -- 組件通信(一)
- Vue -- 組件通信(二)
- Vue -- 組件通信(三)
- Vue -- 組件通信(四)
- Vue -- 組件通信(五)
- Vue -- 組件通信(六)
- Vue -- bus非父子組件通信
- Vue -- 封裝js插件成vue組件
- vue組件分裝 -- 進階篇
- Vue -- 組件封裝splitpane(分割面板)
- UI -- 正式封裝
- Vue -- iview 可編輯表格案例
- Ui -- iview 可以同時編輯多行
- Vue -- 了解遞歸組件
- UI -- 正式使用遞歸菜單
- Vue -- iview Tree組件
- Vue -- 利用通信仿寫一個form驗證
- Vue -- 使用自己的Form
- Vue -- Checkbox 組件
- Vue -- CheckboxGroup.vue
- Vue -- Alert 組件
- Vue -- 手動掛載組件
- Vue -- Alert開始封裝
- Vue -- 動態表單組件
- Vue -- Vuex組件的狀態管理
- Vuex -- 參數使用理解
- Vuex -- state擴展
- Vuex -- getters擴展
- Vuex--mutations擴展
- Vuex -- Action 異步
- Vuex -- plugins插件
- Vuex -- v-model寫法
- Vuex -- 更多
- VueCli -- 技巧總結篇
- CLI -- 路由基礎
- CLI -- 路由升級篇
- CLI --異步axios
- axios -- 封裝axios
- CLI -- 登錄寫法
- CLI -- 權限
- CLI -- 簡單權限
- CLI -- 動態路由加載
- CLI -- 數據性能優化
- ES6 -- 類的概念
- ES6類 -- 基礎
- ES6 -- 繼承
- ES6 -- 工作實戰用類數據管理
- JS -- 適配器模式
- ES7 -- 裝飾器(Decorator)
- 裝飾器 -- 裝飾器修飾類
- 裝飾器--修飾類方法(知識擴展)
- 裝飾器 -- 裝飾器修飾類中的方法
- 裝飾器 -- 執行順序
- Reflect -- es6 自帶版本
- Reflect -- reflect-metadata 版本
- 實戰 -- 驗證篇章(基礎)
- 驗證篇章 -- 搭建和目錄
- 驗證篇章 -- 創建基本模板
- 驗證篇章 -- 使用
- 實戰 -- 更新模型(為了迎合ui升級)
- 實戰 -- 模型與接口對接
- TypeSprict -- 基礎篇章
- TS-- 搭建(一)webpack版本
- TS -- 搭建(二)直接使用
- TS -- 基礎類型
- TS -- 枚舉類型
- TS -- Symbol
- TS -- interface 接口
- TS -- 函數
- TS -- 泛型
- TS -- 類
- TS -- 類型推論和兼容
- TS -- 高級類型(一)
- TS -- 高級類型(二)
- TS -- 關于模塊解析
- TS -- 聲明合并
- TS -- 混入
- Vue -- TS項目模擬
- TS -- vue和以前代碼對比
- TS -- vue簡單案例上手
- Vue -- 簡單弄懂VueRouter過程
- VueRouter -- 實現簡單Router
- Vue-- 原理2.x源碼簡單理解
- 了解 -- 簡單的響應式工作原理
- 準備工作 -- 了解發布訂閱和觀察者模式
- 了解 -- 響應式工作原理(一)
- 了解 -- 響應式工作原理(二)
- 手寫 -- 簡單的vue數據響應(一)
- 手寫 -- 簡單的vue數據響應(二)
- 模板引擎可以做的
- 了解 -- 虛擬DOM
- 虛擬dom -- 使用Snabbdom
- 閱讀 -- Snabbdom
- 分析snabbdom源碼 -- h函數
- 分析snabbdom -- init 方法
- init 方法 -- patch方法分析(一)
- init 方法 -- patch方法分析(二)
- init方法 -- patch方法分析(三)
- 手寫 -- 簡單的虛擬dom渲染
- 函數表達解析 - h 和 create-element
- dom操作 -- patch.js
- Vue -- 完成一個minVue
- minVue -- 打包入口
- Vue -- new實例做了什么
- Vue -- $mount 模板編譯階段
- 模板編譯 -- 分析入口
- 模板編譯 -- 分析模板轉譯
- Vue -- mountComponent 掛載階段
- 掛載階段 -- vm._render()
- 掛載階段 -- vnode
- 備份章節
- Vue -- Nuxt.js
- Vue3 -- 學習
- Vue3.x --基本功能快速預覽
- Vue3.x -- createApp
- Vue3.x -- 生命周期
- Vue3.x -- 組件
- vue3.x -- 異步組件???
- vue3.x -- Teleport???
- vue3.x -- 動畫章節 ??
- vue3.x -- 自定義指令 ???
- 深入響應性原理 ???
- vue3.x -- Option API VS Composition API
- Vue3.x -- 使用set up
- Vue3.x -- 響應性API
- 其他 Api 使用
- 計算屬性和監聽屬性
- 生命周期
- 小的案例(一)
- 小的案例(二)-- 泛型
- Vue2.x => Vue3.x 導讀
- v-for 中的 Ref 數組 -- 非兼容
- 異步組件
- attribute 強制行為 -- 非兼容
- $attrs 包括 class & style -- 非兼容
- $children -- 移除
- 自定義指令 -- 非兼容
- 自定義元素交互 -- 非兼容
- Data選項 -- 非兼容
- emits Option -- 新增
- 事件 API -- 非兼容
- 過濾器 -- 移除
- 片段 -- 新增
- 函數式組件 -- 非兼容
- 全局 API -- 非兼容
- 全局 API Treeshaking -- 非兼容
- 內聯模板 Attribute -- 非兼容
- key attribute -- 非兼容
- 按鍵修飾符 -- 非兼容
- 移除 $listeners 和 v-on.native -- 非兼容
- 在 prop 的默認函數中訪問 this -- ??
- 組件使用 v-model -- 非兼容
- 渲染函數 API -- ??
- Slot 統一 ??
- 過渡的 class 名更改 ???
- Transition Group 根元素 -- ??
- v-if 與 v-for 的優先級對比 -- 非兼容
- v-bind 合并行為 非兼容
- 監聽數組 -- 非兼容