www.2527.com_澳门新葡8455手机版_新京葡娱乐场网址_
做最好的网站

原型方式

2019-05-02 05:39 来源:未知

图片 1图片 2

1.angular.bind()

图片 3

  1 /*2048*/
  2 
  3 #include<stdio.h>
  4 #include<stdlib.h>
  5 #include<conio.h>
  6 #include<time.h>
  7 
  8 void print(void);/*显示游戏界面*/
  9 int add(void);/*对中间变量数组进行处理*/
 10 
 11 int code[4][4] =
 12    {
 13     {0,0,0,0},
 14     {0,0,0,0},
 15     {0,0,0,0},
 16     {0,0,0,0},
 17    };/*游戏中的16个格子*/
 18 int temp[5];/*中间变量*/
 19 int move = 0;/*移动次数*/
 20 int score = 0;/*分数*/
 21 
 22 
 23 
 24 int main(void)
 25 {
 26      int gameover = 0;/*判断游戏是否结束,1结束,0继续*/
 27      int i,j;
 28      int change = 1;/*判断格子中的数是否改变,0不变*/
 29      char input;
 30      srand((unsigned)time(NULL));/*设置随机数的起点*/
 31      while(gameover == 0)
 32          {
 33               if(change >= 1)/*仅当数发生改变时添加新数*/
 34                {
 35                     do
 36                      {
 37                           i=((unsigned)rand()) % 4;
 38                           j=((unsigned)rand()) % 4;
 39                      }while(code[i][j] != 0);
 40                     if(((unsigned)rand()) % 4 == 0)
 41                      {
 42                           code[i][j]=4;
 43                      }
 44                     else
 45                      {
 46                           code[i][j]=2;/*随机选一个空格填上2或4*/
 47                      }
 48                     move  ;/*增加次数*/
 49                }
 50               print();/*显示*/
 51               input = getch();/*输入方向*/
 52               change = 0;
 53               switch(input)
 54                    {
 55                         case '0':/*退出*/
 56                                  printf("Are you sure to exit?(y/n)");
 57                                  input=getchar();
 58                                  if(input=='y'||input=='Y')
 59                                     exit(0);
 60                                  break;
 61 
 62                         case 'W':
 63 
 64                         case 'w':/*上*/
 65                                  for(j=0;j<=3;j  )
 66                                    {
 67                                         for(i=0;i<=3;i  )
 68                                           {
 69                                                temp[i]=code[i][j];/*把一列数移到中间变量*/
 70                                           }
 71                                         temp[4]=0;
 72                                         change  = add();
 73                                         for(i=0;i<=3;i  )
 74                                           {
 75                                                code[i][j]=temp[i];/*把处理好的中间变量移回来*/
 76                                           }
 77                                     }
 78                                   break;
 79 
 80                         case 'A':
 81 
 82                         case 'a':/*左*/
 83                                  for(i=0;i<=3;i  )
 84                                    {
 85                                         for(j=0;j<=3;j  )
 86                                           {
 87                                                temp[j]=code[i][j];/*把一行数移到中间变量*/
 88                                           }
 89                                         temp[4]=0;
 90                                         change  = add();
 91                                         for(j=0;j<=3;j  )
 92                                           {
 93                                                code[i][j]=temp[j];/*把处理好的中间变量移回来*/
 94                                           }
 95                                    }
 96                                  break;
 97 
 98                         case 'S':
 99 
100                         case 's':/*下*/
101                                  for(j=0;j<=3;j  )
102                                    {
103                                         for(i=0;i<=3;i  )
104                                           {
105                                                temp[i]=code[3-i][j];/*把一列数移到中间变量*/
106                                           }
107                                         temp[4]=0;
108                                         change  = add();
109                                         for(i=0;i<=3;i  )
110                                           {
111                                                code[3-i][j]=temp[i];/*把处理好的中间变量移回来*/
112                                           }
113                                    }
114                                  break;
115 
116                         case 'D':
117 
118                         case 'd':/*右*/
119                                  for(i=0;i<=3;i  )
120                                    {
121                                         for(j=0;j<=3;j  )
122                                           {
123                                                temp[j]=code[i][3-j];/*把一行数移到中间变量*/
124                                           }
125                                         temp[4]=0;
126                                         change  = add();
127                                         for(j=0;j<=3;j  )
128                                           {
129                                                code[i][3-j]=temp[j];/*把处理好的中间变量移回来*/
130                                           }
131                                    }
132                                  break;
133                    }
134               gameover=1;
135               for(i=0;i<=3;i  )
136               for(j=0;j<=3;j  )
137               if(code[i][j]==0)
138               gameover=0;/*所有格子都填满则游戏结束*/
139         }
140     printf("Game over!n");
141     getch();
142 
143     return 0;
144 }
145 
146 void print(void)/*显示游戏界面*/
147 {
148     int i,j;
149     system("CLS");/*清屏*/
150     printf("2048n");
151     printf("W--UP A--LEFT S--DOWN D--RIGHT 0--EXITn");
152     printf("Score:%d Move:%dn",score,move);
153     printf("Made by lanshanxiaon");
154     printf("|-----------------------|n");/*显示横向分隔线*/
155     for(i=0;i<=3;i  )
156        {
157            for(j=0;j<=3;j  )
158               {
159                    if(code[i][j]==0)
160                      {
161                           printf("|     ");/*0显示空格*/
162                      }
163                    else
164                      {
165                           printf("|]",code[i][j]);/*显示数字和分隔线*/
166                      }
167               }
168            printf("|n|-----------------------|n");/*显示横向分隔线*/
169        }
170 }
171 
172 int add(void)/*对中间变量数组进行处理*/
173 {
174     int i;
175     int t=0;
176     int change=0;/*判断数组是否有改变,0不变,1变化*/
177     do
178      {
179           for(i=0;i<=3;i  )
180             {
181                  if(temp[i]==0)
182                   {
183                        if(temp[i]!=temp[i 1])
184                          change=1;/*当一个0后面不是0时数组改变*/
185                        temp[i]=temp[i 1];
186                        temp[i 1]=0;
187                   }
188             }/*去掉中间的0*/
189           t  ;
190      }while(t<=3);/*重复多次*/
191     for(i=1;i<=3;i  )
192         {
193              if(temp[i]==temp[i-1])
194               {
195                    if(temp[i]!=0)
196                      {
197                           change=1;/*当两个非零相同的数相加时数组改变*/
198                           score=score temp[i];/*加分*/
199                      }
200                    temp[i-1]=temp[i-1]*2;
201                    temp[i]=0;
202               }
203         }/*把两个相邻的相同的数加起来*/
204 
205     return change;
206 }

