栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python-rong-day07

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python-rong-day07

集合
# 集合 set 无序不重复的类型
# 字符串 '' 数字  12 33.3 列表 []  元组 () 字典 {:}  集合 {111}
from random import randint
ls = [randint(10, 13) for _ in range(10)]
print(ls)
s1 = set(ls)
print(s1)
print(type(s1))
# 声明一个集合
s2 = set() # 空集合
d = {} # 空字典
print(type(s2))
print(type(d))
s3 = {111, 222.22, '哈哈'}
print(s3)
# 不支持下标访问
# s3[0]  TypeError: 'set' object is not subscriptable
# 添加
s3.add('嘻嘻')
s3.add('嘻嘻')
s3.add('嘻嘻')
print(s3)
#删除
v = s3.pop()
print(v)
print(s3)
s3.remove(111)
print(s3)
s3.clear()
print(s3)
面向对象
## 面向对象
# 区别面向过程的
# 通过类创建对象 去完成我们要做的事情
# 类是对象的模板, 对象是类的具体实例
# 类的定义格式
# class 类名(object):
#     # 方法
#     pass
# 类中拥有属性(事务的特性)和方法(事务的行为)
class Student(object):
    def __init__(self):
        """__方法名__都是魔法方法, 具有特定的用途
          比如 __init__ 方法就是专门定义对象属性的,
          它会在类创建对象的过程中自动被调用
        """
        print("__init__ 自动被调用")
        # 定义属性
        self.name = "张三"
        self.age = 18
    def study(self):
        print(f"{self.name}疯狂的学习中.....")
    def eat(self):
        print(f"{self.name}大口的吃着...")
# 使用类
# 对象名 = 类名()
s1 = Student()
s2 = Student()
# 对象名.方法名()
s1.study()
s1.eat()
# 属性赋值(修改)
s2.name = "李四"
s2.study()
self 是什么
class Student(object):
    def __init__(self):
        self.name = "张三"
        self.age = 18
    def study(self):
        print(f'self的地址是{id(self)}')
        print(f"{self.name}疯狂的学习中.....")
# self 就是当哪个对象被调用 self 就是谁
s1 = Student()
s2 = Student()
print(s1) # 对象在内存中的地址
print(id(s1)) # 对象在内存中的地址
print(id(s2))
s1.study()
s2.study()
比较完整的类
class Student(object):
    def __init__(self, name, age, score=0):
        self.name = name
        self.age = age
        self.score = score
    def study(self):
        print(f"{self.name}疯狂的学习中.....")
        print(f"分数是{self.score}")
# self 就是当哪个对象被调用 self 就是谁
s1 = Student("张三", 20, 89)
s2 = Student('李四', 17)
s1.study()
s2.study()

定义一个比赛类

# match 类
# 属性 赛事名 str , 比分: str 参赛选手 []
# 方法: 显示上半场比分, 显示下半场比分
# 创建两个比赛对象, 并且进行数据模拟
class Match(object):
    def __init__(self, name, score):
        self.name = name
        self.score = score
        self.player = []

    def first(self):
        print(f"{self.name}")
        print(f"{self.player}参与本场比赛")
        print(f"分数是{self.score}")

    def second(self):
        print(f"{self.name}")
        print(f"{self.player}参与本场比赛")
        print(f"分数是{self.score}")


s1 = Match("英超联赛", 3)
s2 = Match('中超联赛', 17)
s1.player = ['peter', 'c罗', '梅西']
s1.first()
s2.second()
继承
# 继承: 子类继承父类所有属性和方法
class Person(object):
    def __init__(self, name, age):
        self.name = name
        self.age = age
    def eat(self):
        print("喵喵的吃")
    def show(self):
        print(f"姓名: {self.name} 年龄: {self.age}")
class Student(Person):
    def __init__(self, name, age, score):
        # 主动调用父类的__init___方法
        super().__init__(name, age)
        self.score = score
    def show(self):
        print(f"姓名: {self.name} 年龄: {self.age} 分数 {self.score}")
class Teacher(Person):
    def __init__(self, name, age, salary):
        super().__init__(name, age)
        self.salary = salary
    def show(self):
        print(f"姓名: {self.name} 年龄: {self.age} 工资 {self.salary}")
s1 = Student('小明', 13, 100)
s1.show()
t1 = Teacher('Eric', 56, 3500)
t1.show()
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/675018.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号