【问题标题】:how to rewrite function so that it can be used in child classes如何重写函数以便它可以在子类中使用
【发布时间】:2020-08-22 19:10:35
【问题描述】:

如何重写函数init,使其可以放在父类中,在子类中调用。并将所需的功能传递给它。因为在GetWeatherForCurrentCity 类中,我们从方法getCoordinates 中获取位置,而在GetWeatherForRandomCity 类中,我们从方法getLocation 中获取位置。

class Weather {

    async getCoordinates(city) {
        //code
    }

    async getLocation() {
        //code
    }

    async getWeatherForecast(locationCoordinates) {
        //code
    }

    fahrenheitToCelsius(temp) {
        //code
    }

    renderForecastInfo(currently, daily) {
        //code
    }
};

class GetWeatherForCurrentCity extends Weather {

    init() {
        this.getLocation().then((location) => {
            const { loc } = location;
            return this.getWeatherForecast(loc);
        }).then((forecast) => {
            const { currently, daily } = forecast;
            this.renderForecastInfo(currently, daily);
        });
    }
};

class GetWeatherForRandomCity extends Weather {
    constructor(city) {
        super();
        this.city = city;
    }

    makeRandom() {
        //code
    };

    init() {
        
        this.getCoordinates(this.makeRandom()).then(coords => {
            return this.getWeatherForecast(coords);
        }).then((forecast) => {
            const { currently, daily } = forecast;
            this.renderForecastInfo(currently, daily);
        });
    }
}

【问题讨论】:

  • 我真诚地建议你不要在这里使用classes 和继承。只需将其设为简单的 function 即可调用,更加灵活。
  • @Bergi 关于我的任务我必须使用继承
  • 真可惜。不过,您可以编写一个辅助方法 getAndRenderForecast,它将位置的坐标作为参数,并在您的 init 方法中调用它以避免代码重复。
  • 了解更多关于 JS 中的多态性,请参阅此链接stackoverflow.com/questions/44391149/…

标签: javascript class oop asynchronous promise


【解决方案1】:

也许只是在父类中创建init 函数并使用参数调用它?像这样:

class Weather {

    async getCoordinates(city) {
        //code
    }

    async getLocation() {
        //code
    }

    async getWeatherForecast(locationCoordinates) {
        //code
    }

    fahrenheitToCelsius(temp) {
        //code
    }

    renderForecastInfo(currently, daily) {
        //code
    }
    
    init(locationCoordinates) {
      this.getWeatherForecast(locationCoordinates)
        .then((forecast) => {
            const { currently, daily } = forecast;
            this.renderForecastInfo(currently, daily);
        });
    }
};

class GetWeatherForCurrentCity extends Weather {

    init() {
        this.getLocation().then((location) => {
            Weather.prototype.init.call(this, location.loc);
        });
    }
};

class GetWeatherForRandomCity extends Weather {
    constructor(city) {
        super();
        this.city = city;
    }

    makeRandom() {
        //code
    };

    init() {
        this.getCoordinates(this.makeRandom()).then(coords => {
            Weather.prototype.init.call(this, coords);
        });
    }
}

【讨论】:

    【解决方案2】:

    只需在父类中移动函数,然后在子类中重新声明或覆盖它(这称为多态性,是 OOP 概念之一)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-04
      • 2012-03-13
      • 1970-01-01
      • 2012-02-10
      • 1970-01-01
      • 1970-01-01
      • 2014-12-29
      • 1970-01-01
      相关资源
      最近更新 更多