In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Most people do not understand the knowledge points of this article "python how to define classes and objects", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "python how to define classes and objects" article.
Define a simple class
Object-oriented is a larger encapsulation, encapsulating multiple methods in a class, so that objects created by this class can call these methods directly!
Define a class that contains only methods
In python, to define a class that contains only methods, the syntax format is as follows:
# define the class name with class: def method 1 (self, parameter list) # method has and only one self pass def method 2 (self, parameter list) # the def defined outside the class is a function, the def defined in the class is called a method, and there must be self passclass Student: pass in parentheses
The definition format of the method is almost the same as the previously learned function, except that the first parameter must be self
Create object
When a class definition is complete, use this class to create an object in the following syntax format:
Object variable name = class name ()
The first object-oriented program
Demand: kittens like to eat fish, kittens need water
Analysis: 1. Define a cat (Cat) 2. Define two methods, ect and drink3. According to the requirement-there is no need to define the attribute class Cat: # Cat as the name (class name) of the class, and one or more words are composed of the first letter of each word in uppercase The rest of the letters are lowercase "" this is a cat "" def ect (self): # definition method print ("kittens love fish") def drink (self): print ("kittens want water") tom=Cat () # create an object of a class # big_tom=Cat () each class can have multiple objects Each object opens up a new storage space idtom.ect () # object calls the method encapsulated in the class, the object name. Method name () tom.drink () # object calls the method encapsulated in the class
Use self to output the name of each cat inside the method
The self in the method is the reference to which object is called by which object
1. Inside the method encapsulated by the class, self represents the object that currently calls the method.
two。 When calling a method, the programmer does not need to pass the self parameter
Inside the method:
1. You can also access the properties of an object through self
two。 You can also call other object methods through self
# the transformation code is as follows: class Cat: def eat (self): print ("% s love fish"% self.name) tom=Cat () tom.name= "Tom" tom.eat () lazy_cat=Cat () lazy_cat.name= "lazy cat" lazy_cat.eat ()
In daily development, it is not recommended to add attributes to the object outside the class. If the attribute is not found at run time, the program will report an error. What attributes the object should contain should be encapsulated inside the class.
Class Student: def _ init__ (self,name,age): self.name=name self.age=age print ("my name is", name, "this year is", age) xiaoming=Student ("Xiao Ming", 20) xiaohong=Student ("Xiao Hong", 18)
Creation of a class
# creation of class: class Student: native_place= "Jilin" # class attribute def _ _ init__ (self,name,age): # name,age is instance attribute self.name=name self.age=age def eat (self): print ("I am eating...") # instance method def info (self): print ("my name is:", self.name, "age is:" Self.age) # class method @ classmethod def cm (cls): print ("class method") # static method @ staticmethod def sm (): print ("static method") # object creation Create an object of the Student class stu1=Student ("Zhang San", 20) # create an object stu1.info () # object call class method stu1.eat () print ("my name is", stu1.name, "this year's age is:", stu1.age,stu1.eat) print (stu1)
Class properties, class methods, static methods
Class attributes: variables outside methods in a class become class properties and are shared by all objects of the class
Class methods: methods that are decorated with @ calssmethod and accessed directly by class names
Static method: use @ staticmethod to modify the main method and use the class name to access the method directly
Print (Student.native_place) # access class attribute Student.cm () # call class method Student.sm () # call static method stu3=Student ("Zhang San", 20) stu4=Student ("Li Si" 30) print (stu3.native_place) # Jilin print (stu4.native_place) # Jilin Student.native_place= "Tianjin" # change class attribute print (stu3.native_place) # Tianjin print (stu4.native_place) # Tianjin above is the content of this article on "how python defines classes and objects" I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.