图片 4图片 5

原型模式就是从一个对象在创建另外一个对象,不需要知道任何创建的细节:(克隆)

View Code

<!DOCTYPE HTML>
<html ng-app>
<head>
</head>
<script src="http://www.god137.com/uploads/allimg/190502/05393931P-5.jpg"></script>
</body>
<script>
  /*  angluar.bind(obj,fn,[args]) 把对象绑定到函数里,生成新的函数  */
    var obj={name:'xiaoming'};
    var fn= function (arg) {
        console.log(this.name ' is ' arg);  // this 指 obj
    }
    var f1=angular.bind(obj,fn,'so handsome'); // 参数可选
    f1();
    var f2=angular.bind(obj,fn);          // 可在最后才传参数
    f2('weak!');
</script>
</body>
</html>

以创建简历为例:

2048小游戏代码在上面的View Code中,将加号展开就可以看到。

View Code

简历类(Resume) :继承于系统的克隆接口(ICloneable)

点击源码上的复制按键还可以直接将源代码复制下来。

 2.angular.bootstrap()

图片 6图片 7

 

图片 8图片 9

namespace Ptototype_Pattern
{
    class ReSume:ICloneable
    {
        private string name;
        private string age;
        private string sex;
        private string birthday;
        private string time;
        private string company;
        public ReSume(string name)
        {
            this.name = name;
        }

        /// <summary>
        /// 设置个人信息
        /// </summary>
        /// <param name="age"></param>
        /// <param name="sex"></param>
        /// <param name="birthday"></param>
        public void SetPersonInfo(string age, string sex, string birthday)
        {
            this.age = age;
            this.sex = sex;
            this.birthday = birthday;
        }

