Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Object-oriented example Analysis of Python3

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "the object-oriented example analysis of Python3". In the daily operation, I believe that many people have doubts about the object-oriented example analysis of Python3. The editor has consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "object-oriented example analysis of Python3". Next, please follow the editor to study!

# object-oriented import abcfrom enum import Enum, unique@unique # unique ensures no duplicate values class Weekday (Enum): "" enumerated classes "" # name on the left and value Sun = 0 Mon = 1 Tue = 2 Wed = 3 Thu = 4 Fri = 5 Sat = 6class Person (metaclass=abc.ABCMeta): "Person is an abstract class The subclass of Person needs to implement the abstract method "" @ abc.abstractmethod def abs_m (self): print ("Person:abs_m") class Student (Person): "Student Doc" # implement the abstract method def abs_m (self): print ("absMethod") # use the parent class to reference def sup (self): super ( ). Abs_m () # count is the class attribute count = 0 # class method @ classmethod def cm (cls): print ("classMethod:" Student.count) # static method @ staticmethod def sm (): print ("staticMethod:", Student.count) # instance private method def _ p (self): # if self is not used Here you might warn this method may be static print ("private method", self) # define that this class only allows these attributes _ _ slots__ = ('name',' _ _ age', "_ gender") def _ _ init__ (self, name, age, gender): # this is the instance property Self.name = name # two underscores start with private attributes and cannot be accessed externally (if you want to force access, you can use s._Student__name, but this is not recommended) self.__age = age # an underscore begins with private attributes that can be accessed directly from the outside, but access is not recommended (why is this designed?) Self._gender = gender pass # the beginning and end of a double underscore are special methods or variables, not private External can access # so do not define a method or property name as the beginning and end of a double underscore def _ _ str__ (self): return f "{self.name}, {self.__age}, {self._gender}" if _ _ name__ = ='_ main__': print (Student ("jack", 12, "male"). Cm () print (Student ("jack", 12) "male") .sm () s = Student ("jack", 12, "male") # s specific type print (type (s). _ _ name__) # built-in attr function uses print (hasattr (s, "name")) print (getattr (s, "name")) # print (delattr (s, "name")) # print (setattr (s, "abc") 1)) whether # s is a type print (isinstance (s, Student)) # whether the Student class is a subclass of a class print (issubclass (Student) Object) # call s. Enumerated strands _ () method print (s) # call foo method A.func2 using enumerated class day = Weekday.Monprint (day.name) print (day.value) class A (object): bar = 1def func1 (self): print ('foo') @ classmethoddef func2 (cls): print (' func2') print (cls.bar) cls (). Func1 () # call foo method A.func2 () # there is no need to instantiate class C (object): @ staticmethoddef f (): print ('runoob') C.F (); # static method does not need to instantiate cobj = C () cobj.f () # can be called here after instantiation, the study on "object-oriented example analysis of Python3" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report