💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
## 4\. 函数 ### 4.1 函数的定义 ~~~ function hello(name:string):void { console.log('hello',name); } hello('zfpx'); ~~~ ### 4.2 函数表达式 * 定义函数类型 ~~~ type GetUsernameFunction = (x:string,y:string)=>string; let getUsername:GetUsernameFunction = function(firstName,lastName){ return firstName + lastName; } ~~~ ### 4.3 没有返回值 ~~~ let hello2 = function (name:string):void { console.log('hello2',name); } hello('zfpx'); hello2('zfpx'); ~~~ ### 4.4 可选参数 在TS中函数的形参和实参必须一样,不一样就要配置可选参数,而且必须是最后一个参数 ~~~ function print(name:string,age?:number):void { console.log(name,age); } print('zfpx'); ~~~ ### 4.5 默认参数 ~~~ function ajax(url:string,method:string='GET') { console.log(url,method); } ajax('/users'); ~~~ ### 4.6 剩余参数 ~~~ function sum(...numbers:number[]) { return numbers.reduce((val,item)=>val+=item,0); } console.log(sum(1,2,3)); ~~~ ### 4.7 函数重载 * 在Java中的重载,指的是两个或者两个以上的同名函数,参数不一样 * 在TypeScript中,表现为给同一个函数提供多个函数类型定义 ~~~ let obj: any={}; function attr(val: string): void; function attr(val: number): void; function attr(val:any):void { if (typeof val === 'number') { obj.age=val; } else { obj.name=val; } } attr('zfpx'); attr(9); attr(true); console.log(obj); ~~~ ~~~ //如何定义类 //Property 'name' has no initializer and is not definitely assigned //in the constructor.ts(2564) namespace a { class Person { name: string = 'zhufeng'; age: number; constructor() { this.age = 10; } } let p1 = new Person(); console.log(p1.name); console.log(p1.age); } namespace b { // 存取器 getter setter class Person { myname: string; constructor(name: string) { this.myname = name; } get name() { return this.myname; } set name(newVal: string) { this.myname = newVal.toUpperCase(); } } let p = new Person('zhufeng'); console.log(p.name);//zhufeng p.name = 'jiagou'; console.log(p.name); } namespace c { class Person { constructor(public name: string) { } } let p = new Person('zhufeng'); p.name = 'jiagou'; } //继承 /** * 子类继承父类后子类的实例上就拥有了父类中的属性和方法 * 访问修饰符 public protected private * public 自己 自己的子类 和其它类都可能访问 * protected 受保护的 自己和自己的子类能访问 ,其它 类不能访问 * private 私有的 只能自己访问,自己的子类不能访问,其它更不行了 */ namespace d { class Person { public name: string; protected age: number; private amount: number; constructor(name: string, age: number) { this.name = name; this.age = age; this.amount = 100; } getName() { return this.name; } setName(newName: string) { this.name = newName; } } class Student extends Person { static type = 'Student' stuNo: number; constructor(name: string, age: number, stuNo: number) { super(name, age); this.stuNo = stuNo; } static getType() { return Student.type; } getStuNo() { return this.name + this.age + this.amount + this.stuNo; } setStuNo(newStuNo: number) { this.stuNo = newStuNo; } } let s = new Student('zhufeng', 10, 1); console.log(s.name); console.log(s.age); console.log(s.amount); console.log(Student.type); Student.getType(); } ~~~ ### 5.5 继承 * 子类继承父类后子类的实例就拥有了父类中的属性和方法,可以增强代码的可复用性 * 将子类公用的方法抽象出来放在父类中,自己的特殊逻辑放在子类中重写父类的逻辑 * super可以调用父类上的方法和属性 ~~~ class Person { name: string;//定义实例的属性,默认省略public修饰符 age: number; constructor(name:string,age:number) {//构造函数 this.name=name; this.age=age; } getName():string { return this.name; } setName(name:string): void{ this.name=name; } } class Student extends Person{ no: number; constructor(name:string,age:number,no:number) { super(name,age); this.no=no; } getNo():number { return this.no; } } let s1=new Student('zfpx',10,1); console.log(s1); ~~~