objects with state and behavior in oop

后端 未结 4 471
渐次进展
渐次进展 2021-02-04 05:14

I keep hearing the term object has behavior and state or just one of them. But what is the difference or what does it mean, and if anyone can give an example I would really appr

4条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-04 05:26

    abstract class Animal
    {
       int age;
       abstract void Run();
    }
    
    class Tiger:Animal
    {
        override void Run()
        {
          //something.
        }
    }
    
    main()
    {
         Tiger t1 = new Tiger();
         Tiger t2 = new Tiger();
         t1.age = 25;
         t2.age = 10;
    }
    

    Now you have created two Tiger objects. Tiger can Run. That is the behavior of the object Tiger. t1 age is 25 and t2 age is 10. t1.age, t2.age is the state of the object.

    Hope this helps.

提交回复
热议问题