        /// <summary>
        /// 设置工作经历
        /// </summary>
        /// <param name="time"></param>
        /// <param name="company"></param>
        public void SetWorkExperice(string time, string company)
        {
            this.time = time;
            this.company = company;
        }

        public void Display()
        {
            Console.WriteLine("姓名:{0}  年龄:{1} 出生日期:{2}", name, age, birthday);
            Console.WriteLine("在 {0} 工作于 {1} n",time,company);
        }

        public Object Clone()
        {
            return (Object)this.MemberwiseClone();
        }
    }
}

 

<!DOCTYPE HTML>
<meta charset="utf-8">
<head>
</head>
<script src="http://www.god137.com/uploads/allimg/190502/05393931P-5.jpg"></script>
</body>
<script>
    //   angular.bootstrap(); 手动加载模块,   模仿 ng-app自动加载模块    注意不能出现  ng-app
    //创建模块1
  var module1=angular.module('module1',[]);
    module1.controller('controller1', function ($scope) {
        $scope.name='haha';
    })
    //创建模块2
  var module2=angular.module('module2',[]);
  module2.controller('controller2', function ($scope) {
      $scope.name='hehe';
  })
    // 页面加载完成后再加载模块
    angular.element(document).ready(function () {
        angular.bootstrap(document.getElementById("div1"),["module1"]);    //  div1加载 模块1
        angular.bootstrap(document.getElementById("div2"),["module2"]);
    })
</script>
<div id="div1" ng-controller="controller1">div1: {{name}}</div>   <!--  这里要有控制器 -->
<div id="div2" ng-controller="controller2">div2: {{name}}</div>
</body>
</meta>

View Code

 

View Code

客户端类(TestMain)

 

 3.angular.element()  应该用 directive

图片 10图片 11

 

图片 12图片 13

namespace Ptototype_Pattern
{
    class TestMain
    {
        static void Main(string[] args)
        {
            ReSume A = new ReSume("张三");
            A.SetPersonInfo("25","男","1988-10-10");
            A.SetWorkExperice("2010--2013", "X公司");

            ReSume B = (ReSume)A.Clone();
            B.SetWorkExperice("2009--2010", "Y公司");

            ReSume C = (ReSume)A.Clone();
            C.SetWorkExperice("2006--2009", "Z公司");

            A.Display();
            B.Display();
            C.Display();

            Console.ReadLine();

        }
    }
}

 

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <script src="http://www.god137.com/uploads/allimg/190502/05393931P-5.jpg"></script>
    <style>
        .a{color:red;}
    </style>
</head>
<body>
    <p>12345</p>
    <script>
        var p1=document.getElementsByTagName("p")[0];
        angular.element(p1).addClass('a');
    </script>
</body>
</html>

View Code

 

View Code

图片 14

 

 4. JSON.stringify()  // 对象转成string

 

 

图片 15图片 16

深复制和浅复制:MemberwiseClone 方法创建一个浅表副本,方法是创建一个新对象,然后将当前对象的非静态字段复制到该新对象。 如果字段是值类型的,则对该字段执行逐位复制。 如果字段是引用类型,则复制引用但不复制引用的对象;因此,原始对象及其复本引用同一对象。

 

  <script>
        var a={a:1,b:2};
        console.log("xx: " typeof a " :" a);
        var b=JSON.stringify(a);    // 转成string 类型
        console.log("x2x: " typeof b " :" b);
    </script>

引用类型的例子如下:

View Code

新添加工作经历类(WorkExperice)

 5.angular.extend(obj2,obj1)  //  把obj1的属性复制给obj2并把结果赋值给obj2  

 

图片 17图片 18

图片 19图片 20

<script src="http://www.god137.com/uploads/allimg/190502/05393931P-5.jpg"></script>
    <script>
        var obj1={name:'tom'};
        var obj2={sex:'female'};
        var obj_new=angular.extend(obj2,obj1);    // 返回值赋给obj2
        console.log("obj1: " JSON.stringify(obj1));
        console.log("obj2: " JSON.stringify(obj2));
        console.log("obj_new: " JSON.stringify(obj_new));
    </script>
