ts类06
类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | // 类:描述了创建的对象共用的属性和方法 // 实例化对象 class Person { name: string age: number constructor(name, age) { this .name = name this .age = age } sayHi(str: string) { console.log( "ts" + str) } } let cp = new Person( "周三" , 123) //new的时候,会执行类中的constructor(构造函数) cp.sayHi( "1111" ) // 继承类 : 扩展现有的类,类与类的关系,子类继承父类 class Animal { //父类 name: string age: number constructor(name: string, age: number) { this .name = name this .age = age } sayHi(str: string) { console.log( "ts" + str) } } class Dog extends Animal { //通过extends来继承父类的方法 constructor(name: string, age: number) { //使用super方法可以调用父类的构造函数 super (name, age) } //可以调用父类方法,还可以重写父类方法 sayHi() { console.log( "dog类" ) super .sayHi( "父类的sayHi方法" ) } } const A = new Animal( "ccc" , 3) A.sayHi( '+Animal类' ) const B = new Dog( "dog" , 3) B.sayHi() |
类的存取器.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | // 存取器:可以控制对对象成员的访问 class Name { lastName: string lastAge: string constructor(lastName: string, lastAge: string) { //使用super方法可以调用父类的构造函数 this .lastName = lastName this .lastAge = lastAge } // 设置一个读取器,用来读取数据 get fullName() { return this .lastName + ',今年' + this .lastAge + '岁' } // 设置一个设置器,用来设置数据 set fullName(value) { console.log(value) let ppp = value.split( ',今年' ) this .lastName = ppp[0] this .lastAge = ppp[1] } } const C = new Name( '王五' , '4' ) console.log(C.fullName) |
静态成员
静态属性
1 2 3 4 5 6 7 8 9 10 | // 静态成员 // 静态属性 // 类自己的属性和方法 class Silence { static sayHi() { console.log( 'hi' ); } } const s1 = new Silence( '赵六' ); console.log(Silence.sayHi()); |
类的修饰符
// 类的修饰符
// public 修饰的属性或方法是公有的,可以在任何地方被访问到,默认所有的属性和方法都是 public 的
class Bstatic {
private name1: string //访问修饰符,私有属性,不能在声明它的类的外部访问,与static静态方法不同,static不属于任何实例
protected age:string //访问修饰符,修饰的属性或方法是受保护的,区别在于可以在子类中访问
public constructor(name: string,age:string) { //写不写public都差不多,都是表示公有的方法属性
this.name1 = name
this.age = age
}
public p() { //写不写public都差不多,都是表示公有的方法属性
console.log(this.name1)
console.log(this.age)
}
}
class Cstatic extends Bstatic { //继承Bstatic
public What:string
constructor(name: string,age:string,What:string) {
super(name,age) //调用父类属性,可以继承到name和age,区别在与name只能继承不能访问,age可以在子类访问
this.What = What
}
play(){
return this.age+this.What
}
}
const Bs = new Bstatic("名字",'99')
console.log(Bs)
console.log(Bs.p)
const Cc = new Cstatic("名字","333","没啥")
console.log(Cc)
readonly
1 2 3 4 5 6 7 8 9 10 11 12 | // readonly 只读属性关键字 只允许出现在属性声明或索引签名或构造函数中 class Rea { // readonly age:number constructor(readonly age: number, public name: string, private whay: string, protected mie:string) { //如果readonly以及上面三个修饰符写在参数上,则就是创建并初始化的age参数,上面的定义和下面的引用也不用写了 // this.age=age } up() { // this.age = 15 //readonly为只读属性,无法进行修改 } } const R = new Rea(99, "张三" , "李四" , "王五" ) console.log(R) |
抽象类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | // 抽象类 abstract class Y { //定义一个抽象类 , 抽象类不能被实例化,为子类服务 abstract name: string //抽象方法没有函数,因为他不能有具体实现 abstract sayHi() } class X extends Y { //抽象类不能实例化,但是可以被子类继承后实例化 name: string //当抽象类定义了抽象方法或者属性后,在子类也需要去定义 constructor(name) { super () //派生类的构造函数必须包含 "super" this .name = name } sayHi() { //根据上述抽象方法定义子类方法 console.log( "抽象方法" ) } } const x = new X( "朱重八" ) console.log(x.name) console.log(x.sayHi) |
类与接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | // 一般来讲,一个类只能继承自另一个类,有时候不同类之间可以有一些共有的特性,这时候就可以把特性提取成接口 interface Sing { sing() } interface Dance { Dance() } class P implements Sing, Dance { sing() { console.log( "唱歌" ) } Dance() { console.log( "跳舞" ) } } class An implements Sing, Dance { sing() { console.log( "唱歌" ) } Dance() { console.log( "跳舞" ) } } const P1 = new P() const AN = new An() P1.sing() P1.Dance() AN.sing() AN.Dance() interface IRun { Run() } interface ISwin { Swin() } // 接口可以继承其他的多个接口 interface IA extends IRun, ISwin { } class I implements IA { Run() { } Swin() { } } |
接口继承接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | // 一般来讲,一个类只能继承自另一个类,有时候不同类之间可以有一些共有的特性,这时候就可以把特性提取成接口 interface Sing { sing() } interface Dance { Dance() } class P implements Sing, Dance { sing() { console.log( "唱歌" ) } Dance() { console.log( "跳舞" ) } } class An implements Sing, Dance { sing() { console.log( "唱歌" ) } Dance() { console.log( "跳舞" ) } } const P1 = new P() const AN = new An() P1.sing() P1.Dance() AN.sing() AN.Dance() interface IRun { Run() } interface ISwin { Swin() } // 接口可以继承其他的多个接口 interface IA extends IRun, ISwin { } class I implements IA { Run() { } Swin() { } } // 结合案例 class Emen{ constructor(public name:string){} sayHi(){ console.log( "Hi" ) } } class people extends Emen{ // 接口继承类中的实例属性和方法 age:number Method(){ console.log( "没干嘛" ) } } let person:people = { name: "张三" , age:26, sayHi(){ }, Method(){ }, } |
__EOF__

本文作者:userName
本文链接:https://www.cnblogs.com/wencaiguagua/p/18023418.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是博主的最大动力!
本文链接:https://www.cnblogs.com/wencaiguagua/p/18023418.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是博主的最大动力!
代码改变了我们,也改变了世界
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)