Early programming languages, such as Fortran and C, were basically process-oriented. Their main idea of programming was to focus on algorithm implementation. For example, the following is a process-oriented program for maximum positive integer values:
Int maxsofar = 0, price = 1; // The initial value of maxsofar is 0, and price is the input value.
While (price> 0) {// enter the price value cyclically
If (price> maxsofar) // The input value price is greater than the maximum value maxsofar
Maxsofar = price; // The value of maxsofar is the value of price.
String input = joptionpane. showinputdialog ("enter the next price"); // continue to enter the price
Price = double. parsedouble (input); // converts string input to an integer price
}
System. Out. println ("the maximum is" + maxsofar); // print the maximum value of maxsofar.
}
This section mainly implements the algorithm for maximum value. However, if you want to use object-oriented programming, you can use another method:
Max max = new max (); // Max is an object of the Max class.
While (price> 0 ){
Max. updatemax (price); // The object Max calls the updatemax method to update the maximum value.
Price = max. getprice (); // The object Max calls getprice to obtain the value of the next price
}
System. Out. println ("the maximum is" + Max. getmax (); // The object Max calls the getmax method to obtain the maximum value and prints it out.
Object-oriented
The pure object-oriented programming method is as follows:
1. Everything is an object. You can think of an object as a new type of variable. It stores data and can operate on its own data. For example, the maximum value of data is kept in the class max, and the method updatemax generates the latest maximum value based on the newly added price value, and the maximum value returned by the getmax method.
2. A program is a combination of many objects. Through message transmission, each object knows what to do. If you want the object to do something, you must "send a message" to the object ". Specifically, you can think of a message as a call request, which calls a method from the target object. For example, the above object-oriented program segment should belong to a certain class, for example, it belongs to the class shopping, then shopping contains the object Max of the class Max, calling the updatemax method is equivalent to sending an "updatemax" command to the max object of the shopping object, requiring the object Max to recalculate the maximum value.
3. Each object has its own storage space. It can hold other objects, or create new objects by encapsulating existing objects. Therefore, although the object concept is very simple, but after encapsulation, it can achieve a high degree of complexity in the program.
4. Each object belongs to a class. According to the syntax, each object is an "instance" of a "class ". The most important feature of a class is "What messages can be sent to it? ", Which is the operation of the class itself. For example, Max is an instance of Max type.