大纲
因为 class 很关键,自己不再组建语言和学习大纲
引用 黑马程序员 原教学大纲,以免误人子弟
类名首字母大写 (约定成俗,非IDE硬性规定)
这篇文章写了大半才注意到 -_-!!!
个人理解
可选对象,传参可空
简易示例
class lei {
name : string = ''
food? : string
age? : number
}
const l1 = new lei()
console.log('长度1 ',l1.name.length)
console.log('长度2 ',l1.food?.length)
console.log('数字1 ',l1.age)
实际演示
class lei {
name? : string = ''
food? : string
age? : number
constructor(na1?:string, fo1?:string) {
console.log('类初始化函数',`${na1} 吃 ${fo1}`)
console.log('类字段',this.name,this.food)
// 由此可得出结论 new 传参是给初始化函数提供的 不是给字段
}
}
let l2 = new lei('猫','鱼')
let l3 = new lei('狗','屎')
经过实际演示,得出结论
new 传参 是给初始化函数提供的 不是给字段
只要是 new 对象,则初始化函数则会运行,不需要调用
1. 构建对象(定义对象)
interface dx{
name : string
food : string
age? : number
}
2. 建立对象变量
let dx2 : dx = {
food:'yang',
name:'cao',
}
对象变量内的 参数之间一定要用, (逗号)
隔开,否则IDE报错
3. 调用对象变量
const l4 = new lei(dx2) // 可以以变量传参
完整简易示例
interface dx{
name : string
food : string
age? : number
}
class lei {
name? : string = ''
food? : string
age? : number
constructor(dx1 : dx) {
console.log('类初始化函数',`${dx1.name} 吃 ${dx1.food}`)
console.log('类字段',this.name,this.food)
// 由此可得出结论 new 传参是给初始化函数提供的 不是给字段
}
}
let dx2 : dx = {
food:'yang',
name:'cao'
}
const l4 = new lei(dx2) // 可以以变量传参
let l5 = new lei({food:'羊',name:'草'}) // 也可以直接传参 但要附上属性名
个人觉得没什么注意事项,和其他建立class方法差不多
class lei {
name? : string = ''
food? : string
age? : number
constructor(dx1 : dx) {
this.name = dx1.name
this.food = dx1.food
this.age = dx1.age
}
sayhi(yname:string){
console.log(`${yname}:\n很高兴见到你,我叫 ${this.name}`)
}
sayfood(yfood:string){
if (yfood == this.food) {
return ('是的,我喜欢蛋糕')
}else {return(`嗯...我不是很喜欢${yfood}`)}
}
}
const npc1 : lei = new lei({name:'npc小明', food:'蛋糕'})
npc1.sayhi('小强')
console.log(npc1.sayfood('冰淇淋'))
浅析(个人愚见,如有错误大佬也可以指正一下)
这里new出来的对象可以简单的理解你创建了1个NPC
调用方法,就是和这个NPC交互(对话)
再new一个,就是又新创建了一个NPC
NPC2点 就是和 NPC2 进行交互
他们都是引用的同一个定义(比如人,汽车,树,这都是同一种类型定义)
new就是创建实体,所以为什么编程语言中叫实例
为什么花这么长的篇幅讲new,应为之前学JavaScript
这个new折磨我了好久,才理解
新手小技巧
还有就是教学视频中很多name,新手不建议用同样的名字变量
你会搞不清楚谁是谁的
如果非想一样的英文,最起码带个数字编号(太多数字编号也不建议),为了便于识别
也就是说上面学的是动态属性和方法
会随着new对象改变而改变
静态是加在类上,可以直接通过类调用
class lei {
static ver : number = 20241211 // 在静态方法调用静态属性会报错
static getrandom () {return (Math.random())}
}
console.log(lei.ver.toString(),lei.getrandom().toString())
子类继承父类的所有属性和方法
new 子类,可以调用父类属性和方法,还包含子类的属性和方法
注意一点的是
所谓的重构只是在重构的子类生效
简易示例
class People {
name : string = ''
gender? : boolean = true
close? : -1 | 0 | 1 = 0
hp? : number = 100
mp? : number = 100
constructor(name1 : string, gender1?: boolean, close1?: -1 | 0 | 1) {
this.name = name1
if (JSON.stringify(gender1) != 'undefined'){this.gender = gender1}
if (JSON.stringify(close1) != 'undefined'){this.close = close1}
if (this.close == -1){console.log(`${this.name} 你想干嘛`)}
if (this.close == 0){console.log(`${this.name} 有事吗?`)}
if (this.close == 1){console.log(`${this.name} 你好啊!`)}
}
sayme(){console.log(`我叫 ${this.name}`)}
}
// 子类可修改父类字段属性值
class Cm extends People{
hp?: number | undefined = 50 //
close?: 0 | 1 | -1 | undefined = 1
help (name3 : string){
console.log(`${name3} 我受伤了需要得到治疗 你能帮助我吗?`)
}
}
// 子类可以从写父类方法
class Boss extends People {
close?: 0 | 1 | -1 | undefined = -1
// 重构父类初始化函数
constructor(name2 : string, gender1?: boolean, close1?: -1 | 0 | 1) { // 子传参
//super.name = name2 // 重构 父属性 赋值 要与子传参名称一致
super(name2) // 重构 父传参 要与 子传参 名称保持一致
console.log(`吾乃 ${this.name} 大人`) // 父类 初始化函数修改 必须重构 父传参
}
sayme(){
//super.sayme() // 这是子类里调用了父方法
console.log('匹夫,前来受死')
} // 并不能随意修改 比如父类无传参 子类也无法修改成传参
}
/*let npc1 : Cm = new Cm('op')
npc1.sayme()
npc1.help('wxs')*/
let boss1 : Boss = new Boss('boss')
boss1.sayme()
console.log(boss1.name, boss1.close, boss1.gender)
// 由下面代码可得,并没有修改父类的属性及方法 所谓的重构只是在重构的子类生效
let npc2 : People = new People('npc2',true,1)
npc2.sayme()
console.log(npc2.name,npc2.gender,npc2.close)
个人总结
被判断对象 instanceof 判断对象
简易示例
let arr1 : string[] = []
if (arr1 instanceof Array){console.log(`arr1 是数组`)}
else {`arr1 不是数组`}
类的属性和方法可以通过 修饰符 来 限制访问
readonly 只可以取值 不可修改
private 私有 外部(子类)无法访问 只能在类里使用
protected 私有 子类可访问 外部不可访问
public 无限制 默认
简易示例
class Poeple{
private name : string
private close : -1 | 0 | 1
hp : number
constructor(name1: string , close1 : -1 | 0 | 1, hp1 : number) {
this.name = name1
this.close = close1
this.hp = hp1
console.log('生成',this.name,this.close,this.hp)
}
}