4.class类.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /**
  2. * ES6提供了class(类)的概念;
  3. * 通过class定义关键字,可以是定义类
  4. * class 定义类
  5. * constructor 定义构造函数初始化
  6. * extends 继承父类
  7. * super 调用父级构造函数方法
  8. * static 定义静态的方法和属性
  9. */
  10. // class定义类
  11. class Phone{
  12. // 构造函数
  13. constructor(brand,color,price){
  14. this.brand = brand;
  15. this.color = color;
  16. this.price = price;
  17. }
  18. // 定义方法
  19. call(){
  20. console.log("请给我打电话!")
  21. }
  22. }
  23. // 子类
  24. class newPhone extends Phone {
  25. constructor(brand,color,price,vase,small){
  26. super(brand,color,price);
  27. this.vase = vase;
  28. this.small = small;
  29. }
  30. photo() {
  31. console.log("拍照");
  32. }
  33. music() {
  34. console.log("音乐");
  35. }
  36. static run() {
  37. console.log("程序开始启动");
  38. }
  39. static end() {
  40. console.log("程序结束运行");
  41. }
  42. }
  43. var a1 = new Phone("我的","你的",300);
  44. var a2 = new newPhone("12","23","34","我哦","掘金");
  45. console.log(a1,'a1');
  46. console.log(a2,'a2');
  47. a2.call();
  48. newPhone.run();