1、面向对象
2、封装特性
实例化对象巩固练习:
class People:
def __init__(self, name, age, gender):
self.name = name
self.age = age
self.gender = gender
def shopping(self):
print(f'{self.name},{self.age}岁,{self.gender},去西安赛格购物广场购物 ')
def learning(self):
print(f'{self.name},{self.age}岁,{self.gender},在西部开源学习')
p1 = People('小明', 18, '男')
p2 = People('小王', 22, '男')
p3 = People('小红', 10, '女')
p1.shopping()
p2.shopping()
p3.learning()
3、继承特性
4、重写继承类方法
class Student:
"""父类Student"""
def __init__(self, name, age):
self.name = name
self.age = age
def learning(self):
print(f'{self.name}正在学习')
def choice_course(self):
print('正在选课中'.center(50, '*'))
class MathStudent(Student):
"""MathStudent的父类是Student"""
def choice_course(self):
super(MathStudent, self).choice_course()
info = """
课程表
1. 高等数学
2. 线性代数
3. 概率论
"""
print(info)
m1 = MathStudent("粉条博士", 8)
m1.choice_course()
s1 = Student("粉条博士", 8)
s1.choice_course()
5、链表和数组的区别
数组增加 数组删除 数组修改 链表增加 链表删除 链表查看修改需要遍历
封装练习题 第一步:封装
class listNode:
def __init__(self,val=None,next=None):
self.val=val
self.next=next
def travel(self,head):
while head.next:
print(head.next.val,end=',')
head = head.next
def create_l1():
l1 = listNode()
node1 = listNode(val=2)
node2 = listNode(val=4)
node3 = listNode(val=3)
l1.next = node1
node1.next = node2
node2.next = node3
return l1
def create_l2():
l2 = listNode()
node1 = listNode(val=5)
node2 = listNode(val=6)
node3 = listNode(val=4)
l2.next = node1
node1.next = node2
node2.next = node3
return l2
l1 = create_l1()
l2 = create_l2()
l1.travel(l1)
print()
l1.travel(l2)
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def travel(self, head):
"""遍历链表里面的每一个元素"""
while head:
print(head.val, end=',')
head = head.next
def create_l1():
l1 = ListNode()
node1 = ListNode(val=2)
node2 = ListNode(val=4)
node3 = ListNode(val=3)
l1.next = node1
node1.next = node2
node2.next = node3
return l1.next
def create_l2():
l2 = ListNode()
node1 = ListNode(val=5)
node2 = ListNode(val=6)
node3 = ListNode(val=4)
l2.next = node1
node1.next = node2
node2.next = node3
return l2.next
def addTwoNumbers(l1: ListNode, l2: ListNode) -> ListNode:
res = 0
l3 = ListNode()
cur = l3
while(l1 or l2):
if(l1):
res += l1.val
l1 = l1.next
if(l2):
res += l2.val
l2 = l2.next
l3.next = ListNode(res%10)
l3 = l3.next
res //= 10
if res == 1:
l3.next = ListNode(1)
return cur.next
if __name__ == '__main__':
l1 = create_l1()
l2 = create_l2()
l3 = addTwoNumbers(l1, l2)
l3.travel(l3)
练习题二:
6、多继承
a找b 如果b没有找c(先找同一层的)再找d
7、私有属性私有方法
类的外部不能访问和操作如下: 类的内部可以访问: 私有方法是以双下划线开头得,可以在类的内部访问,不能在类的外部访问
可以在类的内部访问
结果得出120
报错!! 类的外部包括子类也不能运行
8、多态特性
不同的对象调用同一个方法时结果是不同的,就叫做多态
9、栈的封装
栈的封装
class Stack(object):
"""栈的封装[1, 2, 3, 4]"""
def __init__(self):
self.stack = []
def push(self, value):
"""入栈"""
self.stack.append(value)
print(f"入栈元素为{value}")
def pop(self):
"""出栈"""
if self.is_empty():
raise Exception("栈为空")
item = self.stack.pop()
print(f"出栈元素为{item}")
return item
def is_empty(self):
"""判断栈是否为空"""
return len(self.stack) == 0
def top(self):
"""返回栈顶元素"""
if self.is_empty():
raise Exception("栈为空")
return self.stack[-1]
def __len__(self):
"""魔术方法, len(object)自动执行的方法"""
return len(self.stack)
if __name__ == '__main__':
stack = Stack()
stack.push(1)
stack.push(2)
stack.push(3)
print(len(stack))
stack.pop()
print(stack.is_empty())
print(stack.top())
10、队列的封装
class Queue(object):
"""
队列的封装
1. 列表的左侧队尾
2. 列表的右侧队头
"""
def __init__(self):
self.queue = []
def enqueue(self, value):
"""入队"""
self.queue.insert(0, value)
print("入队元素为:", value)
def dequeue(self):
"""出队"""
if self.is_empty():
raise Exception("队列为空")
item = self.queue.pop()
print("出队元素:", item)
return item
def __len__(self):
"""获取队列的长度"""
return len(self.queue)
def first(self):
"""获取队头元素"""
if self.is_empty():
raise Exception("队列为空")
return self.queue[-1]
def last(self):
"""获取队尾元素"""
if self.is_empty():
raise Exception("队列为空")
return self.queue[0]
def is_empty(self):
"""判断队列是否为空"""
return len(self.queue) == 0
if __name__ == '__main__':
queue = Queue()
queue.enqueue(1)
queue.enqueue(2)
queue.enqueue(3)
print(queue.is_empty())
queue.dequeue()
print(queue.first())
print(queue.last())
11、二叉树的封装
left:左子树 right:右子树
"""
二叉树:
https://www.cnblogs.com/polly333/p/4740355.html
"""
class Node(object):
"""节点类"""
def __init__(self, val=None, left=None, right=None):
self.val = val
self.left = left
self.right = right
class BinaryTree(object):
"""封装二叉树"""
def __init__(self, root):
self.root = root
def pre_travel(self, root):
"""先序遍历: 根左右"""
if (root != None):
print(root.val)
self.pre_travel(root.left)
self.pre_travel(root.right)
def in_travel(self, root):
"""中序遍历: 左根右"""
if (root != None):
self.in_travel(root.left)
print(root.val)
self.in_travel(root.right)
def last_travel(self, root):
"""后序遍历: 左右根"""
if (root != None):
self.last_travel(root.left)
self.last_travel(root.right)
print(root.val)
if __name__ == '__main__':
node1 = Node(1)
node2 = Node(2)
node3 = Node(3)
node4 = Node(4)
node5 = Node(5)
node6 = Node(6)
node7 = Node(7)
node8 = Node(8)
node9 = Node(9)
node10 = Node(10)
bt = BinaryTree(root=node1)
node1.left = node2
node1.right = node3
node2.left = node4
node2.right= node5
node3.left = node6
node3.right = node7
node4.left = node8
node4.right = node9
node5.left = node10
bt.pre_travel(node1)
|