zoukankan      html  css  js  c++  java
  • python中的实例变量内容学习

    最近在学python中的类变量和实例变量内容,之前在学java的时候,也有了解过类变量和实例变量,具体可查看这个文章:https://www.cnblogs.com/baby-zhude/p/8011969.html

    总的来说就是在java中类变量也叫静态变量,也就是在变量前加了static 的变量;
    实例变量也叫对象变量,即没加static 的变量;
    区别在于:
       类变量和实例变量的区别在于:类变量是所有对象共有,其中一个对象将它值改变,其他对象得到的就是改变后的结果;而实例变量则属对象私有,某一个对象将其值改变,不影响其他对象;

    而在python中却又有点不一样了,虽然类变量仍然是所有实例对象共有的属性,但是其中一个对象将它的值改变后,其他对象得到的并不是改变后的结果;

    我们先将下面的代码输入到编译器中:

    class Vehicle():
    category = '?'
    def __init__(self, wheelcount, power): # 初始化类
    self.wheelcount = wheelcount # 定义实例变量wheelcount
    self.power = power # 定义实例变量power

    def drive(self, distance): # 定义drive方法
    try:self.totaldistance += distance # 判断实例变量totaldistance是否存在
    except: self.totaldistance = distance # 不存在就创建totaldistance这个实例变量,并将distance的值赋值给self.ttotaldistance
    return distance # 将distance的值返回给方法drive

    def gettotaldistance(self): # 定义gettotaldistance方法
    if not hasattr(self, 'totaldistance'): self.totaldistance=(); # 判断实例变量totaldistance是否存在,不存在就创建totaldistance这个实例变量,并将()赋值给该实例变量
    return self.totaldistance # 返回totaldistance这个实例变量的值赋值给gettotaldistance


    def dirp(iter): # 定义函数dirp
    return [i for i in dir(iter) if not i.startswith('__')] # 将dir(iter)的值中过滤掉前面带__的数据后,赋值给变量i,然后将i的值赋值给dirp函数


    hippomobile = Vehicle(2, '马车') # 创建Vehicle的实例对象hippomobile
    car = Vehicle(4, '小汽车') # 创建Vehicle的实例对象car

    print(dirp(car), dirp(hippomobile)) # 打印实例对象car和hippomobile中包含的方法和属性

    car.category = 'machine' # 将值machine赋值给实例对象car中的category属性
    print(car.category) # 打印实例对象car的category属性值
    print(hippomobile.category) # 打印实例对象hippomobile的category属性值

    car.oil = 0 # 为实例对象car定义一个oil属性
    truck = Vehicle(4, '卡车') # 创建Vehicle的实例对象truck
    print(truck.category) # 打印实例对象truck的category属性值
    print(dirp(car), dirp(truck)) # 打印实例对象car和truck中包含的方法和属性

    print(car.drive(100)) # 打印car的drive方法的值
    print(dirp(car)) # 打印实例对象car的方法和属性

    print(car.gettotaldistance()) # 打印car的gettotaldistance方法的值
    print(dirp(car)) # 打印实例对象car的方法和属性

    print(truck.gettotaldistance()) # 打印truck的gettotaldistance方法的值
    print(dirp(truck), dirp(hippomobile)) # 打印实例对象truck和hippomobile的方法和属性

    然后执行代码,查看结果:

    C:UsersAdministratorAppDataLocalProgramsPythonPython35-32python.exe C:/Users/Administrator/Desktop/mypython/class_Vehicle.py
    ['category', 'drive', 'gettotaldistance', 'power', 'wheelcount'] ['category', 'drive', 'gettotaldistance', 'power', 'wheelcount']
    machine
    ?
    ?
    ['category', 'drive', 'gettotaldistance', 'oil', 'power', 'wheelcount'] ['category', 'drive', 'gettotaldistance', 'power', 'wheelcount']
    100
    ['category', 'drive', 'gettotaldistance', 'oil', 'power', 'totaldistance', 'wheelcount']
    100
    ['category', 'drive', 'gettotaldistance', 'oil', 'power', 'totaldistance', 'wheelcount']
    ()
    ['category', 'drive', 'gettotaldistance', 'power', 'totaldistance', 'wheelcount'] ['category', 'drive', 'gettotaldistance', 'power', 'wheelcount']

    进程已结束,退出代码0

    可以看到,我们在Vehicle类中声明了一个category的类变量后,之后在实例对象car中将category的值变更为“machine”之后,其他对象的category属性的值并没有跟着发生变化。

  • 相关阅读:
    云计算和SOA何时走到了一起?
    MVP
    Mvp
    Technology Radar of thoughtworks
    JSF
    我们要积极学习互联网的用户体验
    Gwt
    数字的字符串处理
    C语言字符串函数大全(转自百度百科)
    树状数组
  • 原文地址:https://www.cnblogs.com/xzy186/p/14246946.html
Copyright © 2011-2022 走看看