目录
一、变量及类型
二、函数
三、类
四、枚举
五、模块化
ArkTS基础编程语法的在线练习地址:https://www.typescriptlang.org/zh/play?#code/Q
一、变量及类型
1、变量声明
let 声明变量,赋值后可以修改
const 声明常量,赋值后便不能再修改
案例:
let a:number=100;
const b:number=200;
2、类型推断
let a=100;
const b=200;
根据初始值进类型推断
console.log(typeof c); //number 类型
3、数据类型
number 表示数字,包括整数和浮点数
string 表示字符串,单双引号均可。
boolean 表示布尔值: true 、 false
数组类型:元素类型[]
对象(object)类型:属性名称和属性值组成的数据结构
let a :number = 100
let b :number = -33
let c :number = 2.5
let d :number = -3.9
let a:string = '你好'
let b:string = "hello"
let isOpen:boolean = true
let isDone:boolean = false
let a: number[] = []
let b: string[] = ['你好', 'hello']
let person: {name:string, age:number, gender:string} = {name:'张三', age:10, gender:'男'};
let person={name:'张三', age:10, gender:'男'};
二、函数
1、函数
function 函数名(参数列表):返回值类型 {方法体}
function sum(a:number,b:number): number{
return a+b
}
注意:返回值类型不写,会自动推断
function sum(a: number, b: number) {
return a + b;
}
1.1特殊语法
=====================================================
可选参数:参数+? 占位,传值显示,不传值则显示 undefined
function getPersonInfo(name: string, age?: number): string {
return `name:${name},age:${age}`;
}
let p1 = getPersonInfo('zhagnsan')
console.log(p1); // "name:zhagnsan,age:undefined"
=====================================================
默认参数:不传值,则使用默认参数
function getPersonInfo(name: string, age: number=20): string {
return `name:${name},age:${age}`;
}
let p1 = getPersonInfo('zhagnsan')
console.log(p1); // "name:zhagnsan,age:20"
1.2特殊类型
=====================================================
联合类型:一个函数可能处理不同类型的值,这种函数就是联合类型
function printNumberOrString(message: number | string) {
console.log(message)
}
printNumberOrString('a')
printNumberOrString(1)
=====================================================
任意类型:函数需要处理任意类型的值,使用 any 类型
function print(message:any) {
console.log(message)
}
print('a')
print(1)
print(true)
2、匿名函数与箭头函数
forEach()接收函数作为参数,每遍历一次数据调用一次函数。
2.1匿名函数
变化过程
=====================================================
定义函数,将函数名作为 forEach() 参数
let numbers: number[] = [1, 2, 3, 4, 5]
function print(num:number):void{
console.log(num);
}
numbers.forEach(print)
=====================================================
直接将函数的定义写在 forEach() 中
let numbers: number[] = [1, 2, 3, 4, 5]
numbers.forEach(function print(num:number):void{
console.log(num);
})
=====================================================
函数名可以省略
参数类型可以省略(根据上下文自动判断)
返回值类型可以省略(根据上下文自动判断)
适用于简单且仅需1次性使用的场景
let numbers: number[] = [1, 2, 3, 4, 5]
numbers.forEach(function (num){
console.log(num);
})
2.2箭头函数
即lambda表达式,即对匿名函数进一步简化
=====================================================
function可以省略,参数列表和函数体用箭头代替
let numbers: number[] = [1, 2, 3, 4, 5]
numbers.forEach((num)=>{
console.log(num);
})
=====================================================
参数只有一个,括号()可以省略
let numbers: number[] = [1, 2, 3, 4, 5]
numbers.forEach( num =>{
console.log(num);
})
=====================================================
函数体只有一行代码,花括号{}可以省略,分号';'可以去掉
let numbers: number[] = [1, 2, 3, 4, 5]
numbers.forEach( num =>
console.log(num)
)
三、类
1、类的定义
class Person {
id:number;
name: string;
age: number = 18;
constructor(id: number, name: string,age: number) {
this.id = id;
this.name = name;
this.age = age;
}
introduce(): string {
return `hello,I am ${this.name},and I am ${this.age} years old`
}
}
2、创建对象
let person = new Person(1,'夏天',32)
3、属性和方法调用
console.log(person.age)
console.log(person.introduce())
4、静态成员(静态属性与静态方法)
class Utils{
static count:number=1;
static toLowerCase(str:string){
return str.toLowerCase();
}
}
console.log(Utils.count);
console.log(Utils.toLowerCase('Hello World'));
5、继承
class Person {
id:number;
name: string;
age: number = 18;
constructor(id: number, name: string,age: number) {
this.id = id;
this.name = name;
this.age = age;
}
introduce(): string {
return `hello,I am ${this.name},and I am ${this.age} years old`
}
}
class Student extends Person {
classNumber: string;
constructor(id: number, name: string,age: number, classNumber: string) {
super(id, name,age);
this.classNumber = classNumber;
}
introduce(): string {
return super.introduce()+`, and I am a student`;
}
}
let student = new Student(1,'xiaoming',21,'三年⼆班');
console.log(student.introduce());
6、访问修饰符
class Person {
private id:number;
private name: string;
private age: number = 18;
constructor(id: number, name: string,age: number) {
this.id = id;
this.name = name;
this.age = age;
}
introduce(): string {
return `hello,I am ${this.name},and I am ${this.age} years old`
}
getId() {
return this.id;
}
setId(id:number) {
this.id = id;
}
getName() {
return this.name;
}
setName(name:string) {
this.name = name;
}
getAge() {
return this.age;
}
setAge(age:number) {
this.age = age;
}
}
let person = new Person(1,'夏天',32)
console.log(person.getId())
console.log(person.getName())
console.log(person.getAge())
7、接口(interface)
interface Person {
id: number;
name: string;
introduce(): void;
}
class Student implements Person {
id: number;
name: string;
age: number;
constructor(id: number, name: string, age: number) {
this.id = id;
this.name = name;
this.age = age;
}
introduce(): void {
console.log('Hello,I am a student');
}
}
let s1: Student = new Student(1, 'zhangsan', 17);
console.log(s1.id);
console.log(s1.name);
console.log(s1.age);
s1.introduce();
8、多态
class Teacher implements Person {
id:number;
name:string;
age:number;
constructor(id:number, name:string, age:number) {
this.id = id;
this.name = name;
this.age = age;
}
introduce(): void {
console.log('Hello,I am a teacher');
}
}
let p1: Person = new Student(1, 'zhangsan', 17);
let p2: Person = new Teacher(2, 'lisi', 35);
p1.introduce();//Hello,I am a student
p2.introduce();//Hello,I am a teacher
8、使用接口描述对象类型
interface Person {
name: string;
age: number;
gender: string;
}
let person: Person = {name:'张三', age:10, gender:'男'};
四、枚举
enum Direction {
UP = 'up',
BOTTOM = 'bottom',
LEFT = 'left',
RIGHT = 'right'
}
console.log(Direction.UP) //up
console.log(Direction.BOTTOM) //bottom
五、模块化
export导出,imiport导入
=======================================================
使用花括号导出多个,产生问题,容易命名冲突
moduleA.ts
export function sayHello() {
console.log('hello module A');
}
export const printStr = 'hello world';
moduleB.ts
export function sayHello() {
console.log('hello module A');
}
export const printStr = 'hello world';
moduleC.ts
import { sayHello, printStr } from './moduleA';
import { sayHello, printStr } from './moduleB';
=======================================================
导入模块重命名,产生问题:冲突内容较多时,写法会冗长
moduleA.ts
export function sayHello() {
console.log('hello module A');
}
export const printStr = 'hello world';
moduleB.ts
export function sayHello() {
console.log('hello module A');
}
export const printStr = 'hello world';
moduleC.ts
import { sayHello as shA, printStr as psA } from './moduleA';
import { sayHello as shB, printStr as psB } from './moduleB';
=======================================================
直接模块对象命名,对象调用(推荐)
moduleA.ts
export function sayHello() {
console.log('hello module A');
}
export const printStr = 'hello world';
moduleB.ts
export function sayHello() {
console.log('hello module A');
}
export const printStr = 'hello world';
moduleC.ts
import * as A from './moduleA';
import * as B from './moduleB';
A.sayHello();
B.sayHello();
=======================================================
默认导出(加default),默认导入
moduleA.ts
export default function sayHello() {
console.log('hello module A');
}
moduleC.ts
import sayHelloA from './moduleA';
sayHelloA();