Skip to content
Advertisement

Calling a method in a method [closed]

I have to implement a method (Factory-method) called createFromCelsius, where i can create new Objects with the specific Temperature unit (celsius, kelvin…). I also created methods to convert the code from one unit to another. The problem is i dont know how i can connect the covert-methods to the createFromCelsius method. I tried to make all my methods and variables abstact so that i can call them. The Question: How can i implement the convert-methods in the create-methods?

Here’s my code:

JavaScript

Advertisement

Answer

As mentioned, abstract doesn’t do what you want

A factory would work like this

JavaScript

However, you should define what base unit of measurement your temp field in the class should be rather than passing through the factory parameter. For example, if you want to convert everything to Kelvin, that’d look like

JavaScript

Then you’d only need public getter methods for one way conversions from Kelvin

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement