Java面向对象(Java object oriented)

概念

面向过程让计算机有步骤地顺序做一件事,是过程化思维,使用面向过程语言开发大型项目,软件复用和维护存在很大问题,模块之间耦合严重。
面向对象相对面向过程更适合解决规模较大的问题,可以拆解问题复杂度,对现实事物进行抽象并映射为开发对象,更接近人的思维。

例如开门这个动作,面向过程是 open(Door door),动宾结构,door 作为操作对象的参数传入方法,方法内定义开门的具体步骤。
面向对象的方式首先会定义一个类 Door,抽象出门的属性(如尺寸、颜色)和行为(如 open 和 close),主谓结构。

面向过程代码松散,强调流程化解决问题。面向对象代码强调高内聚、低耦合,先抽象模型定义共性行为,再解决实际问题。

面向对象的三大特性

封装

  • 封装是对象功能内聚的表现形式,在抽象基础上决定信息是否公开及公开等级,核心问题是以什么方式暴漏哪些信息。
    主要任务是对属性、数据、敏感行为实现隐藏,对属性的访问和修改必须通过公共接口实现。
    封装使对象关系变得简单,降低了代码耦合度,方便维护。
  • 迪米特原则就是对封装的要求,即 A 模块使用 B 模块的某接口行为,对 B 模块中除此行为外的其他信息知道得应尽可能少。
    不直接对 public 属性进行读取和修改而使用 getter/setter 方法,是因为假设想在修改属性时进行权限控制、日志记录等操作,在直接访问属性的情况下无法实现。如果将 public 的属性和行为修改为 private 一般依赖模块都会报错,因此不知道使用哪种权限时应优先使用 private。

关于面向对象编程中很多人用get()和set()方法,而不用public的一点总结

关于面向对象编程中很多人用get()和set()方法,而不用public的一点总结

继承

  • 继承用来扩展一个类,子类可继承父类的部分属性和行为使模块具有复用性。继承是”is-a”关系,可使用里氏替换原则判断是否满足”is-a”关系,即任何父类出现的地方子类都可以出现。如果父类引用直接使用子类引用来代替且可以正确编译并执行,输出结果符合子类场景预期,那么说明两个类符合里氏替换原则。

多态

  • 多态以封装和继承为基础,根据运行时对象实际类型使同一行为具有不同表现形式。多态指在编译层面无法确定最终调用的方法体,在运行期由 JVM 动态绑定,调用合适的重写方法。由于重载属于静态绑定,本质上重载结果是完全不同的方法,因此多态一般专指重写。
————————

concept

Process oriented thinking is to let the computer do one thing step by step. Using process oriented language to develop large-scale projects, there are great problems in software reuse and maintenance, and the coupling between modules is serious.
Object oriented is more suitable for solving large-scale problems than process oriented. It can disassemble the complexity of the problem, abstract and map real things into development objects, which is closer to human thinking.

For example, for the action of opening the door, the process oriented is open (door), verb object structure. Door is passed into the method as the parameter of the operation object, and the specific steps of opening the door are defined in the method.
The object-oriented approach first defines a class door, abstracting the attributes (such as size and color) and behavior (such as open and close) of the door, and the subject predicate structure.

Loose process oriented code and emphasis on process to solve problems. Object oriented code emphasizes high cohesion and low coupling. First abstract the model to define common behavior, and then solve practical problems.

Three characteristics of object-oriented

encapsulation

  • Encapsulation is the expression of the cohesion of object functions. On the basis of abstraction, it determines whether the information is open and the level of disclosure. The core problem is how to disclose what information.
    The main task is to hide attributes, data and sensitive behaviors. The access and modification of attributes must be realized through public interfaces.
    Encapsulation makes the object relationship simple, reduces the code coupling and facilitates maintenance.
  • The Demeter principle is the requirement for encapsulation, that is, module a uses a certain interface behavior of module B, and should know as little as possible about other information in module B.
    Instead of directly reading and modifying the public attribute, the getter / setter method is used because it is assumed that you want to perform permission control, logging and other operations when modifying the attribute, which cannot be implemented when accessing the attribute directly. If you change the public attribute and behavior to private, the general dependent module will report an error. Therefore, when you don’t know which permission to use, you should give priority to private.

A summary that many people use get () and set () methods instead of public in object-oriented programming

A summary that many people use get () and set () methods instead of public in object-oriented programming

inherit

  • Inheritance is used to extend a class. Subclasses can inherit some properties and behaviors of the parent class, making the module reusable. Inheritance is an “is-a” relationship. You can use the Richter substitution principle to judge whether the “is-a” relationship is satisfied, that is, where any parent class appears, the child class can appear. If the parent class reference is directly replaced by the subclass reference, and can be compiled and executed correctly, and the output results meet the expectations of the subclass scenario, it indicates that the two classes comply with the Richter replacement principle.

polymorphic

  • Polymorphism is based on encapsulation and inheritance, and makes the same behavior have different manifestations according to the actual type of runtime object. Polymorphism refers to the inability to determine the final method body at the compilation level, which is dynamically bound by the JVM during the run-time and calls the appropriate rewriting method. Because overloading belongs to static binding, the result of overloading is a completely different method in essence, so polymorphism generally refers to rewriting.