我对如何在下面的代码中实现不同数据类型的转换感到困惑。转换包括(int 到 string、string 到 int、float 到 int 等)我的老师说当我读/写文件时这可以很容易地完成,但我仍然很困惑。我将不胜感激任何帮助或建议,谢谢!这是我的代码:
//files
var fs = require("fs");
//reading files
fs.readFile('sources.txt', (err, data) => {
console.log("File output: " + data.toString());
//writing files
fs.writeFile('written.txt',data,(err, result) => {
if(err) console.log('error', err);
});
});
// planet class
class Planet{
constructor(name, numberOfMoons, size) {
this.name = name;
this.numberOfMoons = numberOfMoons;
this.size = size;
}
orbit(){
//return value
return `${this.name} is a planet and therefore orbits around the sun.`
}
}
//inheritance class
class DwarfPlanet extends Planet{
constructor(name, numberOfMoons, size, orbitalNeighbourhood) {
super(name, numberOfMoons, size);
this.orbital = orbitalNeighbourhood;
}
getOrbital(){
//return value
return `${this.name} is a dwarf planet because it doesn't have a clear orbital neighnourhood "`
}
}
let earth = new Planet('Earth', 1 , 6371);
console.log(earth.orbit());
let pluto = new DwarfPlanet("Pluto", 5 , 1188, 'Kuiper Belt');
console.log(pluto.getOrbital());
//Array of Objects (anonymous option)
var stars = [
{
name: 'Sun',
temperature: 5778,
colour: 'White'
},
{
name: 'Pistol',
temperature: 11800,
colour: 'Blue'
},
{
name: "Antares",
temperature: 3500,
colour: "Red"
}
];
// Array of Objects (using Planet Class)
var planets = [
new Planet('Earth', 'One moon', '6,371 km'),
new Planet('Mars', 'Two mooons', '3,389 km'),
new Planet('Uranus', 'Twenty-seven moons', '25,362 km'),
new Planet('Saturn', 'Fifty-three moons', '58,232 km'),
];
console.log("Fun Fact: the biggest star isn't the sun, instead it is a blue star called 'Pistol'. Here's some information about it: ");
console.log(stars[1]);
console.log('Here are some planets and their properties:');
console.log(planets);
慕尼黑8549860
相关分类