 |
Cheat Engine The Official Site of Cheat Engine
|
| View previous topic :: View next topic |
| Author |
Message |
Dr.Disrespect Grandmaster Cheater
Reputation: 3
Joined: 17 Feb 2016 Posts: 526
|
Posted: Sat May 14, 2016 2:26 pm Post subject: Python quesiton: superclass. |
|
|
Please take a look at the code below. I create a superclass "Animal" and a class "Dog" that inherit from "Animal" However, I kept getting an error message, which is in the screenshot. Based on the error message, it looks like the class "Dog" did not get the attributes from superclass "Animal". Thanks in advance.
| Code: |
class Animal:
__name = ""
__height = 0
__sexuality = ""
def __init__(self, name, height, sexuality):
self.__name = name
self.__height = height
self.__sexuality = sexuality
def set_name(self, name):
self.__name = name
def get_name(self):
return self.__name
def set_height(self, height):
self.__height = height
def get_height(self):
return self.__height
def set_sexuality(self, sexuality):
self.__sexuality = sexuality
def get_sexuality(self):
return self.__sexuality
def get_type(self):
return "Animal"
def toString(self):
return "{} is {} cm tall and it is a {}".format(self.__name,
self.__height,
self.__sexuality)
class Dog(Animal):
__owner = ""
def __init__(self, name, height, sexuality, owner):
self.__owner = owner
super(Dog, self).__init__(name, height, sexuality)
def set_owner(self,owner):
self.__owner = owner
def get_owner(self):
return self.__owner
def get_type(self):
return "Dog"
def toString(self):
return "{} is a dog, it is {} cm tall, it is a {} and {} is his owner".format(self.__name, <---- This is line 56
self.__height,
self.__sexuality,
self.__owner)
dog = Dog("Lab", 30, "male", "Chris")
print(dog.toString()) <----This is line 64.
|
| Description: |
|
| Filesize: |
80.05 KB |
| Viewed: |
4544 Time(s) |

|
|
|
| Back to top |
|
 |
ParkourPenguin I post too much
Reputation: 152
Joined: 06 Jul 2014 Posts: 4709
|
Posted: Sat May 14, 2016 5:45 pm Post subject: |
|
|
Haven't studied python, but after taking 10 seconds to use google:
| Quote: | | This brings us to the double underscore prefix. "__privateMethod" can also be accessed from outside the class, but to do so, you need to write "object._ClassName__privateMethod." This makes it a bit more difficult to access said method/field, but not impossible. Also, the method/field name is mangled with the class in which that method/field is defined, not in any subclasses. | Source: https://www.quora.com/What-are-the-access-modifiers-in-python
_________________
I don't know where I'm going, but I'll figure it out when I get there. |
|
| Back to top |
|
 |
Dr.Disrespect Grandmaster Cheater
Reputation: 3
Joined: 17 Feb 2016 Posts: 526
|
Posted: Mon May 16, 2016 12:23 pm Post subject: |
|
|
| ParkourPenguin wrote: | Haven't studied python, but after taking 10 seconds to use google:
| Quote: | | This brings us to the double underscore prefix. "__privateMethod" can also be accessed from outside the class, but to do so, you need to write "object._ClassName__privateMethod." This makes it a bit more difficult to access said method/field, but not impossible. Also, the method/field name is mangled with the class in which that method/field is defined, not in any subclasses. | Source: https://www.quora.com/What-are-the-access-modifiers-in-python |
Thanks for the reply, but I still don't know where I did wrong,
|
|
| Back to top |
|
 |
Zanzer I post too much
Reputation: 126
Joined: 09 Jun 2013 Posts: 3278
|
Posted: Mon May 16, 2016 8:54 pm Post subject: |
|
|
Sounds like it's suggesting:
Or get rid of all the underscores for your variables...
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
|
|