python类方法继承其他函数变量_Python类,域,方法,对象,继承

类和对象: 是面向对象编程的两个主要方面,类创建一个新类型,而对象这个类的实例。。

域: 属于一个对象或类的变量被称为域。域有两种类型: 属于每个实例(类的对象)或属于类本身。它们分别被称为实例变量和类变量。

方法: 对象也可以使用属于类的函数来具有功能。这样的函数被称为类的方法。

属性: 域和方法可以合称为类的属性。

类使用class关键字创建。类的域和方法被列在一个缩进块中。

类的方法与普通的函数只有一个特别的区别——它们必须有一个额外的第一个参数名称self,但是在调用这个方法的时候你不为这个参数赋值,这个特别的变量指对象本身。这也意味着如果你有一个不需要参数的方法,你还是得给这个方法定义一个self参数。

1.创建一个类

#!/usr/bin/python

# Filename: simplestclass.py

class Person:

pass # An empty block

p = Person()

print (p)

2.使用对象的方法

#!/usr/bin/python

# Filename: method.py

class Person:

def sayHi(self):

print 'Hello, how are you?'

p = Person()

p.sayHi()

# This short example can also be written as Person().sayHi()

这里我们看到了self的用法。注意sayHi方法没有任何参数,但仍然在函数定义时有self。

3.__init__方法

__init__方法在类的一个对象被建立时,马上运行。这个方法可以用来对你的对象做一些你希望的初始化 。注意,这个名称的开始和结尾都是双下划线。

#!/usr/bin/python

# Filename: class_init.py

class Person:

def __init__(self, name):

self.name = name

def sayHi(self):

print 'Hello, my name is', self.name

p = Person('Swaroop')

p.sayHi()

# This short example can also be written as Person('Swaroop').sayHi()

最重要的是,我们没有专门调用__init__方法,只是在创建一个类的新实例的时候,把参数包括在圆括号内跟在类名后面,从而传递给__init__方法。这是这种方法的重要之处。

4.使用类与对象的变量

有两种类型的域 ——类的变量和实例(对象的)变量,它们根据是类还是实例(对象)拥有这个变量而区分。

类的变量: 由一个类的所有实例(对象)共享使用。只有一个类变量的拷贝,所以当某个对象对类的变量做了改动的时候,这个改动会反映到所有其他的实例上。

实例(对象的)变量: 由类的每个实例(对象)拥有。因此每个对象有自己对这个域的一份拷贝,即它们不是共享的,在同一个类的不同实例中,虽然对象的变量有相同的名称,但是是互不相关的。

#!/usr/bin/python

# Filename: objvar.py

class 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 'I am the only person here.'

else:

print 'We have %d persons here.' % Person.population

swaroop = Person('Swaroop')

swaroop.sayHi()

swaroop.howMany()

kalam = Person('Abdul Kalam')

kalam.sayHi()

kalam.howMany()

swaroop.sayHi()

swaroop.howMany()

运行结果

[root@host 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.

I am the last one.

就如同__init__方法一样,还有一个特殊的方法__del__,它在对象消逝的时候被调用。对象消逝即对象不再被使用,它所占用的内存将返回给系统作它用。在这个方法里面,我们只是简单地把Person.population减1。

这样就有一个惯例,如果某个变量只想在类或对象中使用,就应该以单下划线前缀。而其他的名称都将作为公共的,可以被其他类/对象使用。记住这只是一个惯例,并不是Python所要求的(与双下划线前缀不同)

5.继承: 面向对象的编程带来的主要好处之一是代码的重用,实现这种重用的方法之一是通过继承机制。继承完全可以理解成类之间的类型和子类型关系。

#!/usr/bin/python

# Filename: inherit.py

class 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.salary

class 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.marks

t = Teacher('Mrs. Shrividya', 40, 30000)

s = Student('Swaroop', 22, 75)

print # prints a blank line

members = [t, s]

for member in members:

member.tell() # works for both Teachers and Students

运行结果

[root@host 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"

[root@host python]#

为了使用继承,我们把基本类的名称作为一个元组跟在定义类时的类名称之后。然后,我们注意到基本类的__init__方法专门使用self变量调用,这样我们就可以初始化对象的基本类部分。这一点十分重要——Python不会自动调用基本类的constructor,你得亲自专门调用它。我们还观察到我们在方法调用之前加上类名称前缀,然后把self变量及其他参数传递给它。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值