letname: string ='xiao ming';
name ='xiao li';constage: number =20;// age = 24; 常量不可重新赋值
console.log(name);
console.log(age.toString());
console.log(`my name is ${name}, i am ${age} yeaers old`);
letstudents: Array<string>=['xiao ming','xiaozhang','xiaohong'];// 1. for(let i =0; i < students.length; i++){
console.log(students[i]);}// 2. for(let stu of students){
console.log(stu);}// 3. let index =0;while(index < students.length){
console.log(students[index++]);}
函数
// // 函数标识function 函数名字、参数、返回值、函数体functionprintStudentsInfo(students: string[]):void{for(let stu of students){
console.log(stu);}}printStudentsInfo(students_1);
classPerson{publicname: string ='xiao xiao';private_age: number =20;isMale: boolean =true;// 构造函数constructor(name: string,age: number,isMale: boolean){this.name = name;this._age = age;this.isMale = isMale;}getage(): number {returnthis._age;}setage(age: number){this._age = age;}printInfo(){if(this.isMale){
console.log(`${this.name} is a boy, and he is ${this.age} years old`);}else{
console.log(`${this.name} is a girl, and she is ${this.age} years old`);}}}constp1: Person =newPerson('xiao x',22,true);
p1.printInfo();// 私有变量不可访问,没有age的属性只有age的方法用来获取_age的属性// console.log(p1.age()); console.log(p1._age.toString());
console.log(p1.age.toString());