search

Home  >  Q&A  >  body text

java - 面向对象思考问题的方法

我学了C语言,他是面向过程的。
我在学习面向对象时,发现和面向过程思考问题很多地方不一样,考虑现实中的问题。
请大家说一下你们是怎么思考面向对象的问题。

谢谢大家

ringa_leeringa_lee2767 days ago543

reply all(2)I'll reply

  • PHPz

    PHPz2017-04-17 13:40:42

    C is a process-oriented language, and methods defined in the code are generally called all the time; in object-oriented languages ​​​​(such as Java) or object-oriented languages ​​​​(such as JavaScript), methods are defined on objects or classes. (ps: There is no clear explanation of classes in js). For example, there is a method to compare sizes:

    In C language:

    int compare(int num1,int num2){
         return num1>num2?num1:num2;
    }
    
    //调用
    compare(1,2);
    

    In Java

    public class Compare{
         private int num1;
         private int num2;
    
         public Compare(int num3,int num3){
                  this.num1=num3;
                  this.num2=num4;
         }
    
         public int com(){
                   return this.num1>this.num2?this.num1:this.num2;
         }
    }
    
    //调用
    Compare cpm = new Compare(5,6);
    System.out.println(cpm.com());
    

    The above is a simple example to distinguish the different ways of thinking about the same problem between process-oriented and object-oriented. Although there is no outstanding point of being object-oriented here, in actual projects, there are many methods and a large amount of coding. In this case, classes can organize the code very well. For example, a user has different behaviors such as eating, walking, sleeping, and entertainment.
    In C language, the implementation might look like this:

    void eating(){}
    
    void walk(){}
    
    void sleep(){}
    
    void entertainment(){}
    
    .......//定义其它方法
    

    In an object-oriented language, the implementation may look like this:

    public User{
        void eating(){}
    
        void walk(){}
    
        void sleep(){}
    
        void entertainment(){}
    
        ....//其它方法
    }
    
    

    Obviously, object-oriented assembles the code structure very well, making it easier to understand. KuKe has an article about understanding object-oriented programming:
    Understanding object-oriented programming in this way, I hope it will be helpful to the subject's understanding of object-oriented programming

    reply
    0
  • ringa_lee

    ringa_lee2017-04-17 13:40:42

    You can read a Turing book called "This Object-Oriented Programming Book for Everyone".

    reply
    0
  • Cancelreply