typescript之路(二)

  • 基本类
    JavaScript程序使用函数和基于原型的继承来创建可重用的组件,而java是运用类和集成等来实现可重用的组件等。typescript也支持这种写法。
class Greeter {
    greeting: string; //参数
    constructor(message: string) {//构造函数
        this.greeting = message;
    }
    greet() {//类的方法
        return "Hello, " + this.greeting;
    }
}

let greeter = new Greeter("world");//建立一个

编译之后的代码为:

var Greeter = /** @class */ (function () {
    function Greeter(message) {//对应ts constructor
        this.greeting = message;
    }
    Greeter.prototype.greet = function () {//对应类的方法,在js里面,相当于向Greeter这个最高级别的父级的原型链上添加了一个方法
        return "Hello, " + this.greeting;
    };
    return Greeter;
}());//自启动函数,返回一个函数。Greeter
var greeter = new Greeter("world");//执行这句代码相当于调用Greeter方法,为其内的变量赋值

  • 继承
class Animal{
    move(step:number =0){
        console.log(`该动物移动了${step}`)
    }
}
class Dog extends Animal{
    bark(){
        console.log("汪!汪!汪");
    }
}
const dog=new Dog();
dog.bark();
dog.move(10);

编译之后的代码:

var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }//__的构建函数为dog
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());//dogproto属性指向animal
    };
})();
var Animal = /** @class */ (function () {
    function Animal() {
    }
    Animal.prototype.move = function (step) {
        if (step === void 0) { step = 0; }
        console.log("\u8BE5\u52A8\u7269\u79FB\u52A8\u4E86" + step);
    };
    return Animal;
}());
var Dog = /** @class */ (function (_super) {
    __extends(Dog, _super);//定义dog继承Animal 

    function Dog() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    Dog.prototype.bark = function () {
        console.log("汪!汪!汪");
    };
    return Dog;
}(Animal));
var dog = new Dog();
dog.bark();
dog.move(10);

Dog
			    __proto__: Animal
			bark: ƒ ()
			constructor: ƒ Dog()
			__proto__:
			move: ƒ (step)
			constructor: ƒ Animal()
			__proto__: Object

_extends(Dog,__super)主要做的事情是将dog的prototype指向Animal,这样就达到了dog继承animal,实现其实就是dog.prototype=new Animal();

  • 公共,私有与受保护的修饰符
    默认为public修饰符,公共的,可以外new之后访问读取。
    private私有变量,只能通过函数的返回到外部。
    readonly关键字将属性设置为只读的。 只读属性必须在声明时或构造函数里被初始化。

  • 抽象类
    抽象方法的语法与接口方法相似。 两者都是定义方法签名但不包含方法体。 然而,抽象方法必须包含 abstract关键字并且可以包含访问修饰符。

abstract class Animal {
    abstract makeSound(): void;
    move(): void {
        console.log('roaming the earch...');
    }
}
let a=new Animal();//错误不能直接实例化。
abstract class Department {

    constructor(public name: string) {
    }

    printName(): void {
        console.log('Department name: ' + this.name);
    }

    abstract printMeeting(): void; // 必须在派生类中实现
}

class AccountingDepartment extends Department {

    constructor() {
        super('Accounting and Auditing'); // 在派生类的构造函数中必须调用 super()
    }

    printMeeting(): void {
        console.log('The Accounting Department meets each Monday at 10am.');
    }

    generateReports(): void {
        console.log('Generating accounting reports...');
    }
}

函数

函数的定义和js函数的定义差不多,在ts中,定义函数可以加入类型检查。

function buildName(firstName: string, lastName = "Smith") {
    return firstName + " " + lastName;
}

let result1 = buildName("Bob");                  // works correctly now, returns "Bob Smith"
let result2 = buildName("Bob", undefined);       // still works, also returns "Bob Smith"
let result3 = buildName("Bob", "Adams", "Sr.");  // error, too many parameters
let result4 = buildName("Bob", "Adams");  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以使用for循环或for...of循环来遍历维数组。例如: for(let i=0;i<arr.length;i++){ for(let j=0;j<arr[i].length;j++){ console.log(arr[i][j]); } } 或 for(let row of arr){ for(let element of row){ console.log(element); } } ### 回答2: 在 TypeScript 中,遍历维数组有多种方式可以实现。以下是常用的几种方法: 1. 使用 for 循环嵌套遍历维数组: ```typescript const arr: number[][] = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]; for (let i = 0; i < arr.length; i++) { for (let j = 0; j < arr[i].length; j++) { console.log(arr[i][j]); // 输出数组元素 } } ``` 2. 使用 forEach 方法遍历维数组: ```typescript const arr: number[][] = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]; arr.forEach((row: number[]) => { row.forEach((item: number) => { console.log(item); // 输出数组元素 }); }); ``` 3. 使用 for...of 循环遍历维数组: ```typescript const arr: number[][] = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]; for (const row of arr) { for (const item of row) { console.log(item); // 输出数组元素 } } ``` 以上是几种常见的遍历维数组的方式,根据不同的需求选择适合的方法进行遍历操作。 ### 回答3: 在Typescript中遍历维数组可以使用嵌套的for循环。下面是示例代码: ```typescript let matrix: number[][] = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]; for (let i = 0; i < matrix.length; i++) { for (let j = 0; j < matrix[i].length; j++) { console.log(matrix[i][j]); } } ``` 这段代码首先定义了一个维数组`matrix`,然后使用嵌套的for循环来遍历该数组。外层循环用于遍历行,内层循环用于遍历列。在循环体中,可以通过`matrix[i][j]`来访问维数组中的元素,并对其进行相应的操作。 以上代码会依次输出1、2、3、4、5、6、7、8、9,表示成功遍历了维数组中的所有元素。 需要注意的是,以上示例是针对维数字数组的情况。如果维数组是由其他类型的元素组成,可以根据实际情况来替换元素类型和遍历过程中的操作。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值