To start an Object-Oriented Programming, the first step is to create a class( sometimes there may be more than one class) and give its meaning. Then we can start writing the definitions. Usually, the first definition we have to write is initials. It usually includes some key information that appears later.
then we have to write some string method and representation method, which return a string version of self and a string representation of self.
Then sometimes we may need a method called –equal--, which is created to see if two pieces of stuff in the class are the same.
After writing down these normal things, we usually focus on the method that needs our real intelligence. Since we know this kind of methods follows what we want to realize, we cannot define them as we did above. We may understand them by using an example.
If we want to create something that can calculate the area of any triangle by knowing its three nodes, we have to firstly import the method Math, and then write something like this:
return (self.base**2 * 3**(1/2)) / 4
Above is how we write basic classes. And sometimes we need to write some subclasses for further usage. The subclasses inherit all the features of the basic class, and we can add the methods in the same way.
没有评论:
发表评论