java是强制写class关键字的语言,不能有独立的函数游离在类外出现在文件中,这和python c++ php都不同,后面的都可以单独在类外写函数,所以java被称为是纯面向对象的语言,py和php都不是强制向对象的写法。
尽管如此,有的笨瓜,还是打死也写不出类来,而且强烈的反对写类,认为面向过程已经能搞定一切了,发明c++这种语言的人就是闲的没事,c++对于c来说是完全的多余没必要。
但是当被公司要求写java时候,就会蒙蔽,觉得没有面向对象经验,很难转变,此篇演示完全的面向过程的思维来写java。让从不用面向对象的py人员可以不用面向对象来写java。
首先是两个缩写。面向对象编程 oop, 面向过程编程pop
先来一个面向对象的java常规写法,再来一个完全的反对面向对象的写法。
PersonOOP.java文件。
import java.util.logging.*; /** * 一个使用面向对象写的人类 * @author bfzs */ public class PersonOOP { private String name; private int age; private final String eyeColor; private float weight; public float height; private static Logger logger = Logger.getLogger(PersonOOP.class.toString()); public PersonOOP(String personName, int age, String eyeColor, float weight, float height) { name = personName; this.age = age; this.eyeColor = eyeColor; this.weight = weight; this.height = height; } /** * 年龄增长 * @param growingAge 要增加的年龄 */ public void growAge(int growingAge) { age = age + growingAge; } /** * 展示年龄的方法 */ public void showAge() { logger.info(name + " 的年龄是 " + age); } /** * 体重增长 * * @param growingWeight 要增加的体重 */ public void growWeight(float growingWeight) { weight = weight + growingWeight; } public void showWeight() { logger.info(name + " 的体重是 " + weight); } /** * 身高增长 * * @param growingHeight 要增加的身高 */ public void growHeight(float growingHeight) { height = height + growingHeight; } public void showHeight() { System.out.println(name + " 的身高是 " + height); } public void showEyeColor() { logger.info(name + " 的眼睛颜色是 " + eyeColor); } public void walk() { logger.info(name + " 在走路"); } }
下面是调用PersonOOP类的方式:
TestOOP.java,主要是展示人的属性,然后用一些方法来改变人的属性,然后再展示人的属性
public class TestOOP { public static void main(String[] args) { //xiaohong PersonOOP xiaohong = new PersonOOP("xiaohong", 16, "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(); // ******************************************************************************************* //xiaomin PersonOOP xiaomin = new PersonOOP("xiaomin", 18, "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(); } }
下面来展示的是用完全面向过程的思维来写人类
PersonPOP.java
import java.util.logging.*; /** * 一个使用100%纯面向过程写的人类 * @author bfzs */ public class PersonPOP { private static Logger logger = Logger.getLogger(PersonOOP.class.toString()); /** * 设置为私有方法,禁止此类调用构造函数进行实例化,仅能由类名调用方法不能用实例调用方法,当作工具类使用 */ private PersonPOP() { } public static int growAge(int age, int growingAge) { age = age + growingAge; return age; } public static void showAge(String name, int age) { logger.info(name + " 的年龄是 " + age); } public static float growWeight(float weight, float growingWeight) { weight = weight + growingWeight; return weight; } public static void showWeight(String name, float weight) { logger.info(name + " 的体重是 " + weight); } public static float growHeight(float height, float growingHeight) { height = height + growingHeight; return height; } public static void showHeight(String name, float height) { System.out.println(name + " 的身高是 " + height); } public static void showEyeColor(String name, String eyeColor) { logger.info(name + " 的眼睛颜色是 " + eyeColor); } public static void walk(String name) { logger.info(name + " 在走路"); } }
下面是TestPOP.java文件,主要是调用PersonPOP,来展示人的属性和改变人的属性。
public class TestPOP { public static void main(String[] args) { //xiaohong String xiaohongName = "xiaohong"; int xiaohongAge = 16; String xiaohongEyeColor = "black"; float xiaohongWeight = 40.0f; float xiaohongHeight = 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); // ******************************************************************************************* //xiaomin String xiaominName = "xiaomin"; int xiaominAge = 18; String xiaominEyeColor = "blue"; float xiaominWeight = 50.0f; float xiaominHeight = 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); } }
用面向过程来写人类,主要写法方式就是不使用任何的成员变量/实例属性,类中所有方法全部都用static修饰。调用面向过程的人类时候,需要在类外设置很多变量来保存属性。调用面向对象的时候是把属性绑定到对象上,所以不需要更多的类外数据,假设人类有100个属性,需要操作10个人,面向过程需要在类外设置1000个变量,使用面向对象在类外只需要设置10个人类实例变量就可以了。就算有人说面向对象太复杂了,没必要用面向对象,但那是写起来复杂,一旦做为基础类被调用起来,那是比调用面向过程的类爽多了。而且在写人类的时候,面向对象也只是多了成员变量声明和初始化设置,并没有比面向过程的写法难很多,只是思维不一样。
如果是打死也不愿意使用oop的人,那么可以用此篇的面向过程思维来写java,面向对象最最基本的一点是封装(继承和多态是可选的,封装是必须的),oop是数据和方法的封装,PersonPOP类完全没有对人的属性有任何封装,所以不算是面向对象。没有什么是只有面向对象能搞定,面向过程搞不定了的任务。
原文地址:https://www.cnblogs.com/ydf0509/p/9130185.html