I'm learning python Class time of , Try running the following code :
# Create a dog class
class Dog():
""" A simple attempt to simulate a puppy """
def __init__(self,name,age):
""" Initialization property namez and age"""
self.name = name
self.age = age
def hungry():
""" Print a message """
print("The dog is hungry")
# Create an object
my_dog = Dog("Dalin",4)
# Calling method
my_dog.hungry()
appear Report errors :
Finally found The mistake is :
among hungry() Method must have a parameter , The general default is "self"
The revised code is :
# Create a dog class
class Dog():
""" A simple attempt to simulate a puppy """
def __init__(self,name,age):
""" Initialization property namez and age"""
self.name = name
self.age = age
def hungry(self):
""" Print a message """
print("The dog is hungry")
# Create an object
my_dog = Dog("Dalin",4)
# Calling method
my_dog.hungry()
summary :
When defining a method in a class , At least one parameter should be defined , It is usually named "self", Instance of its representative class , Represents the address of the current object , It's a must , Even if you do not need to pass in the corresponding parameters when calling .