1.面向对象设计(OOD):将一类具体事物的数据和动作整合到一起
2.面向对象编程(OOP):用定义类和实例或者对象方法实现面向对象的设计
3.类属性:数据属性 函数属性(实例只有属性对象 无函数属性 它调用类函数属性)
########################面向对象############### 1.面向对象设计(OOD):将一类具体事物的数据和动作整合到一起 2.面向对象编程(OOP):用定义类和实例或者对象方法实现面向对象的设计 语法 类名首写字母大写 类数据属性 类函数属性(方法) python为类内置的特殊属性 类名.__dic__查看类属性 类名.__name__# 类的名字(字符串) 类名.__doc__# 类的文档字符串 类名.__base__# 类的第一个父类(在讲继承时会讲) 类名.__bases__# 类所有父类构成的元组(在讲继承时会讲) 类名.__dict__# 类的字典属性 类名.__module__# 类定义所在的模块 类名.__class__# 实例对应的类(仅新式类中) 类的特殊属性(了解即可) 经典类 class 类名: x = 1 # 类数据属性 def __init__(self): # self实例化对象自己本身 pass def func(self): # 函数属性 pass 新式类 class 类名(object): def __init__(self): pass def func(self): pass
#!/usr/bin/python env # encoding: utf-8 class People: # 类名 country = 'China' # 类数据属性 def __init__(self, name, gender, food): # 初始化函数 self.name = name self.gender = gender self.food = food def eat(self): # 类函数属性 print("%s eat %s" % (self.name, self.food)) return "eat" def dark(): print("%s dark %s" % (self.name, self.food)) def sex(self): print("%s gender is %s" % (self.name, self.gender)) p1 = People("lisi", "man", "apple") # 实例化的对象 实例属性只有数据属性 没有函数属性 print(p1.__dict__) # 查看实例属性 {'food': 'apple', 'name': 'lisi', 'gender': 'man'} # p1.dark() # 报错 TypeError: dark() takes 0 positional arguments but 1 was given p1.eat() # self = p1实例化对象 People.eat(p1) # 相当于 p1.eat()
4.类 和 实例属性增删改查
类属性操作
#!/usr/bin/python env # encoding: utf-8 # 类属性增删改查 class People: # 类名 country = 'China' # 类数据属性 def __init__(self, name, gender, food): # 初始化函数 self.name = name self.gender = gender self.food = food def eat(self): # 类函数属性 print("%s eat %s" % (self.name, self.food)) return "eat" def dark(): print("%s dark %s" % (self.name, self.food)) def sex(self): print("%s gender is %s" % (self.name, self.gender)) p1 = People("lisi", "man", "apple") # 实例化的对象 实例属性只有数据属性 没有函数属性 # 查看类属性 print(People.country) # 修改类属性 People.country = "Chinese" print(People.country) # 添加类属性 People.sex1 = "man" print(People.sex1) # man print(p1.sex1) # man # 删除类属性 del People.sex1 # print(People.sex1) # AttributeError: type object 'People' has no attribute 'sex1' # 函数属性操作 def dark(): print("dark") People.dark=dark People.dark() # dark
实例属性操作
#!/usr/bin/python env # encoding: utf-8 # 实例属性增删改查 实例属性只有数据属性 无函数属性 class People: # 类名 country = 'China' # 类数据属性 def __init__(self, name, gender, food): # 初始化函数 self.name = name self.gender = gender self.food = food def eat(self): # 类函数属性 print("%s eat %s" % (self.name, self.food)) return "eat" def dark(): print("%s dark %s" % (self.name, self.food)) def sex(self): print("%s gender is %s" % (self.name, self.gender)) p1 = People("lisi", "man", "apple") # 实例化的对象 实例属性只有数据属性 没有函数属性 # 查看 print(p1.country) # 修改 p1.country = "Chinese" print(p1.country) def dark(): print("dark") p1.dark=dark p1.dark() # dark # 添加 p1.age = "12" print(p1.age) # 12 # 删除 del p1.age print(p1.age) # AttributeError: 'People' object has no attribute 'age'
5.对象和实例属性
# 对象和实例属性 class People: # 类名 country = 'China' # 类数据属性 def __init__(self, name, gender, food): # 初始化函数 self.name = name self.gender = gender self.food = food def eat(self): # 类函数属性 print("%s eat %s" % (self.name, self.food)) return "eat" def dark(): print("%s dark %s" % (self.name, self.food)) def sex(self): print("%s gender is %s" % (self.name, self.gender)) p1 = People("lisi", "man", "apple") # 实例化的对象 实例属性只有数据属性 没有函数属性 p1.country = "Chinese" print(p1.country) # Chinese print(People.country) # China country = "Chinese" class China: country = 'China' l = [1,2,3] def __init__(self, name): self.name = name print('country:', country) def eaat(self): pass # "."的调用和类 实例属性有关 不用点和它们没关系 print(China.__dict__) # {'country': 'China', 'eaat': <function China.eaat at 0x013DCDB0>, '__dict__': <attribute '__dict__' of 'China' objects>, '__init__': <function China.__init__ at 0x013DCD68>, '__doc__': None, '__module__': '__main__', '__weakref__': <attribute '__weakref__' of 'China' objects>} c = China('zhangsan') # __init__里面打印 country: Chinese print(c.__dict__) # {'name': 'zhangsan'} print("----------") # 注意 赋值是新增属性,不是更改属性 # c.l = [2,3] # print(c.l) # [2,3] # print(China.l) # [1, 2, 3] # c.l.append(4) # print(c.l) # [2, 3,4] # print(China.l) # [1, 2, 3] print(c.l) print(China.l) c.l.append(4) print(c.l) print(China.l) # 显示 # [1, 2, 3] # [1, 2, 3] # [1, 2, 3, 4] # [1, 2, 3, 4]