编程知识 cdmana.com

Java object oriented overview


> Part of this paper is excerpted from On Java 8
## Object oriented programming refers to object-oriented programming , I have to mention another concept : abstract . The ultimate goal of programming is to solve a problem , The complexity of the problem depends directly on the type and quality of the abstraction . Early combinatorial languages used a slight abstraction of the underlying machine , To the later C Language is the abstraction of combinatorial language . Nevertheless , Their abstract principles still require us to focus on the underlying structure of the computer , Instead of the problem itself, object-oriented programming (Object-Oriented Programming OOP) It's a programming way of thinking and coding architecture . Different from the traditional process oriented programming , Object oriented programming puts problem space ( Practical problems to be solved ) The elements in and their representation in solution space are expressed in a universal way , This representation is called an object (Object). For objects that cannot be mapped in the problem space , We can also add new object types , In order to solve specific problems . All in all ,OOP Allow us to describe the problem in terms of the problem , Not based on the solution to the problem . Every object has its own state , And you can do specific operations , In other words , They all have their own characteristics and behaviors, according to the concept of object-oriented programming , Five basic characteristics can be summarized :- Everything is an object - A program is a set of objects , You can send messages to each other to tell each other what to do - Each object has its own storage space , To accommodate other objects - Every object has a type - All objects of the same class can receive the same message : An object has its own state 、 Behavior and identification , This means that the object has its own internal data ( Status )、 Method ( Act ), And distinguish them from each other ( Each object has a unique address in memory )
## Interface all objects are unique , But it is also part of the class to which objects with the same features and behaviors belong . This idea is applied to object-oriented programming , And use the basic keywords in the program class To represent a type , Each class has its own common features and behaviors . After creating a class , Many objects can be generated , These objects are treated as elements in the problem to be solved . In fact , When we're doing object-oriented programming , One of the biggest challenges is : How to establish an ideal one-to-one mapping relationship between the elements of problem space and scheme space ? If we can't establish an effective mapping , Objects can't do really useful work , There must be a way to make a request to an object , Make it solve some practical problems , Like finishing a deal 、 Turn on a switch, etc . Only a specific object can be received , The request we make to an object is defined through its interface, such as an electric light class Light, We can go to Light Requests from objects include opening on、 Shut down off, So in Light Class we need to define two methods on() and off(), Then build a Light Object of type , And call its interface method. You've found that , The object receives the request and feeds back the result , So we can think of an object as a provider of a service , In other words, your program will provide services for users , And it can also do this by calling services provided by other objects . Our ultimate goal is to develop or call some objects already in the library , Provide the ideal service to solve the problem further , We need to break down the problem , Abstract it into a set of services , And organized division of each single function 、 The close and clear role of modules , Avoid cramming too many functions into one object . This kind of programming can improve the reusability of code , At the same time, it is convenient for others to read and understand our code
## Encapsulation we divide the focus areas of programming into R & D and application . The application designer calls the R & D programmer to build the basic tool class for rapid development , R & D programmers develop a tool class , The utility class exposes only the necessary content to the application designer , And hide the details of the internal implementation , This can effectively prevent the tool class from being misused and changed . Obviously , We need some methods to ensure the correct use of tool classes , Only set access control , In order to fundamentally solve this problem, therefore , There are two reasons to use access control :1. Let application designers not touch parts they shouldn't touch 2. The builder of the class library can improve and update the tool library without affecting the use of others Java Three explicit keywords are provided to set the access permission of the class , The difference is public( Open )、private( private ) and protected( Protected ), These access modifiers determine who can use the methods they decorate 、 A variable or class 1. public Indicates that anyone can access and use the element 2. private Except for the class itself and the methods inside the class , The element cannot be accessed directly by the outside world 3. protected By protected Modified members are visible to this package and its subclasses . This sentence is a little too general , A more specific generalization should be : - Base class protected It's visible in the bag - If the subclass is not in the same package as the base class , Then the subclass instance can access the inherited from the base class protected Method , And cannot access the base class instance item protected Method 4. default If you don't use the first three , The presupposition is default Licensing rights , Resources under this permission can be accessed by members of other classes in the same package
## Reusability of reusable code and design solutions is one of the advantages of object-oriented , We can achieve reusability by reusing objects of a class , for example , Use an object of one class as a member variable of another class . therefore , The newly formed class can be made up of any number and type of other objects , Here are two concepts of composition and aggregation :- Combination (Composition) Often used to indicate ownership (has-a relationship) for example , Cars have engines . In a combinatorial relationship , The whole piece has the life cycle of the part , So when the whole thing is deleted , The parts will be deleted - polymerization (Aggregation) Represents a dynamic combination . In an aggregate relationship , The whole piece will not have the life cycle of the part , So when the whole thing is deleted , Components will not be deleted, and the combination can bring great flexibility to our program . In a new class , Member objects use private Access permission , In this way, the application designer cannot directly access it , We can do this without affecting the client code , Take time to modify those members . We can also change member objects at runtime , Thus dynamically changing the behavior of the program . The inheritance mentioned below does not have this flexibility , Because the compiler limits classes created by inheritance
## The concept of inherited objects facilitates programming , It allows us to encapsulate all kinds of data and functions together , In this way, the concept of problem space can be properly expressed , Without having to use the underlying machine language class Keywords , It can form the basic unit of programming language . Unfortunately , There are still many problems in doing so : After creating a class , Even if another new class has a similar function , You still have to recreate a new class . If we can take advantage of existing data types , Clone it , Then add and modify according to the situation , That would be a lot more convenient . Inheritance is designed for that , But inheritance is not the same as cloning . In the process of inheritance , Such as primitive class ( Base class 、 The parent class ) Something has changed , Modified clone class ( Subclass 、 Derived classes ) It also reflects this change. The base class usually has multiple derived classes , It contains all the features and behaviors that are shared between types derived from it . The latter may contain more features than the former , And can handle more information ( Or deal with them in a different way ) Use inheritance , You will build a type hierarchy , To indicate a type of problem you're trying to solve . A common example is shape , Every shape has a size 、 Color 、 Location, etc , Each shape can be drawn 、 erase 、 Mobile, etc , You can also derive other shapes , Like a circle 、 Square 、 Triangles and so on . Each shape derived can have additional features and behaviors , for example , Some shapes can be flipped , The formula of calculating shape area is different from each other, and the type hierarchy reflects the similarity and difference between shapes , You don't need to build many intermediate models between problem description and solution description . Inherits from an existing type and creates a new type , A new type contains not only all members of an existing type ( Although private members are hidden and inaccessible ), More importantly, it replicates the interface of the base class . That is to say , A message that a base class object can receive, and a derived class object can receive . If the base class doesn't meet your needs , You can add more methods to derived classes , Even change the behavior of existing base class methods ( Coverage ), Just redefine the method in the derived class
## When dealing with the hierarchy of classes , An object is usually regarded as the base class to which it belongs , Instead of treating it as a concrete class , In this way , We can write code that is not limited to a particular type . For example, the above shape example , Methods manipulate general shapes , I don't care whether it's a circle or a triangle . All shapes can be drawn 、 Erase and move . Therefore, the ability of a method to send a message to any of the objects representing the shape does not have to worry about how the object handles the information , Reduce the cost of software maintenance . If we take the derivative component type as its base class , The compiler can't know exactly which shape is drawn at compile time , That kind of car is moving , That's the point : When the program receives this message , Programmers don't care which code will be executed , The drawing method can be applied equally to every possible shape , Shapes run the appropriate code based on their specific types, so , We can add a new subclass with different execution modes without changing the method that calls it , Better for program extensions . So how does the compiler determine which part of the code to run ? In general , The compiler cannot make function calls , On non OOP Function calls generated by the compiler cause what is called early binding , This means that the compiler generates a call to a specific function name , The call is resolved to the absolute address of the code to be executed . Object oriented languages use the concept of late binding , When sending information to an object , The code being called is not determined until execution time , All the compiler has to do is make sure that the method exists , And do type checking on arguments and return values , But I don't know the exact code to run, in order to run late binding ,Java Use a special code point instead of an absolute call , This code uses the information stored in the object to calculate the address of the method body . therefore , The behavior of each object varies according to the content of a particular code point . When you send a message to an object , The object knows how to process the message . In some languages such as C++ The flexibility of the method's later binding properties must be explicitly granted , And in the Java in , Dynamic binding is the default behavior , When sending a message to an object without additional keywords to implement polymorphism , If the program does not know what the specific type it receives , But in the end, the execution is right , This is the multiformity of objects . It is a dynamic object-oriented approach to programming , The compiler and the execution time system are responsible for controlling all the details . We just need to know what to do , And how to better design programs with polymorphism
## Object creation and life cycle one of the key issues in using an object is how to create and destroy an object . Every object needs resources to survive , Especially memory . When the object is no longer in use , We should release resources in time , Cleaning up memory, however , The actual situation is often much more complicated . How do we know when to clean up these things ? Maybe an object is processed in a system , But in other systems it may not be done yet . in addition , Where is the object information ? How to control its life cycle ? stay C++ The idea adopted in the design is efficiency first , It leaves the choice of these questions to the programmer . When a programmer is writing , Determine memory footprint and survival space by placing objects on a stack or static storage area . Opposite , We also sacrifice the flexibility of the program Java Using dynamic memory allocation , Dynamically create objects in heap memory . In this way , We don't know the number of objects created until the program runs 、 Survival type and time . It may take longer to make space on heap memory than stack memory ( But not necessarily ), But the dynamic distribution theory holds that : Objects are usually complex , Compared to the overall cost of object creation , The cost of finding and freeing memory space is negligible . For the life cycle of objects , stay C++ When do you have to destroy an object in the design , and Java The garbage collection mechanism can automatically find objects that are no longer in use and release the corresponding memory space , bring Java The coding process of is compared with C++ Make it simple

版权声明
本文为[itread01]所创,转载请带上原文链接,感谢
https://cdmana.com/2020/12/20201224215001801l.html

Scroll to Top