zoukankan      html  css  js  c++  java
  • js面向对象1

    对于js面向对象写一个比较初级的程序 模拟一个动物园的唱歌比赛

    数据类
        /// <summary>
        /// 动物父类
        /// </summary>
        function animal()
        {
            /// <summary>
            /// 姓名
            /// </summary>
            var name;
    
    
        }
    
    function bird() {
        this.sing = function () {
            return 100;
        }
    }
    
    bird.prototype = new animal();
    
    
    function mouse() {
        this.sing = function () {
            return 50;
        }
    }
    
    mouse.prototype = new animal();
    
    
    操作类
    function singmatch() {
        this.work = function work(la) {
            var d = 0;
            var win = null;
            for ( i in la) {
                var d1 = la[i].sing();
                if (d1 > d) {
                    d = d1;
                    win = la[i];
                }
            }
            return "winner is " + win.name;
        }
    
      
    }
    
    表现类
    <script type="text/javascript" src="animal.js"></script> 
    <script type="text/javascript" src="bird.js"></script> 
    <script type="text/javascript" src="mouse.js"></script> 
    <script type="text/javascript" src="singmatch.js"></script> 
    
    <script>
        var a = new singmatch();
        var bird1 = new bird();
        bird1.name = "bird1";
    
        var mouse1 = new mouse();
        mouse1.name = "mouse1";
    
        var list = [bird1, mouse1];
    
       
        document.write( a.work(list));
    </script>
    

      

      

  • 相关阅读:
    图01--[基本概念&&图的实现]
    并查集01--[Quick Find&&Quick Union]
    排序05--[计数排序&&基数排序&&桶排序]
    排序04--[快速排序&&希尔排序]
    harukaの赛前日常
    harukaの收藏夹
    hello world&Restart the Journey
    成外集训小记
    CSPS2019游记
    博弈论初步
  • 原文地址:https://www.cnblogs.com/frog2008/p/2326184.html
Copyright © 2011-2022 走看看