First, the old and new classes in Python object-oriented
1) New-style classes (recommended): When defining a class, the base class (object) must be inherited in parentheses after the class. In python3.x, if the parent class is not specified, the object is used as the base class by default; in python2.x, if the parent class is not specified, the object is not used as the base class.
2) Old-style classes (classic classes): When defining a class, you do not need to inherit the object class in parentheses after the class, or even parentheses.
3) The dir method is a built-in method used to view the class.
Second, private methods and properties
1) Private properties are properties that the object does not want to expose; private methods are methods that the object does not want to expose. When defining private properties and methods, add __ (two underscores) before the property or method name
#Define a man class
class Man (object):
def __init __ (self, name, age):
self.name = name
self .__ age = age # define a private property age
def __secret (self):
print "The age of% s is:% s"% (self.name, self .__ age)
def test (self): # Public classes can call private properties and methods
self .__ secret ()
demo = Man (‘demo’, 18)
# Cannot access private properties
# print demo.age
# Cannot access private methods
# demo.secret ()
demo.test ()
2) The problem of private attributes and methods in inheritance: subclasses cannot inherit the private attributes and methods of the parent class
class A (object):
def __init__self (self, name):
self .__ age = 18 # define a private property and a public property
self.name = name
def __secret (self):
print "The age of% s is% s"% (self.name, self .__ age)
class B (A):
def test (self):
print self.name
# Cannot access parent class private properties and methods
# print self .__ age
# self .__ secret ()
demo = A (‘demo’)
# Private properties and methods of the parent class cannot be accessed outside
demo.test
Class attributes and methods
1) Class object: A class is a special object, also called a class object. Therefore, class objects also have their own corresponding properties and methods, called class properties and class methods.
2) Class attribute: It is the attribute defined for the class object; it is usually used to record the characteristics related to the class; it does not record the characteristics of specific objects. Defined below the class keyword.
3) Class methods: methods defined for class objects; only class attributes or class methods can be called inside a class method. The definition format is: @classmethod def class name (): pass
class Toy (object):
count = 0 # define class attributes using assignment statements
def __init __ (self, name):
self.name = name
Toy.count + = 1 # Every time an object is created, the counter is incremented by 1.
@classmethod # define the identity of a class method
def show_toy_count (cls):
print "The number of toy objects is% d"% cls.count
# Create toy object
toy1 = Toy (‘Lego’)
toy2 = Toy (‘Toy car’)
Toy.count # call class attributes
Toy.show_toy_count () # call class method
Fourth, the static method
1) Static method: When developing, if you need to encapsulate a method in a class; this method does not need to access instance attributes or instance methods, nor does it need to access class attributes or class methods; at this time we can encapsulate this method as static method. The definition format is: @staticmethod def class name (): pass
class Cat (object):
@staticmethod
# Cannot access instance attributes / class attributes
# Static method does not need to pass the first parameter self
def call ():
print "Meow Meow ~~~"
# Called through ‘class name.static method’; (you can call static methods directly without creating an object)
Cat.call ()
Five, comprehensive examples of class objects, class attributes, and static methods
1) Requirements: design a game class
Attribute: Define an attribute top_score to record the highest historical score of the game (class attribute)
Define an attribute player_name to record the player name of the current game player (instance attribute)
method:
show_help show game help information (static method, own parameters)
show_top_socre shows the highest historical score (class method, parameter is cls)
start_game starts the current player's game (instance method, parameter is self)
import random
class Game (object):
top_score = 0 # Define a class attribute to calculate the highest score
def __init __ (self, name):
self.play_name = name
Game.top_score = 0 # initialized to 0
@staticmethod
def show_help (): # define a static function
print "Game Help"
print "Randomly generated 1-10 numbers. Guess three times! The first guess is 10 points; the second guess is 5 points; the third guess is 3 points; the guess is 0 points! After you guess, you will be recorded. Highest score! "
def start_game (self): # define instance method
num = random.randint (1, 10)
score = 0
for i in range (3):
guest = int (raw_input ("Please enter a guessed number:"))
if guest == num:
if i == 0:
score + = 10
break
elif i == 1:
score + = 5
break
else:
score + = 3
break
else:
score = 0
print "Xiao Ming's score is% d"% score
if score> Game.top_score:
Game.top_score = score
@classmethod
def show_top_socre (cls): # Create a class method to display the highest score of the game
print "Show top grade:% s"% cls.top_score
# View game help
Game.show_help ()
# Create game object
xming = Game (‘小 明’)
# Start playing games
xming.start_game ()
# View historical highest score
Game.show_top_socre ()
Example summary:
1, instance method: the method needs to access the instance properties.
2. Class method: ‘only’ access to class attributes inside the method
3. Static method: Inside the method, you don't need to access instance attributes and class attributes
4. Inside the method, you need to access both the instance properties and the class properties, which can be defined as instance methods.
Object-oriented Python