Python学习(是我的课堂上学到的东西)不包含课外

2023-11-14课堂

下面是今天学的有关python的一些内容
归纳一下就是对封装装饰器,继承,多态的内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
"""
装饰器的封装
"""

class Student:
def __init__(self, name, age):
self.name = name
self.age = age

@property
def age(self):
print("get")
return self.__age

@age.setter
def age(self,age):
print("set")
self.__age = age

s = Student("zhangsan", 18)
s.age = 21
print(s.age)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
"""
类方法和静态方法的装饰器
"""

class Student:

@classmethod
def class_method(cls):
print('class method')
print(cls)

@staticmethod
def static_method():
print('static method')

s = Student()
s.class_method()
Student.class_method()
Student.static_method()
s.static_method()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
"""
类成员和实例成员
"""
class Student:
id = 10
name = "jack"
def __init__(self,city,salary):
self.city = city
self.salary = salary

@classmethod
def class_method(cls):
print("class_method")

def instance_method(self):
print("instance_method")

# 类成员
print(Student.id,Student.name)
Student.class_method()
# 实例成员
stu = Student("Hong Kong",10000)
print(stu.id,stu.name,stu.city,stu.salary)
stu.instance_method()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
"""
继承:当一个类中定义的属性和方法需要在另一个类中直接使用,且该类和上一个类之间存在is a 的关系,就可以使用继承来实现属性和方法的重复使用
- is a 的关系 Person Staff Staff is a Person(继承) Car is a cat(no)
- 继承的实现
class Student(person,staff)
"""

class Father:
name = "jack"
age = 40
def teacher(self):
print("teaching...")

class Son(Father):
pass

s = Son()
print(s.name, s.age)
s.teacher()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
"""
继承(多继承)
"""

class Person:
name = "Person"
age = 18
def eat(self):
print("Person eat")

class Staff:
name = "Staff"
age = 20
def eat(self):
print("Staff eat")

class Coder(Person, Staff):
pass

c = Coder()
print(c.name, c.age)
c.eat()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
"""
继承:会先创建父类对象再创建子类的对象
"""

class Animal:
def __init__(self):
print("Animal is created")

class Cat(Animal):
# 注意:如果子类显示的定义了构造函数,需要手动调用父类的构造函数,如果子类没有定义构造函数,则会自动调用父类的构造函数
def __init__(self):
# 第一种方法:类
# Animal.__init__(self)
# 第二种方法:super()
super().__init__()
print("Cat is created")

cat = Cat()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
"""
多态
"""

class Animal:
def yell(self):
pass

class Dog(Animal):
def yell(self):
print('汪汪汪')

class Cat(Animal):
def yell(self):
print('喵喵喵')

# 演示方式1
animal: Animal = Dog()
animal.yell()
animal: Animal = Cat()
animal.yell()
print("----------------")
# 演示方式2
animals: [Cat(), Dog()]
for animal in animals:
animal.yell()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
-- 创建数据库
create database python;

-- 这里调用自己的数据库
use python;

-- 创建表
create table students(
ID int primary key auto_increment,
Name varchar(20) not null,
Python float default 50.0,
Java float default 60.0,
C float default 0.0
);

-- 插入数据
insert into students(Name,Python,Java,C) values ("张三",90.5,60,55),
("李四",100,88,66),
("王五",77,58,43)
;

-- 查询数据
select * from students;
select Name,Python from students;
select ID,Name,Python from students where Python >=80;

-- 更新数据
update students set C = 60 where ID=1;

-- 删除数据
delete from students where id =3;

-- 删除全部数据
truncate table students;