Week Three - Object-Oriented Programming
Object-oriented programming is a very useful technique in
programming, it creates object that has both data and function, and is often
related to real-world situations. In the first week, we talked about how to
make a data variable "private", and how users can access it using the
"get" and "set" method. You cannot declare a variable as
private in python, by convention all the "private variables" have an
underscore before its name.
In week three, we first learned about inheritance. Every class we create is a subclass of class Object, and we automatically inherit all the methods and variable from Object class. And we can override those methods in the subclass, for example, the __eq__ method in Object class will return true if the two objects is referring to the same memory address. However, if we create a class we can write our own version of the __eq__ to decide how the two objects are going be equal. I find inheritance really useful, for one reason is that it can avoid duplicated code. For instance, if we are making a game, and we have Sword, Axe, Wand classes. If they are only different in some methods, it is more convenient to create a Weapon class and create all the common methods. Then create subclasses Sword, Axe and Wand, with the methods that are different.
I also found raising and catching exceptions a little frustrating, it took me quite a while to figure out what does a “try…except…” block do. From my understanding, if you raise an exception inside the try block, if there is any other code after this line, it will not be executed. But I have to say exceptions are also really useful, because it provides a way to deal with potential errors in the program. And if a program crashes, with an exception, it is easier for the user to figure out what the bug is.
Overall, I found this week's material easier to understand than last week (recursion), and lab #2 was also easier then lab #1.
In week three, we first learned about inheritance. Every class we create is a subclass of class Object, and we automatically inherit all the methods and variable from Object class. And we can override those methods in the subclass, for example, the __eq__ method in Object class will return true if the two objects is referring to the same memory address. However, if we create a class we can write our own version of the __eq__ to decide how the two objects are going be equal. I find inheritance really useful, for one reason is that it can avoid duplicated code. For instance, if we are making a game, and we have Sword, Axe, Wand classes. If they are only different in some methods, it is more convenient to create a Weapon class and create all the common methods. Then create subclasses Sword, Axe and Wand, with the methods that are different.
I also found raising and catching exceptions a little frustrating, it took me quite a while to figure out what does a “try…except…” block do. From my understanding, if you raise an exception inside the try block, if there is any other code after this line, it will not be executed. But I have to say exceptions are also really useful, because it provides a way to deal with potential errors in the program. And if a program crashes, with an exception, it is easier for the user to figure out what the bug is.
Overall, I found this week's material easier to understand than last week (recursion), and lab #2 was also easier then lab #1.
No comments:
Post a Comment