#coding=utf-8
#构造方法(当一个对象呗创建,会立即调用构造方法)
##########################################################################
class FooBar:
#定义类使用驼峰式(英文单词首字母大写)
def __init__(
self):
#定义函数使用小写字母
self.x=
1 #初始化赋值
f=FooBar()
#创建一个对象
print f.x
# 1
##########################################################################
class FoolBar:
def __init__(
self,value=
2):
self.somevar=value
#默认值,初始化
f=FoolBar()
# 2
print f.somevar
##########################################################################
class A:
def hello(
self):
print 'Hello,A'
class B(A):
pass
a=A()
#创建一个A类的对象a
b=B()
#创建一个B类的对象b
a.hello()
#Hello,A
b.hello()
#Hello,A
##########################################################################
class A:
def hello(
self):
print 'Hello,A'
class B(A):
def hello(
self):
print 'Hello,B'
a=A()
b=B()
a.hello()
#Hello,A
b.hello()
#Hello,B
##########################################################################
class Bird:
__metaclass__ =
type
def __init__(
self):
self.hungry=
True
def eat(
self):
if self.hungry:
print 'Gugu.'
self.hungry=
False
else:
print 'No thanks.'
class SongBird(Bird):
def __init__(
self):
super(SongBird
,self).
__init__()
#返回一个超类对象
self.sound=
'Bugubugu...'
def sing(
self):
print self.sound
sb=SongBird()
sb.sing()
#Bugubugu...
sb.eat()
#Gugu.
sb.eat()
#No thanks.
######################################################################
转载请注明原文地址: https://ju.6miu.com/read-35199.html