0

What is Class? And when we use that?

26th Sep 2019, 11:59 AM
Luan Truong
Luan Truong - avatar
2 Answers
+ 2
OOP is a programming concept where you divide tasks into object methods. Atleast it increases readability of code. Objects are naturally the core part of OOP. Class is a structure that can create objects. Class defines what its objects are and how they should work. Example: class Builder: ... big_builder = Builder() big_builder.build_house() This line: big_builder.build_house() already tells that object big_builder builds a house. build_house is a method, a function, that could have a 1000 lines of code, but still this 1 line already tells much what the method would do. It is all described in 1 line of code that "big builder simply builds a house". Example2: class Plant: ... tree = Plant() while tree.is_alive(): if tree.needs_water(): tree.water() else: tree.grow() This tells, that as long the tree is alive it will either get watered or it will grow. The while loop would be put whole in method tree.live()
27th Sep 2019, 8:10 PM
Seb TheS
Seb TheS - avatar