4.typescript代码练习

//类和继承
type Color2 ='Black'|'White'
type File2 ='A'|'B'|'C'|'D'|'E'|'F'|'G'|'H'
type Rank2 = 1|2|3|4|5|6|7|8 //1  
class Position
{
    constructor(
        public file:File2,
        public rank:Rank2
    ){}
}
abstract class Piece{ //abstract 限制用户直接实例化Piece
    protected position:Position
    constructor(
        private readonly color:Color2,
        file:File2,
        rank:Rank2
    )
    {

        this.position = new Position(file,rank)
    }
    public moveTo(position:Position) //模式public
    {
        this.position = position
    }
    abstract canMoveTo(position:Position):boolean //实现抽象类时也要实现抽象方法
   
     
   
}
//typescript super, this
let set  = new Set
set.add(1).add(2).add(3)
set.has(2)
set.has(5)
//接口
interface Sushi111
{
    calories:number
    salty:boolean
    tasty:boolean
}
type Cake111 ={
    calories:number
    salty:boolean
    tasty:boolean
}
type Food ={
    calories:number
     
    tasty:boolean
}
type Sushi =Food &{
    salty:boolean
}
type Cake = Food &{ //&表示交集运算符
    sweet:boolean
}
interface Food2{
    calories:number
     
    tasty:boolean

}
interface Sushi2 extends Food2{
    salty:boolean
}
interface Sushi2 extends Food2{ //同名接口不会报错
    salty:boolean
}
interface Cake2 extends Food2{//接口扩展
    sweet:boolean
}
interface Animal
{
    eat(food:string):void
    sleep(hours:number):void
}
interface Feline{
    meow():void
}
class Cat implements Animal{
    eat(food:string)
    {
        console.info('Ate some',food,'.mmm!')
    }
    sleep(hours: number): void {
        console.info('slept for',hours,'hours')
    }
}
//类实现多个接口
class Cat2 implements Animal,Feline{
    eat(food:string)
    {
        console.info('Ate some',food,'.mmm!')
    }
    sleep(hours: number): void {
        console.info('slept for',hours,'hours')
    }
    meow(): void {
        console.info('neow')
    }
}
let cat_obj = new Cat2  //类的调用使用方法
cat_obj.eat('rice')
 cat_obj.sleep(8)
 cat_obj.meow()
 
 //最简单的数据库
 type State ={
    [key:string]:string
 }
 class StringDatabase{
    state:State ={}
    get(key:string):string|null{
        return key in this.state?this.state[key]:null
    }
    set(key:string,value:string):void{
        this.state[key] = value
    }
    static from(state:State){
        let db = new StringDatabase
        for(let key in state){
            db.set(key,state[key])
        }
    }
 }
 //设计模式,工厂模式5.11
 type Shoe = {
    purpose:string
 }
 class BalletFlat implements Shoe{
    purpose= "dancing"
 }
 class Boot implements Shoe{
    purpose = 'woodcutting'
 }
 class Sneaker implements Shoe{
    purpose = 'walking'
 }
 //建造制鞋工厂
 let shoe2 = {
    create(type:'balletFlat'|'boot'|'sneaker'):Shoe{
        switch(type){
            case 'balletFlat':return new BalletFlat
            case 'boot':return new Boot
            case 'sneaker':return new Sneaker
        }
    }
}
shoe2.create('boot')
//建造者模式

class RequestBuilder2{
   private url:string|null=null
   private data:object|null = null
   private method:'get'|'post'|null=null
   setURL(url:string):this{
    this.url = url
    return this
   }
   setData(data:object):this{
    this.data = data
    return this
   }
   setMethod(method:'get'|'post'):this{
    this.method = method
    return this
   }
   send()
   {

   }
}
//测试
new RequestBuilder2().setURL('/usr')
.setMethod('get')
.setData({firstName:'annaa2'})






 
posted @ 2024-03-27 14:09  txwtech  阅读(51)  评论(0编辑  收藏  举报