For example, if I want to create a game, then there should be some objects in the game, warriors or enemies. Let us just assume that all object are human being. Then first, there should be a human class that have all attributes of a human and some methods that a human can do. Warriors and enemies are subclass of the human class, which means they will inherits all the attributes of human and those methods as well. What are those attributes? Hp or mp might be a good idea. And something like speed, defense data, attack data should be good. Those are variables stored in the class and can be easily accessed by using a getter or setter method. What are those methods? Attack, defense, sleep, walk or any action that will affect the variables will be good.
After creating a class, actually we just create some definition and we still have no object. Then we should create an instance of that class and we can modified the object by using some methods.
After all, I learned C before I learned Python. Although there is no class in C, C does have some data type that is really similar to class, structure. Actually, I think class is just a improved version of structure. Those are both neat way to keep track on every related variable.
Something about the property: I was really confused when I saw the function property. After reading the text on piazza, I finally understand what this means.
The variable stored in the class can be accessed by using getter or setter method. And what the property does is just overriding the default getter and setter method. Why we doing this is because python does not have other way to make the variable private. And by overriding the default getter and setter method, we can actually make them private.
Something about the property: I was really confused when I saw the function property. After reading the text on piazza, I finally understand what this means.
The variable stored in the class can be accessed by using getter or setter method. And what the property does is just overriding the default getter and setter method. Why we doing this is because python does not have other way to make the variable private. And by overriding the default getter and setter method, we can actually make them private.
No comments:
Post a Comment