JavaScript(ES2015ïŒES6ïŒ) ãProgateð¥ð¥JavaScript â £ïŒ2ç« ã¯ã©ã¹ã®ç¶æ¿ïŒã
ååãæžãå¿ããŸããããæèŠæéã¯1ïœã§ããã®ã§ãæ®ãã®ç®æšæèŠæéã¯2ïœâïž
ð¥ç¶æ¿ãšã¯
ð¥ç¶æ¿ããã¯ã©ã¹ã䜿ã
ð¥ã¡ãœããã®è¿œå
ð¥ãªãŒããŒã©ã€ãïŒ1ïŒ
ð¥ãªãŒããŒã©ã€ãïŒ2ïŒ
ãð¥ç¶æ¿ãšã¯ã
ðžæ°ããäœæããã¯ã©ã¹ãæ¢åã®ã¯ã©ã¹ã®äžçš®ã§ããå Žåã«äœ¿çšã
ãâclass Dog extends Animal { }
ããããããââ ïžextendsã¯è€æ°åœ¢ã
ãð¥ç¶æ¿ããã¯ã©ã¹ã䜿ãã
ãð¥ã¡ãœããã®è¿œå ã
ðžã¡ãœããã¯ç¬èªã«èšå®ã§ãã
ãâæ»ãå€ã䜿ãã
ãâåã¯ã©ã¹ã§å®çŸ©ããç¬èªã®ã¡ãœããã¯ã芪ã¯ã©ã¹ããåŒã³åºããšã¯åºæ¥ãªãã
class Animal {
ããconstructor(name, age) {
ããããããããããããããthis.name = name;
ããããããããããããããthis.age = age;
ãããããããããããããã}
ããgreet() {
ããããããconsole.log("ããã«ã¡ã¯");
ãããããã}
ããinfo() {
ããããããthis.greet();
ããããããconsole.log(`ååã¯${this.name}ã§ã`);
ããããããconsole.log(`${this.age}æ³ã§ã`);
ããããã}
}
class Dog extends Animal {
// getHumanAgeã¡ãœãããè¿œå ããŠãã ãã
ããgetHumanAge(){
ããããããããããreturn this.age*7;
âð€å
ããŒã¿ã衚瀺ããåŸã«ããã®å
ããŒã¿ãå
ã«ããç°ãªãããŒã¿ãåºåãããæã«äœ¿çšããïŒïŒãšããã€ã¡ãŒãžãæã£ãð€
ãããããããããã}
}
// å®æ°dogã«Dogã¯ã©ã¹ã®ã€ã³ã¹ã¿ã³ã¹ã代å
¥ããŠãã ãã
const dog = new Dog("ã¬ãª", 4 );
// dogã«å¯ŸããŠinfoã¡ãœãããåŒã³åºããŠãã ãã
dog.info( ã);
// å®æ°humanAgeãå®çŸ©ããå®æ°dogã«å¯ŸããŠgetHumanAgeã¡ãœãããåŒã³åºããå€ã代å
¥ããŠãã ãã
const humanAge = dog . getHumanAge ( ) ;ãââïžæ°ããconsole.logã衚瀺ããããã®æºå
// ã人é幎霢ã§ããæ³ã§ãããšåºåããŠãã ãã
console.log(`人é幎霢ã§${humanAge}ã§ã`);
ãð¥ãªãŒããŒã©ã€ãã
ðžèŠªã¯ã©ã¹ãšã芪ã¯ã©ã¹ãç¶æ¿ããåã¯ã©ã¹ã®åãååã®ã¡ãœããã«ã€ããŠ
ãâåã¯ã©ã¹ã®ã¡ãœãããåªå
ããã
ããïŒåã¯ã©ã¹ã®ã¡ãœããã芪ã¯ã©ã¹ã®ã¡ãœãããäžæžãããŠããïŒoverrideïŒ
ð€ãclass Dog extends Animal ãå
ã®info(){}ã¯ãè¿œå ããåã ãæžãã¡ããã¡ãªã®ããªïŒæ£ããèªèãããªãã®ããªïŒïŒ
ãâãã¡ã§ããã
ããè«ŠããŠãåŠãçé¢ç®ã«ãã¡ããšæžããŸãããâŠ
ðžconstructorããªãŒããŒã©ã€ããã
ãâsuper( );
â ïžè¡šç€ºã¯ãããŠããã©ãçµ±äžæããªãâ
ããthis.breed = "ãã¯ã¯";
ããâãã
ããthis.breed = breed;ãã
ããâã«ããåŸã
ããåŒæ°ãšããŠ
ããconst dog = new Dog("ã¬ãª" , 4 , "ãã¯ã¯" );
ããdog.info();
ãã
ããŸãšãã
JavaScript â
£ã¯åèš2h30ïœãããã®ãŠã§ã€ãã§ããã
ãŸã 1人ã§ã¯äœããªãã§ãããã¬ãã«ãªå²ã«ã¯ãJavaããã¹ã²ãŒïŒãã£ãŠæãã§æ¥œããã§åºæ¥ãŠãŸãÙ©( 'Ï' )Ùãã
â
¥ãŸã§é²ãã ãããŸã埩ç¿ã«æ»ã£ãŠãã£ãããïŒåŸ
ã£ãŠããããŒãâŠ
ïŒ(ï¿£ãŒï¿£)ïŒ/~~~
ãã®èšäºãæ°ã«å ¥ã£ãããµããŒããããŠã¿ãŸãããïŒ