namespace Ptototype_Pattern
{
    class WorkExperice
    {
        private string workTime;
        private string workCompany;
        /// <summary>
        /// 工作时间
        /// </summary>
        public string WorkTime
        {
            get { return workTime; }
            set { workTime = value; }
        }

        /// <summary>
        /// 工作企业
        /// </summary>
        public string WorkCompany
        {
            get { return workCompany; }
            set { workCompany = value; }
        }
    }
}

View Code

View Code

 6. webStorm 智能提示jq angularjs语法

 

图片 21

简历类(Resume) :继承于系统的克隆接口(ICloneable)

 7.声明 controllers 的推荐的方法是使用数组表示法

图片 22图片 23

图片 24

namespace Ptototype_Pattern
{
    class ReSume:ICloneable
    {
        private string name;
        private string age;
        private string sex;
        private string birthday;
        //private string time;
        //private string company;
        private WorkExperice work;
        public ReSume(string name)
        {
            this.name = name;
            work = new WorkExperice();

        }

        /// <summary>
        /// 设置个人信息
        /// </summary>
        /// <param name="age"></param>
        /// <param name="sex"></param>
        /// <param name="birthday"></param>
        public void SetPersonInfo(string age, string sex, string birthday)
        {
            this.age = age;
            this.sex = sex;
            this.birthday = birthday;
        }

        /// <summary>
        /// 设置工作经历
        /// </summary>
        /// <param name="time"></param>
        /// <param name="company"></param>
        public void SetWorkExperice(string time, string company)
        {
            //this.time = time;
            //this.company = company;
            work.WorkTime = time;
            work.WorkCompany = company;

        }

        public void Display()
        {
            //Console.WriteLine("姓名:{0}  年龄:{1} 出生日期:{2}", name, age, birthday);
            //Console.WriteLine("在 {0} 工作于 {1} n",time,company);

            Console.WriteLine("姓名:{0}  年龄:{1} 出生日期:{2}", name, age, birthday);
            Console.WriteLine("在 {0} 工作于 {1} n", work.WorkTime, work.WorkCompany);

        }

        public Object Clone()
        {
            return (Object)this.MemberwiseClone();
        }
    }
}

8.angularjs.forEach()  angularjs.fromJson()

View Code

图片 25图片 26

客户端不变:(全为Z公司)

<script src="http://www.god137.com/uploads/allimg/190502/05393931P-5.jpg"></script>
    <script>
        /* 1. angularjs.forEach();  */
        var objs =[{a:1},{a:2}];
        angular.forEach(objs, function(data,index,array){
            //data等价于array[index]
            console.log(data.a '=' array[index].a);
            console.log("xx: " data.a);
        });
        /*  2.  angular.fromJson()  */
        var json='{"name":"haha","password":"321"}';
        var jsonStr=angular.fromJson(json);     //  把json转成对象
        console.log("jsonStr: " typeof jsonStr);
        console.log("jsonStr name: " jsonStr.name);
    </script>

图片 27

View Code

MemberwiseClone 方法创建一个浅表副本,对于引用类型复制引用,但不复制引用的对象

 9. ng-class

改为深复制如下:

图片 28图片 29

工作经历类(WorkExperice)

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style>
        .testCol{color:blue;}
        .testSize{font-size: 30px;}
        .testBorder{border: 1px solid red;}
        .red{color:red;}
    </style>
</head>

<body>
    <div ng-app="app" ng-controller="myCtrl">

    <p ng-class="{testCol:color,testSize:size,testBorder:border}">{{name}}</p>
                    <!--  属性是样式,值是true or false -->
        <p ng-class="{strike: deleted, bold: important, red: error}">Map Syntax Example</p>
        <input type="checkbox" ng-model="deleted"> deleted (apply "strike" class)<br>
        <input type="checkbox" ng-model="important"> important (apply "bold" class)<br>
        <input type="checkbox" ng-model="error"> error (apply "red" class)
        <hr>


        </div>
        <script src="http://www.god137.com/uploads/allimg/190502/05393931P-5.jpg"></script>
        <script>
            angular.module("app",[])
                .controller("myCtrl",["$scope", function ($scope) {
                     $scope.name='haha';
                        $scope.color=true;     /*  true 则应用该样式*/
                        $scope.size=false;
                        $scope.border=true;
                }])
        </script>
