学习笔记如下:
基础类型
(() => {
let flag: boolean = true
console.log(flag)
let a1: number = 10
let a2: number = 0b1010
let a3: number = 0o12
let a4: number = 0xa
console.log(a1);
console.log(a2);
console.log(a3);
console.log(a4);
let name:string = 'tom'
name = 'jack'
let age:number = 12
const info = `My name is ${name}, I am ${age} years old!`
console.log(info);
let u: undefined = undefined
let n: null = null
let list1: number[] = [1, 2, 3]
let list2: Array<number> = [1, 2, 3]
let t1: [string, number]
t1 = ['hello', 10]
console.log(t1[0].substring(1))
enum Color {
Red,
Green,
Blue
}
let myColor: Color = Color.Green
console.log(myColor, Color.Red, Color.Blue)
let c: Color = Color.Green
let colorName: string = Color[2]
console.log(colorName)
let notSure: any = 4
notSure = 'maybe a string'
notSure = false
let list: any[] = [1, true, 'free']
list[1] = 100
function fn(): void {
console.log('fn()')
}
function fn2(obj:object):object {
console.log('fn2()', obj)
return {}
}
console.log(fn2(new String('abc')))
console.log(fn2(String))
function toString2(x: number | string) : string {
return x.toString()
}
function getLength(x: number | string) {
if ((<string>x).length) {
return (x as string).length
} else {
return x.toString().length
}
}
console.log(getLength('abcd'), getLength(1234))
let b9 = 123
let b10
b10 = 123
b10 = 'abc'
})()
接口
(() => {
interface Iperson{
firstName: string,
lastName:string
}
function showFullName(person: Iperson) {
return person.firstName +'_'+person.lastName
}
const person = {
firstName:'东方',
lastName:'不败'
}
console.log(showFullName(person))
})()
类
(() => {
interface Iperson{
firstName: string,
lastName:string
}
class Person{
firstName: string
lastName: string
fullName: string
constructor(firstName: string, lastName: string) {
this.firstName = firstName
this.lastName = lastName
this.fullName = this.firstName + '_' + this.lastName
}
}
function showFullName(person:Iperson) {
return person.firstName + '_' + person.lastName
}
const person = new Person('诸葛', '孔明')
console.log(showFullName(person))
})()
类的继承
(() => {
class Person{
name: string
age: number
gender:string
constructor(name: string, age: number, gender: string) {
this.name = name
this.age = age
this.gender = gender
}
sayHi(str: string) {
console.log(`我是:${this.name},${str},我今年${this.age}岁,我是${this.gender}的`);
}
}
class Student extends Person{
Grade:string
constructor(name: string, age: number, gender: string,Grade:string) {
super(name, age, gender)
this.Grade = Grade
}
sayHi() {
console.log('我是学生类的中sayhi方法');
super.sayHi('哈哈')
console.log(`我今年上${this.Grade}`);
}
}
const person1 = new Person('大聪明', 18, '男')
person1.sayHi('嘻嘻')
const stu1 = new Student('大明白', 20, '男','五年级')
stu1.sayHi()
})()
类的多态
(() => {
class Animal{
name:string
constructor(name: string) {
this.name = name
}
run(distance: number=11) {
console.log(`${this.name}跑了${distance}米的距离`);
}
}
class Dog extends Animal {
constructor(name: string) {
super(name)
}
run(distance: number=10){
console.log(`${this.name}跑了${distance}米的距离`);
}
}
class Cat extends Animal {
constructor(name: string) {
super(name)
}
run(distance: number=5){
console.log(`${this.name}跑了${distance}米的距离`);
}
}
class Pig extends Animal {
constructor(name: string) {
super(name)
}
run(distance: number=100){
console.log(`${this.name}跑了${distance}米的距离`);
}
}
const ani:Animal = new Animal('动物')
ani.run()
const dog:Dog = new Dog('小黑')
dog.run()
const cat:Cat = new Cat('小黄')
cat.run()
const pig:Pig = new Pig('小白')
pig.run()
const dog1: Animal = new Dog('大黑')
dog1.run()
const cat1:Animal = new Cat('大黄')
cat1.run()
function showRun(ani: Animal) {
ani.run()
}
showRun(dog1)
showRun(cat1)
})()
类属性成员中的修饰符
(() => {
class Person{
public name: string
age: number
gender: string
private data: string
protected address: string
readonly ids:number
constructor(public profection:string,name: string, age: number, gender: string,data:string,address:string,ids:number) {
this.name = name
this.age = age
this.gender = gender
this.data = data
this.address = address
this.ids = ids
}
sayHi() {
console.log(`身份id:${this.ids},你好,我叫${this.name},是个${this.gender},今年${this.age}岁了,今年是${this.data},家在${this.address}`);
}
}
class Student extends Person{
constructor(profection:string,name: string, age: number, gender: string,data:string,address:string,ids:number) {
super(profection,name, age, gender, data, address, ids)
}
eat() {
console.log(`身份id:${this.ids},你好,我叫${this.name},是个${this.gender},今年${this.age}岁了,家在${this.address}`);
}
}
const per1 = new Person('计算机','小明',20,'男孩','2021','深圳',1)
per1.sayHi()
const stu1 = new Student('计算机','小红', 18, '女孩','2022','梅州',2)
stu1.eat()
console.log(per1.name);
console.log(per1.ids);
})()
静态成员
(() => {
class Person{
static name1: string = '葫芦娃'
constructor() {
}
static sayHi() {
console.log('hello');
}
}
const person: Person = new Person()
console.log(Person.name1);
Person.name1 = '变形金刚'
console.log(Person.name1);
Person.sayHi();
})()
抽象类
(() => {
abstract class Animal{
abstract name:string
abstract eat()
sayHi() {
console.log('hello');
}
}
class Dog extends Animal {
name:string ='小黄'
eat() {
console.log('吃狗粮');
}
}
const dog:Dog = new Dog()
dog.eat()
dog.sayHi()
console.log(dog.name);
})()
ts函数写法
(() => {
function add(x: string, y: string): string {
return x + y
}
console.log(add("20", "20"));
const add2 = function (x: number, y: number): number {
return x + y
}
console.log(add2(10, 20));
const add3: (x: number, y: number) => number = function (x: number, y: number): number {
return x + y
}
console.log(add3(1, 2));
})()
参数:可选参数,默认参数,动态参数
(() => {
const getFullName = function (firstName: string = '李', lastName?: string): string {
if (lastName) {
return firstName + '_' + lastName
}
return firstName
}
console.log(getFullName('赵', '子龙'));
})()
(() => {
function info(name: string, ...args: string[]) {
console.log(name, args);
}
info('必选参数', '动态传入的参数1', '动态传入的参数2', '动态传入的参数3', '动态传入的参数4')
})()
函数重载
(() => {
function add(x: string, y: string): string
function add(x: number, y: number): number
function add(x: string | number, y: string | number): string | number {
if (typeof x === 'string' && typeof y === 'string') {
return x + y
} else if (typeof x === 'number' && typeof y === 'number') {
return x + y
}
}
console.log(add('祝福', '平安'));
console.log(add(1, 2));
})()
泛型:泛型的定义以及使用,一个函数多个泛型参数,泛型接口,泛型类
(() => {
function getArr1(val: number, count: number): number[] {
const arr: number[] = []
for (let i = 0; i < count; i++) {
arr.push(val)
}
return arr
}
function getArr2(val: string, count: number): string[] {
const arr: string[] = []
for (let i = 0; i < count; i++) {
arr.push(val)
}
return arr
}
function getArr3<T>(val: T, count: number): T[] {
const arr: T[] = []
for (let i = 0; i < count; i++) {
arr.push(val)
}
return arr
}
const arr3 = getArr3<string>("aaaa", 3)
console.log(arr3);
console.log(arr3[0].split(''));
const arr4 = getArr3<number>(200, 3)
console.log(arr4);
console.log(arr4[0].toFixed(2));
})()
(() => {
function getMsg<K, V>(val1: K, val2: V): [K, V] {
return [val1, val2]
}
const arr1 = getMsg<string, number>('tony', 123456)
console.log(arr1[0].split(''), arr1[1].toFixed(2));
})()
(() => {
interface IbaseCRUD<T> {
data: T[]
add: (t: T) => T
getById: (id: number) => void
}
class User {
id?: number
name: string
age: number
constructor(name: string, age: number) {
this.name = name
this.age = age
}
}
class UserCRUD implements IbaseCRUD<User> {
data: User[] = []
add(user: User): User {
user = { ...user, id: Date.now() + Math.random() }
this.data.push(user)
return user
}
getById(id: number): void {
this.data.find(item => item.id === id)
}
}
const userCRUD = new UserCRUD()
const { id } = userCRUD.add(new User('java', 1))
userCRUD.add(new User('pythone', 2))
userCRUD.add(new User('php', 3))
userCRUD.add(new User('javascript', 4))
console.log(userCRUD.data)
})()
(() => {
class GenericNumber<T> {
zeroValue: T
add: (x: T, y: T) => T
}
let myGenericNumber = new GenericNumber<number>()
myGenericNumber.zeroValue = 0
myGenericNumber.add = function (x, y) {
return x + y
}
let myGenericString = new GenericNumber<string>()
myGenericString.zeroValue = 'abc'
myGenericString.add = function (x, y) {
return x + y
}
console.log(myGenericString.add(myGenericString.zeroValue, 'test'))
console.log(myGenericNumber.add(myGenericNumber.zeroValue, 12))
})()
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!