TypeScript 函数 类

一.函数

1.函数的基本使用

//匿名函数

const makeMoney =function(cc:number,aa:number):number{
    return aa+cc
}
let res =makeMoney(100,50)
console.log(res)

有名函数

function writeCode(hour:number,sleep:string){
    return hour
}
let res1 =writeCode(11,"dd");
console.log(`我每天熬夜${res1}小时学习`)

箭头函数

const ccc= (time:number):void=>{
    console.log(`我每天熬夜${time}小时学习`)
}
ccc(24);

接口函数

type myFunc=(x:number,y:number)=>number
const myfunc=(a:number,b:number)=>a+b

2.函数参数的处理

可选参数

const func1:(x:number,y?:number)=>number=function(a,b){
    return a;
}

const func2 =function(a:number,b?:number):number{
    return a;
}
let cc=func2(11,20)
console.log(cc);

func2(11);
func2(10,5);
func2(10,undefined);

函数的默认值

const func3 =function(a:number=1,b:number=2,c:number=3){
    return a+b+c
}
let aa =func3(10,20)
console.log(aa);

func3();
func3(10);
func3(10,20);
func3(10,20,30);

剩余参数

const func4=function(...args:any[]){
    console.log(args);
}
func4(100,100,"cc","ccc","555")

3.构造函数的使用

构造函数

在创建的时候就属性创建出来

var cc =new Function("a","b","return a+b");
var aa = cc(10,20);
console.log(aa);

4.函数重载

function add(a:number,b:number):number;
function add(a:string,b:string):string;
function add(a:number,b:string):number
function add(a:string,b:number):number

function add(a:any,b:any):any{
    return a+b
}
var cc =add(10,20)
console.log(cc);
add("aa","cc")
add(25,"cc")
add("cc",99)


//定义参数类型与参数数量不同
function ac (s1:string):void;
function ac(n1:number):void;
function ac( x:any,y?:any):void{
    console.log(x);
    console.log(y);
    
    
}
ac("cc")
ac(1,)

二.类

1.类的基本使用

class Person{
    //字段属性
    name:string
    age:number
    //  构造函数
    constructor(name:string,age:number){
        this.name=name
        this.age=age
    }
    
    sayHello():void{
        console.log(`我的名字叫${this.name},我今年${this.age}`)
    }
}
let aa = new Person("CC",22)
aa.sayHello()

2.类的继承

class Person{
    //字段属性
    name:string
    age:number
    //  构造函数
    constructor(name:string,age:number){
        this.name=name
        this.age=age
    }
    
    sayHello():void{
        console.log(`我的名字叫${this.name},我今年${this.age}`)
    }
}
// let aa = new Person("CC",22)
// aa.sayHello()


class student extends Person{
    score:string

    constructor(name:string,age:number,score:string){
        super(name,age)
        this.score=score
    }
    sayHello(): void {
        //调用父类中的方法
        super.sayHello()

    //重写父类中的方法
        console.log(`重复我的名字叫${this.name},我今年${this.age},我的成绩是${this.score}`);
        
    }
}
let s = new student("cc",20,"A")
s.sayHello()

3.static与instanceof

static关键字

//static关键字用于定义的数据成员,(属性和方法)为静态的,静态成员可以直接通过类名调用

class StaticTest{
    static salary:string;

    static say():void{
        console.log("我们想要的工资是"+StaticTest.salary);
        
    }
}
StaticTest.salary="18k"
StaticTest.say();

instanceof运算符

// instanceof运算符用于判断对象是否是指定的类型,如果是返回true,否则返回false

class Person{}
let p = new Person
let inPerson = p instanceof Person;
console.log("p是Person实例化出来的嘛?",inPerson);

class student extends Person{}
let s =new student()
let isStudent = s instanceof Person;
console.log("p是Person实例化出来的嘛?",isStudent);

4.类的修饰符

public

public表示公共的,用来指定在创建实例后可以通过实例访问的,也就是类定义的外部可以访问的属性和方法。默认是 public,但是 TSLint 可能会要求你必须用修饰符来表明这个属性或方法是什么类型的。

 private

private修饰符表示私有的,它修饰的属性在类的定义外面是没法访问的:

 protected

protected修饰符是受保护修饰符,和private有些相似,但有一点不同,protected修饰的成员在继承该类的子类中可以访问,我们再来看下上面那个例子,把父类 Parent 的 age 属性的修饰符 private 替换为 protected:

class Person{
    public name:string;
    protected age:number;
    private sex:string;

constructor(name:string,age:number,sex:string){
    this.name=name
    this.age=age
    this.sex =sex
}

   say():void{
        console.log(`我的名字叫${this.name},性别为${this.sex}我今年${this.age}`);
        
    }
}
class student extends Person {
    score:string
    constructor(name:string, age:number,sex:string ,score:string) {
        super(name, age,sex);
        this.score = score;
    }
   
    say():void{
        console.log("aa");
        this.name="我当时"
        this.age= 20
        // this.sex="男"
        this.score="158"
    }
}
class PrintConsole{
    readonly str1:string ="HTML,CSS,JS,VUE,REACT,NODE"
    readonly str2:string
    readonly str3:string
    readonly str4:string
    constructor(str2:string,str3:string,str4:string){
        this.str2=str2
        this.str3=str3
        this.str4=str4
    }
    show():void{
        // this.str2="123"
        console.log(this.str2,
            this.str3,
            this.str4);
        
    }
}
let pc =new PrintConsole("cccc","cccc","cccc")
pc.show()

5.getter与setter

官方的另一个名字: 存取器

// 通过getter/setter来截取对象成员的访问

这个也就 ES6 标准中的存值函数和取值函数,也就是在设置属性值的时候调用的函数,和在访问属性值的时候调用的函数,用法和写法和 ES6 的没有区别

class GetNameClass{
    private _fullName:string="ccc"
    
    get fullName():string{
        console.log("我是get方法")
        return this._fullName
    }
    set fullName(newName:string){
        console.log("我是get方法");
        this._fullName=newName;
    }
}
let starname = new GetNameClass()
starname.fullName="aaa"
console.log(starname);
console.log(starname.fullName);

6.抽象

抽象类一般用来被其他类继承,而不直接用它创建实例。抽象类和类内部定义抽象方法,使用abstract关键字,

abstract class Person{
    abstract name:string
    abstract age : number
    abstract show() :string

    showName():void{
        console.log("黄狗")
    }
}

class student extends Person{
    name:string="黄狗狗"
    age:number=20
    show(){
        return "黄狗黄狗";
    }
}
let s =new student;
let res =s.show()
console.log(res);

7.类的初始化顺序

class old{
    name:string="黄狗"
    constructor(){
        console.log("我的名字是"+this.name);
        
    }
}

class young extends old{
    name:string="黄某"
    constructor(){
        super()
        console.log("我的名字是"+this.name);
        
    }
}
let y =young

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值