- 类
Class Cat:
#初始化方法,创建对象时会自动调用
def __init__(self, color, weight, weiba):
self.color = color
self.weight = weight
self.weiba = weiba
#类中的方法,都需要加self参数
def eat(self):
print("吃鱼")
xiaohuamao = Cat("white", 5, "有尾巴")
xiaohuamao.eat()
- Demo1
#conding=utf-8
class SweetPotato:
def __init__(self):
self.cookLevel = 0
self.cookString = "生的"
self.condiments = []
def cook(self,times):
self.cookLevel += times
if self.cookLevel > 0 and self.cookLevel <= 3:
self.cookString = "生的"
elif self.cookLevel > 3 and self.cookLevel <= 5:
self.cookString = "半生不熟"
elif self.cookLevel > 5 and self.cookLevel <= 8:
self.cookString = "熟了"
elif self.cookLevel > 8:
self.cookString = "烤焦了"
def addCondiments(self,temp):
self.condiments.append(temp)
def __str__(self):
if len(self.condiments) == 0:
s = "地瓜的cookLever为:%s,地瓜的生熟程度为:%s"%(str(self.cookLevel),self.cookString)
else:
zuoliao = ""
for i,condiment in enumerate(self.condiments):
if i == len(self.condiments) - 1:
zuoliao = zuoliao + condiment
else:
zuoliao = zuoliao + condiment + ","
s = "地瓜的cookLever为:%s,地瓜的生熟程度为:%s,添加的佐料为:%s"%(str(self.cookLevel),self.cookString,zuoliao)
return s
potato = SweetPotato()
print(potato)
potato.cook(1)
potato.addCondiments("胡椒")
print(potato)
potato.cook(3)
potato.addCondiments("辣椒")
print(potato)
potato.cook(2)
potato.addCondiments("酱油")
print(potato)
potato.cook(4)
potato.addCondiments("醋")
print(potato)
- Demo2
#coding=utf-8
class Home:
def __init__(self,area):
self.area = area
self.items = []
def addItems(self,item):
itemArea = item.getArea();
self.area -= itemArea
if self.area >= 0:
print("添加家具成功")
self.items.append(item)
else:
print("添加家具失败,空间不够了")
self.area += itemArea
def __str__(self):
if len(self.items) > 0:
s = "当前家里剩余的面积为:" + str(self.area) + "......家里的家具为:"
for temp in self.items:
s += temp.getName() + ","
return s.strip(",")
else:
s = "当前家里剩余的面积为:" + str(self.area) + "......家里没有家具"
return s
class Bed:
def __init__(self,name,area):
self.name = name
self.area = area
def getName(self):
return self.name
def getArea(self):
return self.area
def __str__(self):
s = "床的名字为:%s,床的面积为:%s"%(self.name,str(self.area))
return s
home = Home(100)
print(home)
bed = Bed("大床",15)
print(bed)
home.addItems(bed)
print(home)
bed2 = Bed("婴儿床",150)
print(bed2)
home.addItems(bed2)
print(home)
- 私有属性和私有方法
在属性和方法前面加两个下划线,代表私有属性和私有方法。私有属性和私有方法只能在类内部使用,在类外部不能使用。在类外部通过本类对象来调用私有属性和私有方法也是不行的(和java的区别)。
#coding=utf-8
class Person:
def __init__(self):
#这表示私有属性,外部不能直接访问
self.__name = None
self.__age = None
def setName(self,name):
self.__name = name
def getName(self):
return self.__name
def setAge(self,age):
self.__age = age
def getAge(self):
return self.__age
#这是私有方法,外部不能直接调用
#Python把私有方法的名字改了,改成_Person__test1()
def __test1(self):
print("test1======")
#这个方法暴露给外部,来调用私有方法
def test2(self):
self.__test1()
print("test2======")
person = Person()
person.setName("Tom")
person.setAge(18)
print(person.getName() + "......" + str(person.getAge()))
person.test2()
#这样可以调用私有方法
person._Person__test1()
- 多继承
多继承中,两个父类有同名方法,子类调用该方法,则执行第一个父类里面的方法。(由mro算法决定)
- 多态
Python是弱类型语言,多态表现并不明显
- 类属性、类方法和静态方法
#coding=utf-8
class Test(object):
#类属性
num = 0
#实例方法
def __init__(self):
#实例属性
self.age = 10
#实例方法
def abc(self):
print("实例方法")
#类方法(主要作用是用来操作类属性的,比较安全,和静态方法区别开来)
@classmethod
def setNum(cls,num):
cls.num = num
#静态方法(完成一个相对独立的功能)
@staticmethod
def testStatic():
print("test static method")
t = Test()
t.abc()
print(Test.num)
#通过类方法来操作类属性,这样比较安全
Test.setNum(20)
print(Test.num)
t.testStatic()
Test.testStatic()