(1) Class is a general term for something , The object is a real object . for example : Animals are a class , Dogs and cats are objects .
(2) Class through instantiation , Get the object
(1) Class has properties and methods
(2)__init__ Method is a special method , Every time you create an object with a class , Will automatically run this method .
The name of the method , There are two underscores at the beginning and two underscores at the end , It's an agreement , Avoid conflicts with other common method names
(3) Parameters self Is a reference to the object itself , Whether the object can access the properties and methods in the class .
self It is an automatic transfer without manual transfer , So when creating objects from classes , Just give the following formal parameters (name,age) Provide value .
# Defining classes
class Animal:
# Define the properties of the class : By construction method
def __init__(self,name,age):
self.name=name
self.age=age
# Define the methods of the class
def sit(self):
# Simulate the animal being ordered to sit down
print(self.name + " is now sitting")
# Instantiation 1—— Get objects through classes
dog=Animal('Xiaohua',3)
print(dog.name)
print(dog.age)
Xiaohua
3
# Instantiation 2—— Get other objects
cat=Animal('Xiaomao',2)
cat.name
‘Xiaomao’
cat.sit()
Xiaomao is now sitting