TypeScript2

函数
 

函数的基本使用

export default {}
//类的基本使用
class aa {
    //定义属性
    name: string
    age: number
    //构造函数
    constructor(aa: string, bb: number) {
        this.name = aa;
        this.age = bb;
    }
    //函数方法
    hx() {
        console.log(`我的男生是:${this.name},球员号码是:${this.age}`);
    }
}
//实例化
let ss = new aa("库里", 30);
ss.hx();

函数参数的处理

export default {}
//可选参数
// type aa = (a: number, b: number) => number;
// const bb: aa = (x: number, y?: number) => {
//     return x + y  //报错但可以运算
// };
// console.log(bb(10, 20));

const func1: (a: number, b: number) => number = (x: number, y: number) => {
    return y;
}
console.log(func1(1000, 2000));

const func2 = function (a: number, b?: number): number {
    return a;
}
func2(10);
func2(10, 20);
func2(10, undefined);

//参数的默认值
const func3 = function (x: number = 100, y: number = 200, z: number = 300) {
    return x + y + z;
}
//let res= func3(100, 200);
let res = func3(100, 200, 400);
// func3(); //600
// func3(1, 2) //303
console.log(res);

//函数的剩余参数
const func4 = function (...args: any[]) {
    console.log(args);
}
func4(1, 2, 3, 4, 5, "李易峰");

const func5 = function (a: number, b: number, ...args: any[]) {
    console.log(a);
    console.log(b);
    console.log(args);

}
func5(10, 20, 30, "邱淑贞", "邢菲");

构造函数

export default {}
//构造函数
var myfunc = new Function("a", "b", "return a*b");
var res = myfunc(10, 20);
console.log(res); 

重载函数

export default {}
//函数重载
// 重载是方法名字相同,而参数不同r返回类型可以相同也可以不同。
// 每个重载的方法(或者构造函数)都必须有一个独一无二的参数类型列表。

function addFunc(a: number, b: number): number
function addFunc(a: string, b: string): string
function addFunc(a: number, b: string): number
function addFunc(a: string, b: number): number
function addFunc(a: any, b: any): any {
    return a + b;
}
addFunc(10, 20);
addFunc("刘亦菲", "鞠婧祎");
addFunc("杨幂", 18);
addFunc(18, "迪丽热巴");


//参数数量不同
function star(s1: string): string
function star(s1: number, n1: number): void
function star(s1: any, n1?: any): any {
    let res = s1 + n1;
    console.log(s1);
    console.log(n1);
}
star("11");
star(10, 10);


类的基本使用

export default {}
//类的基本使用
class aa {
    //定义属性
    name: string
    age: number
    //构造函数
    constructor(aa: string, bb: number) {
        this.name = aa;
        this.age = bb;
    }
    //函数方法
    hx() {
        console.log(`我的男生是:${this.name},球员号码是:${this.age}`);
    }
}
//实例化
let ss = new aa("库里", 30);
ss.hx();

类的继承

export default {}
class aa {
    a1: string;
    a2: number;
    constructor(b1: string, b2: number) {
        this.a1 = b1;
        this.a2 = b2;
    }
    hs() {
        console.log(`历史三分王:${this.a1},球衣号:${this.a2}`);
    }
}
//继承
class cc extends aa {
    c1: any
    constructor(b1: string, b2: number, c2: any) {
        super(b1, b2);
        this.c1 = c2;
    }
    hss() {
        super.hs();
        console.log(`历史三分王:${this.a1},球衣号:${this.a2},${this.c1}`);
    }
}
let ss = new cc("库里", 30, "继承");
ss.hss();

static与instanceof

export default {}
//static:静态的
//使用static来修饰的函数只能通过类名进行访问,不能通过对象来访问
class staticTest {
    static salary: number
    static say(): void {
        console.log(`我们想要的工资是${staticTest.salary}k`)
    }
}
staticTest.salary = 18;
staticTest.say();

// instanceof运算符
// instanceof 运算符用于判断对象是否是指定的类型,如果是返回 true,否则返回false
class Person { }
let p = new Person();
let isPerson = p instanceof Person;
console.log("p是Person实例化出来的吗?", isPerson);

//继承
class Studemt extends Person { }
let s = new Studemt();
let isStudent = s instanceof Person;
console.log("s是Person实例化出来的吗?", isStudent);

类的修饰符

export default {}
// public(默认): 公有,可以在任何地方被访问
// protected: 受保护,可以被其自身以及其子类访问
// private: 私有,只能被其定义所在的类访问。

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(this.name);
        console.log(this.age);
        //console.log(this.sex); //私有不可继承
        console.log(this.score);
    }
}
let s = new Student("王心凌", 18, "女", "A");
s.say();

//readonly: 可以使用readonly关键字将属性设置为只读的。只读属性必须在声明时或构造函数里被初始化。
class Print {
    readonly str1: string = "我是声明时赋值的"
    readonly str2: string
    readonly str3: string
    readonly str4: string
    readonly str5: string
    constructor(str2: string, str3: string, str4: string, str5: string) {
        this.str2 = str2;
        this.str3 = str3;
        this.str4 = str4;
        this.str5 = str5;
    }
    hx() {
        console.log(this.str1, this.str2, this.str3, this.str4, this.str5);

    }
}
let ss = new Print("库里1", "库里2", "库里3", "库里4");
ss.hx();

getter与setter

export default {}
// 如果存在get,但没有set,则该属性自动是只读的
// 如果没有指定setter参数的类型,它将从getter的返回类型中推断出来访问器和设置器必须有相同的成员可见性

class MyName {
    private _fullName: string = "杨幂";
    //读取字段的值
    get fullName() {
        console.log("获取到get属性");
        return this._fullName;
    }
    //添加字段值 
    set fu11Name(newName: string) {
        console.log("获取到set属性");
        this._fullName = newName;
    }
}
let n = new MyName();
n.fu11Name = "刘亦菲";//赋值

console.log(n);
console.log(n.fullName);//取值

抽象类

export default {}
// 定义
// 抽象类做为其它派生类的基类使用。它们一般不会直接被实例化抽象类是专门用于定义哪些不希望被外界直接创建的类的
// 抽象类和接口一样用于约束子类

// 抽象类和接口区别
// 抽象方法必须包含abstract关键字并且可以包含访问修饰符
// 接口中只能定义约束, 不能定义具体实现。而抽象类中既可以定义约束, 又可以定义具体实现
abstract class Person {
    abstract name: string
    abstract age: number
    abstract show(): string

    showName(): void {
        console.log("hello world");

    }
}

class Studemt extends Person {
    name: string = "张三"
    age: number = 18
    show() {
        return "仙剑奇侠传"
    }
}

let s = new Studemt()
let res = s.show()
console.log(res);

类的初始化顺序

export default {}
class Old {
    name: string = "李易峰";
    constructor() {
        console.log(`我是${this.name},我主演了古剑奇谭`);
    }
}
class Young extends Old {
    name: string = "刘亦菲";
    constructor() {
        super();
        console.log(`我是${this.name},我主演了天龙八部`)
    }
};
let y = new Young();

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值