美文网首页
ES6-面向对象与继承

ES6-面向对象与继承

作者: 测试探索 | 来源:发表于2022-07-09 16:40 被阅读0次

一:类

class Point{
    //构造函数
    constructor(x,y) {
        this.x = x;
        this.y = y;
    }

    toString(){
        return '点的位置:(' + this.x + ',' + this.y +  ')'
    }
}

let p = new Point(1,2);

console.log(p.toString());
image.png

二、静态方法

image.png
class MyDate{
    static validateMonth(month){
        month = Number.parseInt(month);
        return month >= 1 && month <=12;
    }
}

console.log(MyDate.validateMonth(12));
image.png

三、继承

3-1:继承的使用
image.png

class Animal {
    constructor(name,age) {
        this.name = name;
        this.age = age;
    }

    eat(){
        return "Food";

    }
    showInfo(){
        console.log("动物的信息:名称" + this.name + ",年龄" + this.age);
    }

}

//继承
class Cat extends Animal {
    constructor(name,age,color) {
        super(name,age);
        this.color = color;
    }

}

let cat = new Cat('小黑',3,'black');
cat.showInfo();

let animal = new Animal("小黄",5);
animal.showInfo();

let food = animal.eat();
console.log(food);
image.png
3-2:重写父类方法demo1

class Animal {
    constructor(name,age) {
        this.name = name;
        this.age = age;
    }

    eat(){
        return "Food";

    }
    showInfo(){
        console.log("动物的信息:名称" + this.name + ",年龄" + this.age);
    }

}

//继承
class Cat extends Animal {
    constructor(name,age,color) {
        super(name,age);
        this.color = color;
    }

//   重写showInfo方法
    showInfo(){
        console.log("毛的信息:名称" + this.name + ",年龄" + this.age + ",毛色" + this.color);
    }

}

let cat = new Cat('小黑',3,'black');
cat.showInfo();

let animal = new Animal("小黄",5);
animal.showInfo();

let food = animal.eat();
console.log(food);
image.png

demo2


class Animal {
    constructor(name,age) {
        this.name = name;
        this.age = age;
    }

    eat(){
        return "Food";

    }
    showInfo(){
        console.log("动物的信息:名称" + this.name + ",年龄" + this.age);
    }

}

//继承
class Cat extends Animal {
    constructor(name,age,color) {
        super(name,age);
        this.color = color;
    }

    eat() {
        //调用父类的方法
        let result =  super.eat();
        console.log("来自父类:",result);
        return result + "fish"

    }

//   重写showInfo方法
    showInfo(){
        console.log("毛的信息:名称" + this.name + ",年龄" + this.age + ",毛色" + this.color);
    }

}

let cat = new Cat('小黑',3,'black');
cat.showInfo();
let foods = cat.eat();
console.log(foods);

let animal = new Animal("小黄",5);
animal.showInfo();

let food = animal.eat();
console.log(food);
image.png

相关文章

  • ES6-面向对象与继承

    一:类 二、静态方法 三、继承 3-1:继承的使用 3-2:重写父类方法demo1 demo2

  • Java学习day-07:面向对象

    一、面向过程和面向对象 1.面向对象与面向过程的区别: 面向对象具有三大特征;封装,继承,多态;面向对象与面向过程...

  • JavaScript之面向对象编程

    五、面向对象编程 目录:面向对象原型继承、面向对象class继承(ES6引入的) 1.面向对象原型继承 类:模板 ...

  • python 10天快速教程 Day7

    本节重点 面向对象介绍 简单类与对象 魔法方法 继承、super 私有属性与私有方法 一、面向对象介绍 面向过程:...

  • 设计模式之美-BJ002面向对象、设计原则、设计模式、编程规范、

    1.面向对象 面向对象的四大特性:封装、抽象、继承、多态面向对象编程与面向过程编程的区别和联系面向对象分析、面向对...

  • js-继承和闭包

    继承和闭包 一、面向对象的三大特征 封装 继承 多态 二、什么是继承 继承是面向对象软件技术当中的一个概念,与多态...

  • OC中自定义初始化 与 方法分类

    面向对象的三大特征, 封装, 继承与多态!面向对象编程OOP(Object Oriented Programmin...

  • 王艳华Pythonday03

    Python的面向对象 Java 面向对象 继承

  • JS-Object 对象原型

    通过原型这种机制,JavaScript中的对象从其他对象继承功能特性,这种继承机制与经典的面向对象编程语言的继承机...

  • Python面向对象继承

    面向对象继承 面向对象编程 (OOP),英语全称:Object Oriented Programming,面向对象...

网友评论

      本文标题:ES6-面向对象与继承

      本文链接:https://www.haomeiwen.com/subject/brxibrtx.html