</body>
</html>

 

View Code

图片 30图片 31

 10.ng-copy 复制  ng-paste粘贴  ng-cut 剪切

namespace Ptototype_Pattern
{
    class WorkExperice:ICloneable
    {
        private string workTime;
        private string workCompany;
        /// <summary>
        /// 工作时间
        /// </summary>
        public string WorkTime
        {
            get { return workTime; }
            set { workTime = value; }
        }

        /// <summary>
        /// 工作企业
        /// </summary>
        public string WorkCompany
        {
            get { return workCompany; }
            set { workCompany = value; }
        }

        public Object Clone()
        {
            return (object)this.MemberwiseClone();
        }
    }
}

图片 32图片 33

View Code

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <script src="http://www.god137.com/uploads/allimg/190502/05393931P-5.jpg"></script>

</head>
<body>
<div ng-app="app" ng-controller="myCtrl">
        <div ng-bind="name"></div>
    <input type="text" ng-model="name" ng-copy="copy()"/>
    </div>
    <script>
        /*触发条件:复制。鼠标右键复制和快捷键Ctrl C都会触发。*/  
        angular.module("app",[])
            .controller("myCtrl",["$scope", function ($scope) {
                 $scope.name='haha';
                    $scope.copy= function () {
                        alert(1);
                    }
            }])

    </script>
</body>
</html>

 

View Code

简历类(Resume) :继承于系统的克隆接口(ICloneable)

 11.ng-list

图片 34图片 35

图片 36图片 37

namespace Ptototype_Pattern
{
    class ReSume:ICloneable
    {
        private string name;
        private string age;
        private string sex;
        private string birthday;
        //private string time;
        //private string company;
        private WorkExperice work;
        public ReSume(string name)
        {
            this.name = name;
            work = new WorkExperice();

        }
        public ReSume(WorkExperice work)
        {
            this.work = (WorkExperice)work.Clone();
        }

        /// <summary>
        /// 设置个人信息
        /// </summary>
        /// <param name="age"></param>
        /// <param name="sex"></param>
        /// <param name="birthday"></param>
        public void SetPersonInfo(string age, string sex, string birthday)
        {
            this.age = age;
            this.sex = sex;
            this.birthday = birthday;
        }

        /// <summary>
        /// 设置工作经历
        /// </summary>
        /// <param name="time"></param>
        /// <param name="company"></param>
        public void SetWorkExperice(string time, string company)
        {
            //this.time = time;
            //this.company = company;
            work.WorkTime = time;
            work.WorkCompany = company;

        }

        public void Display()
        {
            //Console.WriteLine("姓名:{0}  年龄:{1} 出生日期:{2}", name, age, birthday);
            //Console.WriteLine("在 {0} 工作于 {1} n",time,company);

            Console.WriteLine("姓名:{0}  年龄:{1} 出生日期:{2}", name, age, birthday);
            Console.WriteLine("在 {0} 工作于 {1} n", work.WorkTime, work.WorkCompany);

        }

        public Object Clone()
        {
            //return (Object)this.MemberwiseClone();
            ReSume resum = new ReSume(this.work);
            resum.name = this.name;
            resum.age = this.age;
            resum.birthday = this.birthday;
            return resum;
        }
    }
}
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <script src="http://www.god137.com/uploads/allimg/190502/05393931P-5.jpg"></script>
</head>
<body>
<div ng-app="app" ng-controller="myCtrl">
    <p>ng-list自定义输入数组   a.b.c.d </p>
    <input type="text" ng-model="lists" ng-list="."/><br/>
    {{lists}}
    <li ng-repeat="list in lists">{{list}}</li>
    </div>
    <script>
        angular.module("app",[])
            .controller("myCtrl",["$scope", function ($scope) {
                $scope.lists=['a','b','c'];
            }])
    </script>
</body>
</html>

View Code

View Code

客户端不变:

 12. angular 获得input的值

