/**
* namespace命名空间,用于将此ts文件中定义的类或者方法及接口等传到其它ts文件中
* 会结合export 一起使用,
* 在另一个ts中通过///<reference path="index.ts"/>引入其它ts文件
* 命名空间可进行多层嵌套,此时需要通过.属性或类名或方法名进行访问
* 例如:
* namespace first{
* export namespace second{
* class aa{
* num:number
* constructor(num:number){
* this.num=num
* }
* }
* }
* }
*
*/
namespace firstTsfile{
/**
* interface定义接口(通俗的将就是相当于自定义类型格式)
*/
export interface Ary{
age:number
}
export class ZXTOBJ{
name:string
hh:Ary
constructor(name:string,hh:Ary){
this.name=name
this.hh=hh
}
}
const OJH=new ZXTOBJ('23333',{age:2525})
console.log(OJH)
/**
* 申明类(定义构造函数)
* void表示方法没有返回值
*/
class Car{
color:string
constructor(color:string){
this.color=color
}
disp():void{//void表示此方法没有返回值
console.log('颜色为:'+this.color)
}
}
const obj=new Car('yellow')
console.log(obj)
/**
* 类的继承
* 这里继承了Car的属性和方法
*/
class bigCar extends Car{
carage:number
readA():void{
console.log('这车开了:'+this.carage+'年')
}
}
/**
* 注意:子类只能继承一个父类,但是支持多重继承
*/
class TwoCar extends bigCar{}
const car=new TwoCar('23')
car.carage=24
console.log(car)
/**
* 继承类的方法重写
*/
class ThreeCar extends Car{
disp():void{
super.disp()
console.log('这是继承类的方法输出')
}
}
const kk=new ThreeCar('blue')
kk.disp()
/**
* static关键字(用来定义类的静态属性和方法)
* 通过此关键字定义的属性和方法可以直接通过类名.属性或方法名进行调用,不需要创建实力对象
*/
class Pig{
static enen:string
static say():void{
console.log('hhhhh')
}
}
Pig.enen='sfsfs'
Pig.say()
/**
* 访问控制修饰符
* public(默认):公共,可在此类或任何实例对象或继承类或继承此类的实例对象中访问。
* private(私有)只能被其所定义的类访问。(不包括通过此类构建的实例对象)
* protected(受保护)只能被其自身及其子类和父类访问(不包括通过其自身和子类及父类构建的实例对象)
*/
class LKjk extends bigCar{
dfdfd():void{
this.carage=56575
}
protected hh:number=888888
private num:string='assfs'
public ksksks:number
lll(){
return this.num
}
}
const mm=new LKjk('sdsd')
mm.lll()
class Mobj extends LKjk{
public oo(){
return this.hh
}
}
/**
* 类可以实现接口,
* 通过implements关键字可以把接口中的属性当作类的属性使用
*/
export interface Inlog{
name:string
}
export class Longin implements Inlog{
name:string
num:number
age:number
gg:any
gh:Inlog
constructor(name:string,num:number,age:number,gg:any,gh:Inlog){
this.name=name
this.num=num
this.age=age
this.gg=gg
this.gh=gh
}
}
const KHU=new Longin('zs',1,23,4.2565,{name:'dgeeg'})
console.log(KHU)
}
/**
* TypeScript 模块
* 就是使用export关键字将当前文件中的接口,类,等导出。
* 在其它文件中科使用 import关键字引入,例如:
* a.ts文件中
* export class abc{
* name:string
* constructor(name:string){
* this.name=name
* }
* }
*
* b.ts文件中
* ///<reference path='a.ts'/>
* import hh=require('./a')
*
* const obj = new hh.abc('jack')
* console.log(obj)
*/