Java Basics - object-oriented features

1. Three characteristics

  ①, the package

  The so-called package, is the objective things packaged into an abstract class, the class data and methods allow only trusted class or object manipulation, or object class untrusted information hiding. Packaging is one of the features of object-oriented concepts and the main properties of the object classes. Briefly, a class is a logical entity that encapsulates data and operation code data. In an internal object, some code or some data may be private and can not be accessed outside world. In this manner, the internal data objects provide different levels of protection to prevent accidental changing part of the program irrelevant or incorrect use of the private portion of the object.

  ②, inheritance

  The so-called inheritance, is one that can make objects of a certain type of acquisition properties and methods of another type. It supports the concept of by-level classification. Inheritance refers to such a capability: it can use all the features of an existing class, and without having to rewrite these functions extend the case of the original class. Create a new class by inheriting called "sub-class" or "derived class", inherited class called "base class", "parent" or "superclass." Succession process, that is, from the general to the particular process. Inheritance implementation in two ways: one is inherited from the parent class, without the need for additional coding directly use the properties and methods of the base class; second is to implement an interface, use only the name of the properties and methods of the base class, subclass must provide an implementation.

  ③, polymorphic

  The so-called polymorphism, refer to the same example of a method of a class has different forms in different situations. Polymorphism is a mechanism having a different internal structure of the object can share the same external interface. This means that, although different specific actions for different objects, but through a common class, they can be invoked in the same way.

2. The five basic principles

  There are five basic object-oriented principles, namely the principle of single responsibility, open closed principle, alternative principle, and the principle of separation principle dependent interfaces.

  ①, Single Responsibility Principle (Single Responsibility Principle, SRP)

  It refers to a class of functions to be single, not all-inclusive. As a people, the distribution of work can not be too much, otherwise it is busy all day long, though, efficiency is not high up.

  ②, Open Closed Principle (Open-Close Principle, OCP)

  Refers to a module in scalability should be open, and you can change in terms is closed. For example, a network module had only server function, and now want to join the client function, then it should be in the code without modifying the server function of the premise, it is possible to increase the code that implements the client function, which requires the design in the beginning, service and client should be separated from the public part of the abstracts.

  ③, substitution principle

  Refers to a subclass of the class should be able to replace the parent and the parent class appear anywhere that can appear. For example, a company engaged in the annual party, all employees can participate in the drawing, then regardless of older employees or new employees, regardless of headquarters employees or expatriates, all should be able to participate in the drawing, otherwise the company would discord.

  ④, dependent principle (the Dependency Inversion Principle, DIP)

  Means that a particular abstract dependent, dependent on the lower layer an upper layer. Assuming that the module B is lower than the module of the module A, module B but need to use the function module A, which is a concrete class module B should not be used directly in module A, module B and should have an abstract interface defined by the module a abstract interface to achieve this, so that to achieve the object of the dependent inverted module B also lifted dependence of module a, module a is in turn dependent on the B module defines an abstract interface. Upper module unavoidable rely on the underlying module, but if the module B is also directly dependent on the implementation module A, it may result in a circular dependency. A common problem is the need to include when compiling the module A module B directly to the Java file, and when compiling module B also have to include Java files directly to the module A.

  ⑤, the principle of separation of the interface (the Interface Segregation Principle, ISP)

  Refers to the abstract interface between the modules through isolated, rather than through a specific class strongly coupled together.

  

Guess you like

Origin www.cnblogs.com/JackWeTa/p/11470202.html