imtoken免费币

类和对象怎么在python中使用

2023-10-03 05:42:46      点击:039

这篇文章给大家介绍类和对象怎么在python中使用,类和内容非常详细,对象感兴趣的中使小伙伴们可以参考借鉴,希望对大家能有所帮助。类和

类的对象定义

在定义类或者它的函数的时候,如果没有参数的中使话,需要把参数写为self,类和这样在外部调用的对象时候能清楚调用的是哪个实例的函数。这个self表示的中使是当前的实例,与C++中的类和self和Java中的this是一样的。

空类

#!/usr/bin/python# Filename: simplestclass.pyclass Person:  pass # An empty blockp = Person()print p

我们使用class语句后跟类名,对象创建了一个新的中使类。这后面跟着一个缩进的类和语句块形成类体。在这个例子中,对象我们使用了一个空白块,中使它由pass语句表示。这样编译的时候就会跳过。这是一个空类。

接下来,我们使用类名后跟一对圆括号来创建一个对象/实例。因为这个类不需要任何的参数,所以创建的时候也不需要参数。

$ python simplestclass.py
<__main__.Person instance at 0xf6fcb18c>

打印这个对象,我们可以看到这个对象所属的类,以及它的内存地址。

__init__函数

在上面的例子中,我们没有写任何东西,但是还是会有一个默认的构造函数。在python中也有类似构造函数的东西,就是__init__函数。在创建对象的时候会自动调用这个函数。

#!/usr/bin/python# Filename: class_init.pyclass Person:  def __init__(self, name):    self.name = name  def sayHi(self):    print 'Hello, my name is', self.namep = Person('Swaroop')p.sayHi()

输出:

$ python class_init.py
Hello, my name is Swaroop

当新建一个person对象时,因为__init__中需要有name参数,所以需要提供一个参数。可以看到这个类中有一个自己的域,在构造的时候把name的值赋给对象里的name。
可以看到,sayHi并不需要参数,但是在定义函数时仍然要有参数self

__del__函数

就如同init方法一样,还有一个特殊的方法del,它在对象消逝的时候被调用。相当于析构函数。对象消逝即对象不再被使用,它所占用的内存将返回给系统作它用。但是很难保证这个方法究竟在什么时候运行。如果你想要指明它的运行,你就得使用del语句。代码例子和下面的在一起。

使用类的变量

Python中所有的类成员(包括数据成员)都是公共的,所有的方法都是有效的 。

只有一个例外:如果你使用的数据成员名称以双下划线前缀比如__privatevar,Python的名称管理体系会有效地把它作为私有变量。

这样就有一个惯例,如果某个变量只想在类或对象中使用,就应该以单下划线前缀。而其他的名称都将作为公共的,可以被其他类/对象使用。

#!/usr/bin/python# Filename: objvar.pyclass Person:  '''Represents a person.'''  population = 0  def __init__(self, name):    '''Initializes the person's data.'''    self.name = name    print '(Initializing %s)' % self.name    # When this person is created, he/she    # adds to the population    Person.population += 1  def __del__(self):    '''I am dying.'''    print '%s says bye.' % self.name    Person.population -= 1    if Person.population == 0:      print 'I am the last one.'    else:      print 'There are still %d people left.' % Person.population  def sayHi(self):    '''Greeting by the person.    Really, that's all it does.'''    print 'Hi, my name is %s.' % self.name  def howMany(self):    '''Prints the current population.'''    if Person.population == 1:      print 'There is nobody here.'    else:      print 'We have %d persons here.' % Person.populationswaroop = Person('Swaroop')swaroop.sayHi()swaroop.howMany()kalam = Person('Abdul Kalam')kalam.sayHi()kalam.howMany()swaroop.sayHi()swaroop.howMany()

输出:

$ python objvar.py
(Initializing Swaroop)
Hi, my name is Swaroop.
I am the only person here.
(Initializing Abdul Kalam)
Hi, my name is Abdul Kalam.
We have 2 persons here.
Hi, my name is Swaroop.
We have 2 persons here.
Abdul Kalam says bye.
There are still 1 people left.
Swaroop says bye.
There is nobody here.

这是一个很长的例子。这里,population属于Person类,因此是一个类的变量。name变量属于对象(它使用self赋值)因此是对象的变量。

观察可以发现init方法用一个名字来初始化Person实例。在这个方法中,我们让population增加1,这是因为我们增加了一个人。同样可以发现,self.name的值根据每个对象指定,这表明了它作为对象的变量的本质。

记住,你只能使用self变量来引用同一个对象的变量和方法。这被称为 属性引用。

当一个对象被清除的时候,del被调用,人数减少1并输出谁走了。一开始Abdul Kalam被自动清除了,后来Swaroop也被自动清除了。清除的顺序应该跟对象闲置的时间有关,当闲置时间达到某个值就清除掉。

继承

python的继承和C++还有JAVA没有什么不同,就是构造函数和其他函数里记得要在参数里加上self。函数的重载也是和C++,JAVA一样的。

例子:

#!/usr/bin/python# Filename: inherit.pyclass SchoolMember:  '''Represents any school member.'''  def __init__(self, name, age):    self.name = name    self.age = age    print '(Initialized SchoolMember: %s)' % self.name  def tell(self):    '''Tell my details.'''    print 'Name:"%s" Age:"%s"' % (self.name, self.age),class Teacher(SchoolMember):  '''Represents a teacher.'''  def __init__(self, name, age, salary):    SchoolMember.__init__(self, name, age)    self.salary = salary    print '(Initialized Teacher: %s)' % self.name  def tell(self):    SchoolMember.tell(self)    print 'Salary: "%d"' % self.salaryclass Student(SchoolMember):  '''Represents a student.'''  def __init__(self, name, age, marks):    SchoolMember.__init__(self, name, age)    self.marks = marks    print '(Initialized Student: %s)' % self.name  def tell(self):    SchoolMember.tell(self)    print 'Marks: "%d"' % self.markst = Teacher('Mrs. Shrividya', 40, 30000)s = Student('Swaroop', 22, 75)print # prints a blank linemembers = [t, s]for member in members:  member.tell() # works for both Teachers and Students

输出:

$ python inherit.py
(Initialized SchoolMember: Mrs. Shrividya)
(Initialized Teacher: Mrs. Shrividya)
(Initialized SchoolMember: Swaroop)
(Initialized Student: Swaroop)

Name:"Mrs. Shrividya" Age:"40" Salary: "30000"
Name:"Swaroop" Age:"22" Marks: "75"

schoolmenber是父类,学生和老师是子类,继承的方法为在类定义中把参数设为schoolmenber。可以看到,当我们继承一个类的时候,首先调用的是父类的构造函数,然后才是子类的。

关于类和对象怎么在python中使用就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

佛系 打要工专用电脑壁纸
二次元新年动漫动态电脑壁纸