TypeScript函数、类、继承、类修饰符

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)
  1. 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); // ‘李四’ 在子类里面访问


  1. 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); // ‘李四’ 在子类里面访问


  1. 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公有

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
TypeScript修饰符有以下几种: 1. public public是默认修饰符,即不写修饰符时默认为public。public修饰的属性或方法可以在的内部和外部使用。 2. private private修饰的属性或方法只能在的内部使用,无法在的外部使用。 3. protected protected修饰的属性或方法可以在的内部和子中使用,但无法在的外部使用。 4. readonly readonly修饰的属性只能在初始化时或构造函数中赋值,之后就无法再修改。 示例代码: ```typescript class Person { public name: string; // 公共属性 private age: number; // 私有属性 protected gender: string; // 保护属性 readonly id: number; // 只读属性 constructor(name: string, age: number, gender: string, id: number) { this.name = name; this.age = age; this.gender = gender; this.id = id; } public sayHello() { // 公共方法 console.log(`Hello, my name is ${this.name}.`); } private getAge() { // 私有方法 return this.age; } protected getGender() { // 保护方法 return this.gender; } } class Student extends Person { public getGenderAndAge() { console.log(`My gender is ${this.getGender()} and age is ${this.getAge()}.`); } } const person = new Person('Tom', 18, 'male', 1001); console.log(person.name); // Tom // console.log(person.age); // 无法访问 // console.log(person.gender); // 无法访问 // person.id = 1002; // 无法修改 person.sayHello(); // Hello, my name is Tom. const student = new Student('Jerry', 17, 'female', 1002); // console.log(student.age); // 无法访问 // console.log(student.gender); // 无法访问 console.log(student.name); // Jerry console.log(student.id); // 1002 student.getGenderAndAge(); // My gender is female and age is undefined. ``` 在上面的示例代码中,我们定义了一个Person,其中包含了公共属性、私有属性、保护属性、只读属性、公共方法、私有方法和保护方法。这些属性和方法都有不同的修饰符,可以在不同的场景下使用。同时,我们还定义了一个Student继承自Person,可以访问Person中的保护属性和保护方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值