美文网首页
面向对象

面向对象

作者: 宁que | 来源:发表于2018-09-25 19:53 被阅读0次

面对对象

面向过程与面向对象编程

1、面向过程:所有的工作都是现写现用。

2、面向对象:是一种编程思想,许多功能事先已经编写好了,在使用时,只需要关注功能的运用,而不需要这个功能的具体实现过程。

javascript对象

将相关的变量和函数组合成一个整体,这个整体叫做对象,对象中的变量叫做属性,变量中的函数叫做方法。javascript中的对象类似字典。

创建对象的方法

1、单体

<script type="text/javascript">

var Tom = {

    name : 'tom',

    age : 18,

    showname : function(){

        alert('我的名字叫'+this.name);   

    },

    showage : function(){

        alert('我今年'+this.age+'岁');   

    }

}

</script>

2、工厂模式

<script type="text/javascript">

function Person(name,age,job){

    var o = new Object();

    o.name = name;

    o.age = age;

    o.job = job;

    o.showname = function(){

        alert('我的名字叫'+this.name);   

    };

    o.showage = function(){

        alert('我今年'+this.age+'岁');   

    };

    o.showjob = function(){

        alert('我的工作是'+this.job);   

    };

    return o;

}

var tom = Person('tom',18,'程序员');

tom.showname();

</script>

2、构造函数

<script type="text/javascript">

    function Person(name,age,job){           

        this.name = name;

        this.age = age;

        this.job = job;

        this.showname = function(){

            alert('我的名字叫'+this.name);   

        };

        this.showage = function(){

            alert('我今年'+this.age+'岁');   

        };

        this.showjob = function(){

            alert('我的工作是'+this.job);   

        };

    }

    var tom = new Person('tom',18,'程序员');

    var jack = new Person('jack',19,'销售');

    alert(tom.showjob==jack.showjob);

</script>

3、原型模式

<script type="text/javascript">

    function Person(name,age,job){       

        this.name = name;

        this.age = age;

        this.job = job;

    }

    Person.prototype.showname = function(){

        alert('我的名字叫'+this.name);   

    };

    Person.prototype.showage = function(){

        alert('我今年'+this.age+'岁');   

    };

    Person.prototype.showjob = function(){

        alert('我的工作是'+this.job);   

    };

    var tom = new Person('tom',18,'程序员');

    var jack = new Person('jack',19,'销售');

    alert(tom.showjob==jack.showjob);

</script>

4、继承

<script type="text/javascript">

        function fclass(name,age){

            this.name = name;

            this.age = age;

        }

        fclass.prototype.showname = function(){

            alert(this.name);

        }

        fclass.prototype.showage = function(){

            alert(this.age);

        }

        function sclass(name,age,job)

        {

            fclass.call(this,name,age);

            this.job = job;

        }

        sclass.prototype = new fclass();

        sclass.prototype.showjob = function(){

            alert(this.job);

        }

        var tom = new sclass('tom',19,'全栈工程师');

        tom.showname();

        tom.showage();

        tom.showjob();

    </script>


相关文章

  • PHP全栈学习笔记8

    面向对象的基本概念,面向对象编程,oop,面向对象,面向对象的分析,面向对象的设计,面向对象的编程,什么是类。 类...

  • PHP全栈学习笔记8

    面向对象的基本概念,面向对象编程,oop,面向对象,面向对象的分析,面向对象的设计,面向对象的编程,什么是类。 类...

  • 总结.Net基础知识——献给即将入坑的同行们(一期)

    什么是面向对象 面向对象OO = 面向对象的分析OOA + 面向对象的设计OOD + 面向对象的编程OOP; 通俗...

  • 面向对象基础

    面向对象编程包括: 面向对象的分析(OOA) 面向对象的设计(OOD) 面向对象的编程实现(OOP) 面向对象思想...

  • 20-OOP类与对象

    面向对象 Object Oriented 学习面向对象:XXOO 面向对象的学习: 面向过程和面向对象的区别: 面...

  • JavaScript面向对象核心知识归纳

    面向对象 概念 面向对象就是使用对象。面向对象开发就是使用对象开发。 面向过程就是用过程的方式进行开发。面向对象是...

  • 面向对象(未完成)

    面向对象 标签(空格分隔): 面向对象 第一章:面向对象(上) 什么叫面向对象 面向过程、面向对象、两者区别 构造...

  • 面向对象:创建对象&继承

    博客内容:什么是面向对象为什么要面向对象面向对象编程的特性和原则理解对象属性创建对象继承 什么是面向对象 面向对象...

  • 面向对象

    了解什么是面向对象 首先,我们学习面向对象,要了解什么是面向对象,面向对象的重要概念:类,对象。 面向对象提出的这...

  • 面向对象的三大基本特征和五大设计原则

    1、面向对象概念 1.1、理解面向对象 面向对象是相对面向过程而言; 面向对象和面向过程都是一种思想; 面向过程:...

网友评论

      本文标题:面向对象

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