The most important object-oriented concepts is the class (Class) and instance (Instance), abstract class is a template, which is based on the instance of the class created out of a concrete "objects", each object has the same methods, but each the data may be different. The example code below illustrates some basic knowledge classes.
. 1 # ! / Usr / bin / Python the env 2 # - * - Coding: UTF-. 8 - * - . 3 # @date: 2020-03-08 . 4 # @author: Flyinghappy ([email protected]) . 5 # @link : https://www.cnblogs.com/flyinghappy/ . 6 # @Version: $ $ Id . 7 Import JSON . 8 Import OS . 9 10 class the Person: . 11 '' ' function, a first default parameter class is defined inside Self ' '' 12 is NUM = 0 # class attribute 13 is DEF the __init__ (Self, name = '' , Age = 20 is):# Special way when creating class instances of the name, age and other attributes to bind to the instance. 14 . Self __name__ = name # class instance variables, front '__' double underlined limiting external access 15 self.age Age = # class instance variables 16 self.talking_note = [] # class instance variable . 17 DEF Talk (Self, talk_something): # class method 18 is self.talking_note.append (talk_something) . 19 Print (talk_something) 20 is DEF Save (Self): # class method 21 is filename = ' Person ' + ' .json ' 22 is = person_info [. Self __name__ , self.age, self.talking_note, person.num] 23 is with Open (filename, ' W ' ) AS F: 24 The json.dump (person_info, F) 25 f.close () 26 is 27 IF the __name__ == ' __main__ ' : 28 Person = Person (name = ' flyinghappy ' , age = 40) # declare a class instance of person, name and age and initialize attributes 29 '' ' following the while loop, using the Person class attribute num '' ' 30 the while person.num <. 5 : 31 is note='i am number:'+str(person.num) 32 person.talk(note) 33 person.num+=1 34 else: 35 person.save()
Output:
Person.json file storage contents are as follows:
! # / usr / bin / env python # - * - coding: utf-8 - * - # @Date: 2020-03-08 # @Author: Flyinghappy ([email protected]) # @Link: https: // @Version www.cnblogs.com/flyinghappy/#: Import jsonimport OS $ $ Id
class the Person: '' 'inside the class definition function, the first parameter is the default self' '' num = 0 # class attributes def __init __ (self , name = '', age = 20): # special method, when creating the instance of the class name, age and other property is bound to instance. self .__ name = name # class instance variables, front '__' double underlined limiting external access self.age = age # class instance variables self.talking_note = [] # class instance variable def talk (self, talk_something): # self class method .talking_note.append (talk_something) print (talk_something) def save (self): # class method filename = 'person' + person_info = [self .__ name, self.age, self.talking_note, person.num] with 'json.' open (filename, 'w') as f: json.dump (person_info, f) f.close ()
if __name__ == '__main__': person = Person (name = 'flyinghappy', age = 40) # declare a class instance of person, name and age and initialize attributes '' 'following the while loop, using the Person class attribute num' '' while person.num <5: note = 'i am number:' + str (person.num) person.talk (note) person.num + = 1 else: person.save ()