面向对象的基本概念、基本特征、设计原则

来源:互联网 发布:网络客服岗位要求 编辑:程序博客网 时间:2024/05/22 14:58

    • 面向对象的基本概念
      • 面向对象和面向过程的区别
      • 对象类实例的区别
      • 抽象类接口的区别
        • 定义
        • 区别
        • 使用场景
    • Reference
      • OOP Vs POP 参考网址
      • 抽象类与接口区别
      • 对象与实例的区别

面向对象:面向对象一种对现实世界理解和抽象的软件开发方法——from 百度百科

面向对象编程:是一种基于对象的编程范式(英语为Programming paradigm,范式即模式、方法),它可能包含属性(knows as attribute)和方法(knows as method)。对象的一个特征就是对象的方法可以访问并且通常会修改对象的属性。在面向对象编程中,计算机程序通常是通过产生能相互作用的对象设计而成的。面向对象语言有许多值得注意的差异性,但是最流行的一种是基于类的(class-based),意味着对象是类的实例,类能决定对象的类型。—— from 维基百科.

面向对象的基本概念

面向对象和面向过程的区别

区分维度 Procedure Oriented Programming Object Oriented Programming 分割结果不同 程序分割成一个个函数 程序分割为一个个对象 重要点不同 函数、动作执行的序列比较重要,而不是数据 数据比较重要,而不是函数,因为面向对象编程是对现实世界的抽象 处理方向不同 遵循自顶向下(top down) 遵循自底向上(bottom up) 访问权限修饰符 无 有public、private、protected等 信息流动的种类不同 数据可以自由地在系统中各个函数之间移动 通过成员函数对象可以移动和相互交流 扩展性 新增数据和函数困难 新增数据和函数简单 数据的访问 因为访问权限修饰符,全局数据在系统中各个函数间共享十分简单 数据在成员函数之间不易移动,因为有访问权限修饰符控制 数据隐藏性 没有恰当方式进行数据隐藏,因为不太安全 提供数据隐藏,所以比较安全 重载(Overloading) 不支持 支持函数重载、运算符重载 复用性 较差 较好,继承即是其中代码复用的一个特性 代表性语言 C,VB,Fortran,Pascal C++,JAVA,C#

补充:
1. 当代码进行一些小改动(比如增加或减小某个需求),与OOP相比,一般POP的代码改动量较大。
2. 互动性很高的程序,不适宜用POP实现,侧面反映OOP比较通用。
3. 面向过程: 程序 = 算法 + 数据
4. 面向对象: 程序 = 对象 + 消息
5. Procedural programming commands the computer step by step via a list of instructions. It relies on subroutines or routines. A routine can have a series of computational steps. Procedural programming is sometimes referred to as imperative programming.
6. Object-oriented programming, as its name implies, relies on objects. Objects are components of a program that know how to execute certain tasks and interact with the other elements of the program.
7. OOP、POP共同点是to make programming efficient。

对象、类、实例的区别

类解释一:是一种面向对象计算机编程语言的构造,是创建对象的蓝图,描述了所创建的对象共同的属性和方法。—— from 维基百科

类解释二:In very simple terms, a class is a blueprint or a template for a specific real life object.


对象:an object is the memory block(s) used to store necessary information according this blueprint.

类实例化的结果就是对象。实例化就是一个“按照蓝图(class)创建出一个真实对象”的过程。对象被创建出来后,对象本身就包含了属性、方法等信息,所以可以说对象其实就是存储必要信息的内存块。
例子:

new Car();

说明:
a. 通过new操作,一个Car对象被创建,返回的是Car对象的引用。
b. Car对象的声明周期从调用Car构造函数开始直至被销毁。
c.销毁后被垃圾回收器所回收。


实例:Instance is a memory block that refers an object.

Car myCar = new Car();

说明:
a. Car被实例化后,会返回一个引用,这个引用就是存储在myCar。即是说myCar就是实例(instance)。


Object Vs. Instance
Instance是一个指向Object的引用。
当所有指向一个对象的引用被置空(null),Object被垃圾收集器回收掉。


抽象类、接口的区别

定义

抽象类: Abstract classes are created to capture common characteristics of subclasses. It can not be instantiated, it can be only used as super class by its subclasses. Abstract classes are used to create template for its sub classes down the hierarchy.

接口:An interface is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface. So it is kind of signing a contract, you agree that if you implement this interface, then you have to use its methods. It is just a pattern, it can not do anything itself.

区别

区分维度 抽象类 接口 默认方法实现 它可以有默认的方法实现 接口是完全抽象的,它不能有任何实现 Implementation 子类用关键字extends来继承抽象类,除非子类也是抽象类,否则子类需要实现抽象类的所有方法。 子类使用关键字implements来实现接口,并且需要实现接口的所有方法 构造器 抽象类可以有构造器 接口不能有构造函数 与普通类不同之处 抽象类与普通类几乎一样,除了不能实例化 接口与普通类完全不同 访问修饰符 抽象类方法可以有 public ,protected,private 和default 修饰符 接口方法默认是共有的 Main 方法 抽象类有main方法,我们可以运行它 接口没有main方法,所以我们不能运行它(jdk1.8可以有main方法) 多继承 抽象类只可以继承一个其他类,但是可以实现一个或者一个以上的接口 接口可以继承一个或者一个以上接口 运行速度 比接口快 接口比较慢,因为它会花时间去找类中的实现方法 添加新的方法时 当你在抽象类中添加新的方法时,只需要在方法体中加上实现的代码而不需要修改现有的代码 假如你在接口上添加新的方法, 你需要在所有实现了该接口的子类中添加新增方法的实现

使用场景

  1. 如果你有很多方法,但是只想实现其中的一部分方法,则使用抽象类。
  2. 如果你想使用多继承,则使用接口。
  3. 假如你的基本条款老是变化,则使用抽象类。因为如果使用接口,那么它的所有实现类都需要跟着变化,而使用抽象类就不需要变动。

Reference

OOP Vs POP 参考网址

【1】http://ecomputernotes.com/java/what-is-java/what-is-the-difference-between-procedural-and-object-oriented-programming-in-java
【2】http://freefeast.info/general-it-articles/difference-between-procedure-oriented-programming-and-object-oriented-programming-procedure-oriented-programming-vs-object-oriented-programming/
【3】http://www.circuitstoday.com/difference-between-procedure-oriented-and-object-oriented-programming

抽象类与接口区别

【4】https://www.javacodegeeks.com/2014/06/difference-between-abstract-class-and-interface-in-java.html

对象与实例的区别

【5】http://www.differencebetween.com/difference-between-object-and-vs-instance/

阅读全文
1 0
原创粉丝点击