图片 38

图片 39图片 40

 

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
  <!--<script src="http://apps.bdimg.com/libs/angular.js/1.5.3/angular.min.js"></script>-->
  <script src="//cdn.bootcss.com/angular.js/1.5.3/angular.min.js"></script>
</head>
<body>
<div ng-app="app" ng-controller="myCtrl">
<input type="text" ng-model="orderNum"/>
  <button ng-click="check()">btn</button>
</div>
<script>
  angular.module("app",[])
    .controller("myCtrl", function ($scope) {
      $scope.check= function () {
         alert($scope.orderNum)
      }
    })
</script>
</body>
</html>

 

View Code

 

 13.angular factory service

 

图片 41图片 42

 

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
  <script src="http://www.god137.com/uploads/allimg/190502/05393931P-5.jpg"></script>
</head>
<body>
<div ng-app="appTest" ng-controller="MyController">
<ul>
  <li ng-repeat="test in tests">
    id: {{test.id}} 名字: {{test.name}}
    信息: {{test.lastText}}
    <button ng-click="remove(test)">delete</button>
  </li>
</ul>
</div>
<script>
  var appTest = angular.module("appTest",[]);
  appTest.factory("myFactory", function () {
      var f={};
      f.remove= function (arr,item) {
        return arr.splice(arr.indexOf(item),1);
      }
      return f;
    });
  appTest.service("myService", function (myFactory) {
      this.remove=function(arr,item){
        return myFactory.remove(arr,item);
      }
    });
  appTest.controller("MyController", function ($scope,$http,myService) {
      $http.get("test.json").success(function (data) {
        $scope.tests=data;
      });
      //    把factory service 删除,直接在控制器中可以实现删除效果
      /* $scope.remove= function (test) {
       $scope.tests.splice($scope.tests.indexOf(test),1);
       }*/
      $scope.remove= function (test) {
         myService.remove($scope.tests,test);
      }
    });
</script>

</body>
</html>

 

View Code

14.angular 指令(1)

图片 43图片 44

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body ng-app="app">
  <hello-word>12345</hello-word>
  <hello-word></hello-word>
  <script src="http://www.god137.com/uploads/allimg/190502/05393931P-5.jpg"></script>
  <script>
    angular.module("app",[])
      .directive('helloWord', function () {
        return{
          restrict:'E',    //  E 元素  A 属性
          template:'<div>hi, how are yousss<br/></div>',
          replace:true,    /* 隐藏 <hello-word>标签 */
          transclude:true     /* 保留 标签里面的内容: 12345 */
        }
      })
  </script>
</body>
</html>

View Code

15.anguar 指令 link

图片 45图片 46

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
  <style>
    .expander {
      border: 1px solid black;
      width: 250px;
    }

    .expander>.title {
      background-color: black;
      color: white;
      padding: .1em .3em;
      cursor: pointer;
    }

    .expander>.body {
      padding: .1em .3em;
    }
  </style>
</head>
<body ng-app="expanderModule">
<div ng-controller='SomeController'>
  <expander class='expander' expander-title='title'>
    {{text}}
  </expander>

</div>
  <script src="http://www.god137.com/uploads/allimg/190502/05393931P-5.jpg"></script>
  <script>
    var expanderModule=angular.module('expanderModule', [])
    expanderModule.directive('expander', function() {
      return {
        restrict : 'EA',
        replace : true,
        transclude : true,
        scope : {
          title : '=expanderTitle'
        },
        template : '<div>'
          '<div class="title" ng-click="toggle()">{{title}}</div>'
          '<div class="body" ng-show="showMe" ng-transclude></div>'
          '</div>',
        link : function(scope, element, attrs) {
          scope.showMe = false;
          scope.toggle = function toggle() {
            scope.showMe = !scope.showMe;
          }
        }
      }
    });
    expanderModule.controller('SomeController',function($scope) {
      $scope.title = '点击展开';
      $scope.text = '这里是内部的内容。';
    });
  </script>
</body>
</html>

View Code

 

TAG标签:
版权声明:本文由澳门新葡8455手机版发布于www.2527.com,转载请注明出处:原型方式