Java is forced to write the Class keyword language, there can be no independent function in the class out of the file, which is different from Python C + + PHP, the following can be separate in the class outside the write function, so Java is known as a purely object-oriented language, both PY and PHP are not forced to the object's wording.
Nevertheless, some stupid melon, or killed also write class, and strongly oppose to write class, that the process has been able to deal with everything, the invention of C + + This language is idle, C + + for C is completely redundant unnecessary.
But when asked by the company to write Java, will be blinded, feel no object-oriented experience, it is difficult to change, this article demonstrates a complete process-oriented thinking to write Java. Let the people from the object-oriented py can write Java without object-oriented.
The first is two abbreviations. Oop for object-oriented programming, POP for process programming
Let's start with an object-oriented Java general notation, and then a complete objection to object-oriented notation.
Personoop.java file.
Importjava.util.logging.*;/*** One using object-oriented writing human *@authorBfzs*/ Public classPersonoop {PrivateString name; Private intAge ; Private FinalString Eyecolor; Private floatweight; Public floatheight; Private StaticLogger Logger = Logger.getlogger (personoop.class. toString ()); PublicPersonoop (String personname,intAge, String Eyecolor,floatWeightfloatheight) {Name=PersonName; This. Age =Age ; This. Eyecolor =Eyecolor; This. Weight =weight; This. Height =height; } /*** Age Growth *@paramGrowingage to increase the age*/ Public voidGrowage (intgrowingage) { Age= Age +Growingage; } /*** ways to show age*/ Public voidShowage () {logger.info (name+ "Age is" +Age ); } /*** Weight growth * *@paramgrowingweight to increase the weight*/ Public voidGrowweight (floatgrowingweight) {Weight= weight +Growingweight; } Public voidshowweight () {logger.info (name+ "Weight is" +weight); } /*** Height Increase * *@paramgrowingheight to increase the height*/ Public voidGrowheight (floatgrowingheight) {Height= height +Growingheight; } Public voidshowheight () {System.out.println (name+ "Height is" +height); } Public voidShoweyecolor () {logger.info (name+ "The eye color is" +Eyecolor); } Public voidWalk () {Logger.info (name+ "On foot"); }}
Here's how to call the Personoop class:
Testoop.java, the main display of human properties, and then use some methods to change the properties of people, and then show the properties of people
Public classTestoop { Public Static voidMain (string[] args) {//XiaohongPersonoop Xiaohong =NewPersonoop ("Xiaohong", +, "Black", 40.0f, 153.1f); Xiaohong.showage (); System.out.println (Xiaohong.height); Xiaohong.showweight (); Xiaohong.showeyecolor (); Xiaohong.walk (); Xiaohong.growage (2); Xiaohong.growweight (10.0f); Xiaohong.growheight (5.0f); Xiaohong.showage (); System.out.println (Xiaohong.height); Xiaohong.showweight (); // ******************************************************************************************* //XiaominPersonoop Xiaomin =NewPersonoop ("Xiaomin", "Blue", 50.0f, 163.1f); Xiaomin.showage (); System.out.println (Xiaomin.height); Xiaomin.showweight (); Xiaomin.showeyecolor (); Xiaomin.walk (); Xiaomin.growage (3); Xiaomin.growweight (10.0f); Xiaomin.growheight (5.0f); Xiaomin.showage (); System.out.println (Xiaohong.height); Xiaohong.showweight (); }}
The following shows the writing of human beings with completely process-oriented thinking.
Personpop.java
Importjava.util.logging.*;/*** A human being written using 100% purely process-oriented *@authorBfzs*/ Public classPersonpop {Private StaticLogger Logger = Logger.getlogger (personoop.class. toString ()); /*** Set to private method, prohibit such call constructor to instantiate, can only be called by the class name method cannot invoke method with instance, use as tool class*/ PrivatePersonpop () {} Public Static intGrowage (intAgeintgrowingage) { Age= Age +Growingage; returnAge ; } Public Static voidShowage (String name,intAge ) {Logger.info (name+ "Age is" +Age ); } Public Static floatGrowweight (floatWeightfloatgrowingweight) {Weight= weight +Growingweight; returnweight; } Public Static voidShowweight (String name,floatweight) {Logger.info (name+ "Weight is" +weight); } Public Static floatGrowheight (floatHeightfloatgrowingheight) {Height= height +Growingheight; returnheight; } Public Static voidShowheight (String name,floatheight) {SYSTEM.OUT.PRINTLN (name+ "Height is" +height); } Public Static voidShoweyecolor (string name, String eyecolor) {logger.info (name+ "The eye color is" +Eyecolor); } Public Static voidWalk (String name) {logger.info (name+ "On foot"); }}
The following is the Testpop.java file, mainly called Personpop, to show the properties of people and change the properties of people.
Public classTestpop { Public Static voidMain (string[] args) {//XiaohongString xiaohongname = "Xiaohong"; intXiaohongage = 16; String Xiaohongeyecolor= "BLACK"; floatXiaohongweight = 40.0f; floatXiaohongheight = 153.1f; Personpop.showage (Xiaohongname, xiaohongage); System.out.println (Xiaohongheight); Personpop.showweight (Xiaohongname, xiaohongweight); Personpop.showeyecolor (Xiaohongname, Xiaohongeyecolor); Personpop.walk (Xiaohongname); Xiaohongage= Personpop.growage (Xiaohongage, 2); Xiaohongweight= Personpop.growweight (Xiaohongweight, 10.0f); Xiaohongheight= Personpop.growheight (Xiaohongheight, 5.0f); Personpop.showage (Xiaohongname, xiaohongage); System.out.println (Xiaohongheight); Personpop.showweight (Xiaohongname, xiaohongweight); // ******************************************************************************************* //XiaominString xiaominname = "Xiaomin"; intXiaominage = 18; String Xiaomineyecolor= "Blue"; floatXiaominweight = 50.0f; floatXiaominheight = 163.1f; Personpop.showage (Xiaominname, xiaominage); System.out.println (Xiaominheight); Personpop.showweight (Xiaominname, xiaominweight); Personpop.showeyecolor (Xiaominname, Xiaomineyecolor); Personpop.walk (Xiaominname); Xiaominage= Personpop.growage (Xiaominage, 3); Xiaominweight= Personpop.growweight (Xiaominweight, 10.0f); Xiaominheight= Personpop.growheight (Xiaominheight, 5.0f); Personpop.showage (Xiaominname, xiaominage); System.out.println (Xiaominheight); Personpop.showweight (Xiaominname, xiaominweight); }}
The main way to write people with a process-oriented approach is to not use any of the member variable/instance properties, and all methods in the class are decorated with static. when invoking a process-oriented human, you need to set a number of variables outside the class to hold the property. Call object-oriented when the property is bound to the object, so do not need more out-of-class data, assume that the human has 100 attributes, need to operate 10 people, the process needs to set 1000 variables outside the class, using object-oriented in the outside of the class only need to set 10 human instance variables can be. Even if some people say that object-oriented is too complex, it is not necessary to use object-oriented, but it is complicated to write, once it is called as the base class, it is much more cool than invoking the process-oriented class. And when it comes to writing about human beings, object-oriented is just more of a member variable declaration and initialization setting, and it's not much harder than the process-oriented approach, but the thinking is different.
If you are a person who is not willing to use OOP, then you can use this process-oriented thinking to write Java, the most basic point of object-oriented is encapsulation (inheritance and polymorphism is optional, encapsulation is necessary), OOP is the encapsulation of data and methods, the Personpop class does not have any human attributes in any package, So it's not object-oriented. There is nothing that can be done only with object-oriented, process-oriented tasks.
Write Java programs using 100% process-oriented methods