Python 面向对象编程
来源:互联网 发布:计算机美工专业 编辑:程序博客网 时间:2024/05/29 16:14
# python 中类的使用, 对象的使用, 属性和类属性的区别
class Fruit:
price = 0
def __init__(self):
self.color = "red"
zone = "china"
if __name__ == '__main__':
print Fruit.price
apple = Fruit()
print apple.color
Fruit.price = Fruit.price + 10
print "apple price " + str(apple.price)
banana = Fruit()
print "banana 's price " + str(banana.price)
Python 的属性分为实例属性和静态属性, 实例属性是self 为前缀的属性, __init__ 方法就是python的构造函数, 如果__init__方法中定义的变量没有使用self 作为前缀说明, 则该变量只是普通的局部变量, 类中其他方法定义的变量也只是局部变量。
默认的__color 属性是私有的属性, 如果访问私有属性的话则会出现错误 :
# -*- coding:utf8 -*-
__author = 'shencheng'
# python 中类的使用, 对象的使用, 属性和类属性的区别
class Fruit:
price = 0
__color="red"
def __init__(self):
self.color = "red"
zone = "china"
if __name__ == '__main__':
print Fruit.__color
print Fruit.price
apple = Fruit()
print apple.color
Fruit.price = Fruit.price + 10
print "apple price " + str(apple.price)
banana = Fruit()
print "banana 's price " + str(banana.price)
上面代码会出现问题:
Traceback (most recent call last):
File "C:/Users/shencheng/PycharmProjects/Widget/baselan/langbase.py", line 14, in <module>
print Fruit.__color
AttributeError: class Fruit has no attribute '__color'
# python 中类的使用, 对象的使用, 属性和类属性的区别
class Fruit:
price = 0
# __color="red"
def __init__(self):
self.__color = "red"
class Apple(Fruit):
pass
if __name__ == '__main__':
fruit = Fruit()
apple = Apple()
print Apple.__bases__
print apple.__dict__
print apple.__module__
# python 中类的使用, 对象的使用, 属性和类属性的区别
class Fruit:
price = 0
def __init__(self):
self.__color = "red"
def getColor(self):
print self.__color
@staticmethod
def getPrice():
print Fruit.price
def __getPrice(self):
Fruit.price = Fruit.price + 10
count = staticmethod(__getPrice())
if __name__ == '__main__':
apple = Fruit()
apple.getColor()
Fruit.cout()
banana = Fruit()
Fruit.cout()
Fruit.getPrice()
# __del__的使用方法 , 会在程序结束的时候做一次析构的操作
class Fruit:
def __init__(self, color):
self.__color = color
print self.__color
def __del__(self):
self.__color = ""
print "free"
def grow(self):
print "grow"
if __name__ == '__main__':
color = "red"
f = Fruit(color)
f.grow()
对于C++ 语言析构函数是必须的,程序员需要为对象分配内存空间, 同事也要手动释放内存, 使用python 编写程序不考虑后台的内存管理, 直接面对程序逻辑
#由于类的__init__ 只能初始化一次, 所以两个雷的话只有一个可以被调用
class Fruit(object):
def __init__(self):
print "init fruit"
def grow(self):
print "grow"
class Vegetable(object):
def __init__(self):
print "init vegetable"
def plant(self):
print "plant"
class WaterMelen(Fruit, Vegetable):
pass
if __name__ == '__main__':
w = WaterMelen()
w.grow()
w.plant()
class Fruit:
price = 0
def __init__(self):
self.color = "red"
zone = "china"
if __name__ == '__main__':
print Fruit.price
apple = Fruit()
print apple.color
Fruit.price = Fruit.price + 10
print "apple price " + str(apple.price)
banana = Fruit()
print "banana 's price " + str(banana.price)
Python 的属性分为实例属性和静态属性, 实例属性是self 为前缀的属性, __init__ 方法就是python的构造函数, 如果__init__方法中定义的变量没有使用self 作为前缀说明, 则该变量只是普通的局部变量, 类中其他方法定义的变量也只是局部变量。
默认的__color 属性是私有的属性, 如果访问私有属性的话则会出现错误 :
# -*- coding:utf8 -*-
__author = 'shencheng'
# python 中类的使用, 对象的使用, 属性和类属性的区别
class Fruit:
price = 0
__color="red"
def __init__(self):
self.color = "red"
zone = "china"
if __name__ == '__main__':
print Fruit.__color
print Fruit.price
apple = Fruit()
print apple.color
Fruit.price = Fruit.price + 10
print "apple price " + str(apple.price)
banana = Fruit()
print "banana 's price " + str(banana.price)
上面代码会出现问题:
Traceback (most recent call last):
File "C:/Users/shencheng/PycharmProjects/Widget/baselan/langbase.py", line 14, in <module>
print Fruit.__color
AttributeError: class Fruit has no attribute '__color'
# python 中类的使用, 对象的使用, 属性和类属性的区别
class Fruit:
price = 0
# __color="red"
def __init__(self):
self.__color = "red"
class Apple(Fruit):
pass
if __name__ == '__main__':
fruit = Fruit()
apple = Apple()
print Apple.__bases__
print apple.__dict__
print apple.__module__
# python 中类的使用, 对象的使用, 属性和类属性的区别
class Fruit:
price = 0
def __init__(self):
self.__color = "red"
def getColor(self):
print self.__color
@staticmethod
def getPrice():
print Fruit.price
def __getPrice(self):
Fruit.price = Fruit.price + 10
count = staticmethod(__getPrice())
if __name__ == '__main__':
apple = Fruit()
apple.getColor()
Fruit.cout()
banana = Fruit()
Fruit.cout()
Fruit.getPrice()
# __del__的使用方法 , 会在程序结束的时候做一次析构的操作
class Fruit:
def __init__(self, color):
self.__color = color
print self.__color
def __del__(self):
self.__color = ""
print "free"
def grow(self):
print "grow"
if __name__ == '__main__':
color = "red"
f = Fruit(color)
f.grow()
对于C++ 语言析构函数是必须的,程序员需要为对象分配内存空间, 同事也要手动释放内存, 使用python 编写程序不考虑后台的内存管理, 直接面对程序逻辑
#由于类的__init__ 只能初始化一次, 所以两个雷的话只有一个可以被调用
class Fruit(object):
def __init__(self):
print "init fruit"
def grow(self):
print "grow"
class Vegetable(object):
def __init__(self):
print "init vegetable"
def plant(self):
print "plant"
class WaterMelen(Fruit, Vegetable):
pass
if __name__ == '__main__':
w = WaterMelen()
w.grow()
w.plant()
0 0
- python面向对象编程
- python 面向对象编程
- python面向对象编程
- python面向对象编程
- Python 面向对象编程
- Python面向对象编程
- Python 面向对象编程
- python--面向对象编程
- python面向对象编程
- python 面向对象编程
- Python:面向对象编程
- python面向对象编程
- Python面向对象编程
- python面向对象编程
- Python面向对象编程
- python 面向对象编程
- python 面向对象编程
- PYTHON 面向对象编程
- 从0开始学习 GitHub 系列之「08.如何发现优秀的开源项目」
- Python 文件操作
- 专业开发程序员下载不同版本的FireFox的开发版本 下载地址
- caffe︱ImageData层、DummyData层作为原始数据导入的应用
- Python 字符串操作
- Python 面向对象编程
- Qt 5.8 音频、文本转换引擎
- 一致性hash-java实现treemap版
- 【Oracle10g】Windows 8.1下如何成功安装Oracle 10g 亲试成功
- poj2155
- AS 自定义主题字体颜色和背景
- JS事件之DOM事件流
- 【STM32】STM32之DRV8834电机驱动(PWM方式)
- 设计模式学习笔记之策略模式