typescript函数
函数声明
function run():string {
return 'run'
}
匿名函数
let fun = function():number{
return 123
}
定义方法传参
function getInfo(name:string,age:number):string{
return `${name}--${age}`
}
console.log(getInfo('zhansan',18);
var getInfo1 = function(name:string,age:number):string{
return `${name}---${age}`
}
console.log(getInfo1('zhansan',18));
方法可选参数
// es5里面方法里面实参和形参可以不一样,但是ts中必须一样,如果不一样就需要配置可选参数
// 注意:可选参数必须配置到参数的后面
function getInfo2(name:string,age?:number):string{
if(age){
return `${name}--${age}`
}else{
return `${name}--`
}
}
console.log(getInfo2('zhangsan'));
console.log(getInfo2('zhangsan',19));
默认参数(可选参数)
// es5里面没法设置默认参数,ts和es6都可以设置默认参数
function getInfo3(name:string,age:number=20):string{
if(age){
return `${name}--${age}`
}else{
return `${name}--`
}
}
console.log(getInfo3('zhangsan'));
console.log(getInfo3('zhangsan',19));
剩余参数
function sum(a:number,...result:number[]):number{
let sum1 = a;
for(let i=0; i<result.length;i++){
sum1+=result[i]
}
return sum1
}
console.log(1,2,3,4,5)
函数重载
// java中方法的重载:重载指的是两个或者两个以上同名函数,但他们参数不一样,此时会出现重载情况
// typescript中的重载:通过为一个函数提供多个函数类型定义来实现多种功能的目的
// ts中的重载
function getInfo(name:string):string;
function getInfo(age:number):string;
function getInfo(str:any):any{
if(typeof str === 'string') {
return '我叫:'+ str
}else{
return '今年:' + str
}
}
console.log(getInfo('zs'));
console.log(getInfo(18));
typescript类
typescript中定义类
// (-)
class Person{
name:string; // 属性 前面省略了public关键字
constructor(n:string){
// 构造函数 实例初始化类的时候触发的方法
this.name = n;
}
run():void{
alert(this.name);
}
}
let p = new Person('张三');
p.run()
// (二)
class Person1{
name:string;
constructor(name:string){
this.name = name;
}
getName():string{
return this.name
}
setName(name:string):void{
this.name = name
}
}
let p1 = new Person1('张三');
console.log(p1.getName());
p1.setName("李四");
console.log(p1.getName());
typescript继承
typescript实现继承(extends super)
// 定义一个Person类
class Person{
name:string;
constructor(name:string){
this.name = name
}
run():string{
return `${this.name}在运动`
}
}
let p = new Person('王五');
console.log(p.run());
// Web类来继承Person类
class Web extends Person{
constructor(name:string){
super(name); // 初始化父类的构造函数
}
}
let w = new Web('赵六');
console.log(w.run());
typescript继承中 父类和子类方法一致时
// 注意:如果子类和父类有同样的方法,会优先使用子类的方法
class Person{
name:string;
constructor(name:string){
this.name=name;
}
run():string{
return `${this.name}在运动`
}
}
class Web extends Person{
constructor(name:string){
super(name); /*初始化父类的构造函数*/
}
run():string{
return `${this.name}在运动-子类`
}
work(){
alert(`${this.name}在工作`)
}
}
var w=new Web('李四');
alert(w.run()); // 李四在运动-子类
typescript中类的修饰符(public,protected,provate)
- public:公有 在类里面、子类、类外面都可以访问
class Person{
public name:string;
constructor(name:string){
this.name = name;
}
getName():string{
return this.name
}
}
let p1 = new Person('张三');
console.log(p1.getName()); // ‘张三’ 在类里面访问
console.log(p1.name); // ‘张三’ 在类外面访问
class Man extends Person{
constructor(name:string){
super(name)
}
}
let m = new Man('李四');
console.log(m.name); // ‘李四’ 在子类里面访问
- protected: 保护类型 在类里面、子类里面可以访问,在类外部无法访问
class Person{
protected name:string;
constructor(name:string){
this.name = name;
}
getName():string{
return this.name
}
}
let p1 = new Person('张三');
console.log(p1.getName()); // ‘张三’ 在类里面访问
// console.log(p1.name); // 无法类外面访问 error Property 'name' is protected and only accessible within class 'Person' and its subclasses.
class Man extends Person{
constructor(name:string){
super(name)
}
}
let m = new Man('李四');
console.log(m.name); // ‘李四’ 在子类里面访问
- private:私有 只可在类里面可以访问,子类、类外面无法访问
class Person{
private name:string;
constructor(name:string){
this.name = name;
}
getName():string{
return this.name
}
}
let p1 = new Person('张三');
console.log(p1.getName()); // ‘张三’ 在类里面访问
// console.log(p1.name); // 无法类外面访问 error Property 'name' is protected and only accessible within class 'Person' and its subclasses.
class Man extends Person{
constructor(name:string){
super(name)
}
}
let m = new Man('李四');
// console.log(m.name); // 无法再子类里面访问 error Property 'name' is private and only accessible within class 'Person'
如果不加修饰符,默认为public公有
本文深入探讨 TypeScript 中的函数声明、匿名函数、方法传参、可选参数、默认参数、剩余参数及函数重载的概念与用法。同时,详细解析 TypeScript 类的定义,包括属性、构造函数、方法,以及类的修饰符(public, protected, private)的使用。此外,还介绍了 TypeScript 的继承机制及其在实际编程中的应用。
658

被折叠的 条评论
为什么被折叠?



