类
类的实例方法
- 实例方法的第一个参数必须是”self”。
实例方法只能通过类实例进行调用,这时候”self”就代表这个类实例本身。通过”self”可以直接访问实例的属性,有了init方法,在创建实例的时候,就不能传入空的参数了,必须传入与init方法匹配的参数,但self不需要传。
class A(object):
# 类的初始化
def __init__(self, name):
self.name = name
print("init class A")
def hello(self):
print("hello {0}".format(self.name))
a = A("shan")
a.hello()
# 输出
init class A
hello shan
继承
继承
在Python中,同时支持单继承与多继承,实现继承之后,子类将继承父类的属性。一般语法如下:
class SubClassName(ParentClass1 [, ParentClass2, ...]):
class_suite
- 继承的例子
class Animal(object):
def __init__(self, name):
print("你现在正在初始化一个Animal")
def run(self):
print("Animal can run.")
class Bird(Animal): #继承Animal
def __init__(self):
print("bird")
def fly(self):
print("Bird can fly.")
class Cat(Animal): #继承Animal
def __init__(self, name, sex):
self.name = name
self.sex = sex
super(Cat, self).__init__(self.name)
print("我是一只猫,啦啦啦啦")
def jiao(self):
print("miao miao miao miao")
def run(self):
print("我是一只猫,会上树来会跑路.")
class BianYi(Cat, Bird): #继承Cat,Bird
pass
cat = Cat("mao", "man")
# 输出
你现在正在初始化一个Animal
我是一只猫,啦啦啦啦
类中的调用方法
class DbArgs(object):
# 只有类本身才可以调用
__host = str("1.1.1.1")
__port = str()
__username = str()
__password = str()
__dbname = str()
# 任何人可以调用
name = "ajing"
# 只能实例自己调用
_host = "asdlfjasdl"
def getHost(self):
return self.__host
def setHost(self, host):
self.__host = host
dbArgs = DbArgs()
print(dbArgs.getHost())
dbArgs.name = "就是要改你,怎么的"
print(dbArgs.name)
print(dbArgs._host)
# 输出
1.1.1.1
就是要改你,怎么的
asdlfjasdl
类的应用例子
- 本实例主要是结合类的初始化,实例化,也使用了之前学的打开文件和写文件。
import codecs
class Student(object):
def __init__(self, id, name, score): #初始化 id,name,score
self.id = id
self.name = name
self.score = score
class InitStduents():
def __init__(self): # 初始化中定义一个list,执行init方法,init方法中添加list数据
self.students = list()
self.init()
def init(self):
self.students.append(Student(1001, "aaa", 59))
self.students.append(Student(1002, "bbb", 96))
self.students.append(Student(1003, "ccc", 87))
self.students.append(Student(1004, "ddd", 89))
self.students.append(Student(1005, "eee", 33))
self.students.append(Student(1006, "fff", 85))
self.students.append(Student(1007, "ggg", 78))
self.students.append(Student(1008, "hhh", 97))
self.students.append(Student(1009, "iii", 31))
self.students.append(Student(1010, "jjj", 93))
def sort(self): # 类中的sort方法(list的排序)
return sorted(self.students, key=lambda student: student.score)
def writeFile(self, newStudents): # 类中writeFile方法(把内容写进文件)
with codecs.open("sortStudent.txt", "w")as f:
for i in newStudents:
f.write("id = {0}".format(i.id))
f.write("\t")
f.write("name = {0}".format(i.name))
f.write("\t")
f.write("score = {0}".format(i.score))
f.write("\n")
def main():
students = InitStduents()
newStudents = students.sort()
students.writeFile(newStudents)
if __name__ == "__main__":
main()
# 查看newStudents
id = 1009 name = iii score = 31
id = 1005 name = eee score = 33
id = 1001 name = aaa score = 59
id = 1007 name = ggg score = 78
id = 1006 name = fff score = 85
id = 1003 name = ccc score = 87
id = 1004 name = ddd score = 89
id = 1010 name = jjj score = 93
id = 1002 name = bbb score = 96
id = 1008 name = hhh score = 97
- 使用类的方法编写之前的求阶乘和
class JinCinCount(object):
def __init__(self, n):
self.n = n
def jc(self, n):
result = 1
if n == 0:
return result
else:
for i in range(1, n+1):
result *= i
return result
def count(self):
count = 0
for i in range(0, int(self.n) + 1):
count += self.jc(i)
print("count = {0}".format(count))
def main():
n = input("Please inpurt a number: ")
jinCinCount = JinCinCount(int(n))
jinCinCount.count()
if __name__ == "__main__":
main()
# 输出
Please inpurt a number: 5
count = 154