zoukankan      html  css  js  c++  java
  • js面向对象编程思想

    <!DOCTYPE html>
    <html>
    	<head>
    		<meta charset="utf-8" />
    		<title></title>
    	</head>
    	<body>
    		
    	</body>
    	<script>
    		//实例对象
    		var per1={
    			name:"卡卡西",
    			age:20,
    			sex:"男",
    			eat:function(){
    				console.log("吃拉面");
    			},
    			readBook:function(){
    				console.log("西游记");
    			}
    		}
    		//调用系统的构造函数创建对象
    		var per2=new Object();
    		per2.name="大蛇丸";
    		per2.age=30;
    		per2.sex="男";
    		per2.eat=function(){
    			console.log("吃榴莲");
    		};
    		per2.play=function(){
    			console.log("玩蛇");
    		}
    		//自定义构造函数
    		function Person(name,age,sex){
    			this.name=name;
    			this.age=age;
    			this.sex=sex;
    			this.play=function(){
    				console.log("天天打游戏");
    			};
    		}
    		
    		var per=new Person("小樱",18,"女");
    		console.log(per.name);
    		per.play();
    		
    		//工程模式创建对象
    		function creatObject(name,age){
    			var obj=Object();
    			obj.name=name;
    			obj.age=age;
    			obj.syHi=function(){
    				console.log("你好");
    			};
    			return obj;
    		}
    		
    		//自定义构造函数和工程模式区别
    		/**
    		 * 共同点:都是函数,都可以创建对象,都可以传入参数
    		 * 工厂:
    		 * 函数名是小写的
    		 * 有new
    		 * 有返回值
    		 * new之后的对象是当前的对象
    		 *直接调用函数就可以创建对象
    		 * 
    		 * 自定义构造函数:
    		 * 函数名是大写的首字母
    		 * 没有new
    		 * 没有返回值
    		 * this是当前对象
    		 * 通过new的方式创建对象
    		 * **/
    		
    		function Pers(name,age){
    			this.name=name;
    			this.age=age;
    		}
    		//通过原型来添加方法,解决数据共享,节省内存空间
    		Pers.prototype.eat=function(){
    			console.log("吃凉菜");
    		}
    		var p1=new Pers("小明",20);
    		var p2=new Pers("小红",30);
    		console.log(p1.eat==p2.eat);//结果为真
    	</script>
    </html>
    

      

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8" />
    <title></title>
    </head>
    <body>

    </body>
    <script>
    //实例对象
    var per1={
    name:"卡卡西",
    age:20,
    sex:"男",
    eat:function(){
    console.log("吃拉面");
    },
    readBook:function(){
    console.log("西游记");
    }
    }
    //调用系统的构造函数创建对象
    var per2=new Object();
    per2.name="大蛇丸";
    per2.age=30;
    per2.sex="男";
    per2.eat=function(){
    console.log("吃榴莲");
    };
    per2.play=function(){
    console.log("玩蛇");
    }
    //自定义构造函数
    function Person(name,age,sex){
    this.name=name;
    this.age=age;
    this.sex=sex;
    this.play=function(){
    console.log("天天打游戏");
    };
    }

    var per=new Person("小樱",18,"女");
    console.log(per.name);
    per.play();

    //工程模式创建对象
    function creatObject(name,age){
    var obj=Object();
    obj.name=name;
    obj.age=age;
    obj.syHi=function(){
    console.log("你好");
    };
    return obj;
    }

    //自定义构造函数和工程模式区别
    /**
    * 共同点:都是函数,都可以创建对象,都可以传入参数
    * 工厂:
    * 函数名是小写的
    * 有new
    * 有返回值
    * new之后的对象是当前的对象
    *直接调用函数就可以创建对象
    *
    * 自定义构造函数:
    * 函数名是大写的首字母
    * 没有new
    * 没有返回值
    * this是当前对象
    * 通过new的方式创建对象
    * **/

    function Pers(name,age){
    this.name=name;
    this.age=age;
    }
    //通过原型来添加方法,解决数据共享,节省内存空间
    Pers.prototype.eat=function(){
    console.log("吃凉菜");
    }
    var p1=new Pers("小明",20);
    var p2=new Pers("小红",30);
    console.log(p1.eat==p2.eat);//结果为真
    </script>
    </html>

  • 相关阅读:
    最少说服人数(二分+贪心)
    线段树或树状数组或归并(逆序对)
    线段树(区间更新,区间询问,节点存最小值)
    【Hades】ades是一个开源库,基于JPA和Spring构建,通过减少开发工作量显著的改进了数据访问层的实现
    【hibernate】spring+ jpa + hibername 配置过程遇到的问题
    【方言】Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
    【Bean】 这才是bean,一直没仔细看
    【spring配置】 一组配置文件引出的问题
    org.springframework.web.servlet.view.InternalResourceViewResolver
    org.springframework.orm.jpa.JpaTransactionManager
  • 原文地址:https://www.cnblogs.com/wordblog/p/10260429.html
Copyright © 2011-2022 走看看