Другой способ передать аргументы в машинописи - PullRequest
0 голосов
/ 05 марта 2020

У меня есть класс машинописи и метод в нем. Этот метод принимает три аргумента.

class MyClass {

 public static carStatus(name : string , color : string , isReady : boolean){
    let result = isReady ? 'is ready' : 'is not ready';
    return `${color} ${name} ${result}.`;
 }
}

let carStatus = MyClass.carStatus('pride' , 'white' , true);
console.log(carStatus);

Я хочу установить третий аргумент (isReady) из скобок в методе. И я знаю, что это можно сделать следующим образом:

class MyClass {

public static isReady : boolean;

  public static carStatus(name : string , color : string){
    let result = this.isReady ? 'is ready' : 'is not ready';
    return `${color} ${name} ${result}.`;
  }
}

MyClass.isReady = false;
let carStatus = MyClass.carStatus('pride' , 'white');
console.log(carStatus);

Есть ли другой способ сделать это?

1 Ответ

0 голосов
/ 05 марта 2020

Я полагаю, что самым простым подходом было бы использование отдельного метода для установки значения isReady и одного класса CarStatus без использования методов c:

class CarStatus {
    private isReady: boolean;

    constructor(private name: string, private color: string) {
        this.name = name;
        this.color = color;
    }

    public setReady() {
        this.isReady = true;
    }

    public getStatus(): string {
        let result = this.isReady ? 'is ready' : 'is not ready';
        return `${this.color} ${name} ${result}.`;
    }
}

let carStatus = new CarStatus("pride", "white");
carStatus.setReady();
console.log(carStatus.getStatus());

Вы также можете использовать свободный подход, если вы считаете, что каждое из понятий не обязательно требуется или может быть задано в разное время. В зависимости от ситуации это может быть излишним, так что просто в качестве примера:

class CarStatus {  
    constructor(private name: string, private color: string, private isReady: boolean) {
        this.name = name;
        this.color = color;
        this.isReady = isReady;
    }

    public getStatus(): string {
        let result = this.isReady ? 'is ready' : 'is not ready';
        return `${this.color} ${name} ${result}.`;
    }
}

class CarStatusBuilder {
    private name: string;
    private color: string;
    private isReady: boolean;

    public SetReady(): CarStatusBuilder {
        return new CarStatusBuilder() { this.isReady = true};
    }

    public WithName(name: string): CarStatusBuilder {
        this.name = name;
        return this;
    }

    public WithColor(color: string): CarStatusBuilder {
        this.color = color;
        return this;
    }

    public Build(): CarStatus{
        return new CarStatus(this.name, this.color, this.isReady);
    }
}

let carStatus = new CarStatusBuilder()
    .WithColor("white")
    .WithName("pride")
    .Build();
console.log(carStatus.getStatus());
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...