设为首页 加入收藏

TOP

浅谈JavaScript的面向对象思想(三)
2017-09-19 14:21:04 】 浏览:6495
Tags:浅谈 JavaScript 面向 对象 思想
(typeof this.getName !== "function"){
        Person.prototype.getName = function () {
            return this.name;
        }
    }
    if (typeof this.toEat !== "function"){
        Person.prototype.toEat = function (animal) {
            console.log( this.getName() + "说去吃饭:");
            animal.eat();
        }
    }
 }
function Animal(name) {
    this.name = name;
    if (typeof this.getName !== "function"){
        Animal.prototype.getName = function () {
            return this.name;
        }
    }
 }
function Cat(name) {
    Animal.call(this,name);
    if (typeof this.eat !== "function"){
        Cat.prototype.eat = function () {
            console.log(this.getName() + "吃鱼");
        }
    }
 }
Cat.prototype = new Animal();
function Dog(name) {
    Animal.call(this,name);
    if (typeof this.eat !== "function"){
        Dog.prototype.eat = function () {
            console.log(this.getName() + "啃骨头");
        }
    }
 }
Dog.prototype = new Animal();


var person = new Person("Tom");
person.toEat(new Cat("cat"));//Tom说去吃饭:cat吃鱼
person.toEat(new Dog("dog"));//Tom说去吃饭:dog啃骨头


首页 上一页 1 2 3 4 5 6 下一页 尾页 3/6/6
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Java同步锁的正确使用 下一篇JSON字符串与JSON对象的相互转换

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目