程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python learning notes: classes and objects

編輯:Python
# Classes and objects
# object : There are properties ( features ) And the function / Method ( Behavior ), Is an instance of a class
# class : Keywords are class, Is a template for creating objects
class Human:
# Constructors , Initialization function
def __init__(self, breed, sex, height = '170'):# The default value is
# Define three attributes
self.breed = breed
self.sex = sex
self.height = height
# def __init__(self):
# # Define three attributes
# self.breed = None
# self.sex = None
# self.height = None
# Defined 3 A function
def study(self):
print(" Study ")
def eat(self):
print(" having dinner ")
def sleep(self):
print(" sleep ")
print(" One %s Blood type , Gender :%s, height :%s" % (self.breed, self.sex, self.height))
# The relationship between classes and objects : Class is the template for creating objects , Object is a cumulative instance
human = Human('A', ' Woman ')
# human.breed = 'A'
# human.sex = ' Woman '
# human.height = '170'
human.eat()
human.sleep()
human.study()
# Judgment function type()
print(type(human)) # Human type
print(type(human) == Human)
# isinstance Determine whether it is an instance ?
print(isinstance(human, Human)) # Parameters 1 It's the object , Parameters 2 Is a judged class


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved