Home  >  Article  >  Java  >  5. Object-oriented VS process-oriented in Java

5. Object-oriented VS process-oriented in Java

零下一度
零下一度Original
2017-06-25 10:45:191045browse

1. Object-oriented VS process-oriented

 1 /* 2  * 理解一:人开门 3  * 面向过程:人 打开门 4  * 面向对象: 5  * 人{ 6  *    打开(){ 7  *        门。开开(); 8  *    } 9  * }10  * 门{11  *     开开(){12  *     }13  * }14  * 
15  * 理解二:人把大象装进冰箱16  * 面向过程:17  *     1、打开冰箱;18  *     2、把大象放进去;19  *     3、关闭冰箱门;20  * 面向对象:21  * 人{22  *     打开(冰箱){冰箱.开开()}23  *     操作(大象){大象。进入(冰箱)}24  *     关闭(冰箱){冰箱。合上()}25  * }26  * 大象{27  *     进入(冰箱){}28  * }29  * 冰箱{30  *     开开(){}31  *     合上(){}32  * }33  */

2. Object-oriented programming focuses on the design of classes!

 2.1. No matter how large a project or project is, it must be composed of classes one by one.

 2.2. Classes are abstract, like drawings for manufacturing a car. The specific cars are manufactured according to the drawings, which are actually instantiations of the class

 1 /* 2  * 1、面向对象的编程关注于类的设计 3  * 2、设计类实际上就是设计类的成员 4  * 3、基本的类的成员,属性(成员变量或Field)&方法(Method) 5  */ 6 public class TestPerson { 7     public static void main(String[] args) { 8         Person p1 = new Person(); 9         p1.info();10         11         p1.name = "钟超";12         p1.age = 21;13         p1.sex = true;14         p1.info();15         16         p1.setName("张三");//p1.name = "张三"17         p1.info();18         19         Person p2 = p1;20         System.out.println("P1:" + p1);21         System.out.println("P2:" + p2);22         p2.info();23         24         p2 = new Person();25         System.out.println("P2:" + p2);26         p2.info();27         //实例化Scanner类的对象,通过此对象.nextXxx的方法进行调用,完成相应的功能;28         Scanner s = new Scanner(System.in);29         int i = s.nextInt();30         31         Person p3 = new Person();32         p3.info();33     }34 }35 //类:是抽象的。36 class Person{37     //1.属性38     String name;39     int age = 10;40     boolean sex;41     42     //2.方法43     public void eat(){44         System.out.println("人吃饭");45     }46     public void sleep(){47         System.out.println("人睡觉");48     }49     50     public String getName(){51         return name;52     }53     public void setName(String n){54         name = n;55     }56     57     public void info(){58 //        eat();59 //        sleep();60         System.out.println("name:" + name + "age:" + age + "sex:" + sex);61     }62 }

3. The idea of ​​completing a project (or function)

3.1. Whether the object of the class corresponding to the function to be completed exists;

3.2. If it exists, just call the properties or methods in the corresponding class directly through the object;

3.3. If it does not exist, you need to create an object of the class. Even if the class does not exist, you need to design the class;

4. The three main lines of object-oriented programming:

 1. Classes and their components: attributes, methods, constructors, code blocks, internal classes

 2. Characteristics of object-oriented programming: encapsulation, inheritance, polymorphism (abstraction)

 3. Other keywords: this super package import static final abstract interface...

Class:

1. Focus on the design of the class

2. Class components:

1. Attributes (member variables, Field)

 2. Method (member method, function, Method)

3. About attributes:

4. Method:

  1 /*  2  * 一、面向对象思想的落地法则一:  3  * 1.设计类并设计类的成员(成员变量&方法)  4  * 2.通过类来创建类的对象(也称作类的实例化)  5  * 3、通过“对象。属性”或“对象。方法”来调用,完成相应的功能  6  * 
  7  * 二、创建的多个对象,彼此各自拥有一套类的属性,当对其中一个对象的属性进行修改时。  8  * 不会影响到其他对象的属性值。  9  * 
 10  * 三、类的属性(成员变量) 11  *     成员变量    VS     局部变量 12  *     相同点:1、遵循变量声明的格式;数据类型 变量名 = 初始化值; 13  *          2、都有作用域: 14  *     不同点:1、声明的位置的不同:成员变量:生命在类里,方法外边; 15  *                               局部变量:声明在方法内,方法的形参部分,代码块内 16  *          2、成员变量的修饰符有四个:public(公共的) private(私有的) protected 缺省 17  *                    局部变量没有修饰符:与所在的方法的修饰符一样; 18  *          3、初始化值:一定会有初始化值; 19  *                   成员变量:如果在声明的时候,不显示的赋值,那么不同的数据类型会有不同的默认初始化值; 20  *                 byte short int long = 0; 21  *                 float double = 0。0; 22  *                 char = 空格; 23  *                 boolean = false; 24  *                 引用类似的变量  = null; 25  *                  局部变量:一定要显示的赋值(局部变量没有默认初始化值); 26  *          4、二者在内存中存放的位置不同,成员变量在堆空间中,局部变量在栈空间中; 27  * 总结:关于变量的分类:1、按照数据类型的不同:基本数据类型(8种)&引用数据类型 28  *                  2、按照声明位置的不同::成员变量&局部变量 29  * 
 30  * 四、类的方法:提供某种功能的实现 31  *         1、实例    public void eat(){方法体} 32  *               public String getName(){} 33  *               public void setName(String n){} 34  *         2、格式:权限修饰符  返回值类型(void:无返回值/具体返回值) 方法名(形参){} 35  *         3、关于返回值类型 void:表名此方法不需要返回值 
 36  *                     有返回值的:在方法的最后一定有return + 返回值类型对应的变量 37  *           记忆:void与return不可以同时出现在一个方法内。 38  *         4、方法内可以调用本类的其他方法和属性,但是不能再方法内在定义其他方法; 39  * 
 40  * ClassRoom Car ... 41  */ 42 public class Zoo { 43     public static void main(String[] args) { 44         //基本数据类型的声明:数据类型 变量名 = 初始化值; 45         int i = 10; 46         //1.类的实例化:如下的a1就是一个实实在在的对象 47         Animal a1 = new Animal(); 48         //int[] arr = new int[10]; 49         //通过对象调用属性 50         a1.name = "花花"; 51         a1.age = 3; 52         System.out.println("name:" + a1.name + "\t" + "age:" + a1.age); 53         //通过对象调用方法 54         a1.eat(); 55         a1.sleep(); 56          57         //在创建一个对象 58         Animal a2 = new Animal(); 59         System.out.println("name:" + a2.name + "\t" + "age:" + a2.age); 60         a2.name = "小花"; 61         System.out.println("name:" + a1.name + "\t" + "age:" + a1.age); 62         System.out.println("name:" + a2.name + "\t" + "age:" + a2.age); 63         //a3与a1一样,a3不意味着相较于a1重新创建的一个对象,而是a1与a3共用一个对象实体 64         Animal a3 = a1; 65         System.out.println("name:" + a3.name + "\t" + "age:" + a3.age); 66         a3.name = "维尼熊"; 67         System.out.println("name:" + a1.name + "\t" + "age:" + a1.age); 68          69         System.out.println(a2.getName()); 70         System.out.println(a2.desc()); 71     } 72 } 73  74 class Animal{ 75     //1.属性 76     String name; 77     int age; 78     //2.方法 79     public void eat(){ 80         System.out.println("动物进食"); 81     } 82     public void sleep(){ 83         System.out.println("动物休眠"); 84         //return; 85     } 86     public String getName(){ 87         return name; 88     } 89     public int getAge(){ 90         return age; 91         //其后不可以声明语句 92         //System.out.println("Hello"); 93     } 94     //当通过对象调用此方法是,会将方法的返回值提供给方法的调用者,也就是当前的对象。 95     public String desc(){ 96         if(age > 2){ 97             return "恰同学少年"; 98         }else{ 99             return "还是看动画片的 年龄";100         }101     }102     public void setName(String n){103         name = n;104     }105     public void addAge(){106         int i = 2;107         age += i;108     }109     public void info(){110         //可以在方法内调用本类的其他方法,但是不可以在方法内在定义新德尔方法111         eat();112         sleep();113 //        public void breathJ(){114 //            System.out.println("呼吸");115 //        }116     }117     //System.out.println("Hello!");118 }

5 , The first rule of implementation of object-oriented programming ideas:

1. Design and create classes and class components;

2. Instantiate class objects;

3. Pass Complete a certain function in the form of "object.property" or "object.method"

6. Class initialization memory analysis:

6.1. Memory division structure:

Stack: local variables, object reference names, array reference names

Heap: "things" coming out of new (for example: object entities, array entities), including member variables

Method area: Contains string constants

Static domain: Variables declared as static

6.2. On the basis of understanding, you must learn how to create basic class objects in memory in operation.

7. Overloading of methods:

Requirements: 1. In the same class;
* 2. The method names must be the same;
* 3. Methods The parameter list is different; 1. The number of parameters is different;
* 2. The parameter types are different;
* 4. The overloading of the method has nothing to do with the return value type of the method;

 1 /* 2  * 方法的重载(overload) 3  * 要求:1、同一个类中; 4  *        2、方法名必须相同; 5  *        3、方法的参数列表不同;1、参数的个数不同; 6  *                       2、参数类型不同; 7  *        4、方法的重载与方法的返回值类型没有关系; 8  */ 9 public class TestOverLoad {10 11 }12 class OverLoad{13     //定义两个int型变量的和14     public int getSum(int i, int j){15         return i + j;16     }17     //定义三个int型变量的和18     public int getSum(int i, int j, int k){19         return i + j + k;20     }21     //不能与其他几个方法构成重载22 //    public int getSum1(int i, int j, int k){23 //        return i + j + k;24 //    }25 //    public void getSum(int i, int j, int k){26 //        System.out.println(i + j + k);27 //    }28     //定义两个double型的数据的和29     public double getSum(double d1, double d2){30         return d1 + d2;31     }32     //定义三个double型数据的和33     public void getSum(double d1,double d2,double d3){34         System.out.println(d1 + d2 + d3);35     }36     //以下两个方法也构成重载37     public void method1(int i, String str){38         39     }40     public void method1(String str1, int j){41         42     }43 }

The above is the detailed content of 5. Object-oriented VS process-oriented in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn