封装
# 封装:属性私有化对外提供公共的访问方式
# 防止数据被随意修改
# 使外部程序不需要关注对象内部的构造,只需要通过对外提供的接口进行直接访问
class Student():
name="" #公有属性
age=0
_money = 100 # 私有属性
# 构造函数 创建对象用的 self相当于java中的this
def __init__(self,name,age):
self.name=name
self.age=age
#set/get
def __set_name__(self, owner, money):
self._money=money
def __get__(self, instance, __money):
return self.__money
# 私有函数
def _play(self):
print("打游戏")
#重写object的str函数
def __str__(self):
return "名字是{},年龄是{}".format(self.name,self.age)
#创建对象
print(stu.name+str(stu.age))
print("名字是<%s>,年龄是<%d>"%(stu.name,stu.age))
print(stu)
====================
张三18
名字是<张三>,年龄是<18>
名字是张三,年龄是18
继承
# 继承 python可以多继承
# 子类名 extends 父类名
class Father(object):
name = "诸葛"
def eat(self):
print("啃红薯")
class Mather(object):
money = 100
class Mather2(object):
hourse = "123"
class Son(Father, Mather, Mather2):
pass
name="令狐"
s=Son()
s.eat()
print(s.name)
print(s.money)
print(s.hourse)
====================
啃红薯
令狐
100
123
函数
#无参无返
def eat():
print("吃kfc")
#有参无返w
def eat2(num):
print("%d个人吃KFC"%(num))
# #无参有返
def eat3():
return "吃KFC"
# #有参有返
def eat4(num):
return "%d个人吃KFC"%(num)
eat()
eat2(2)
print(eat3())
print(eat4(5))
===================
吃kfc
2个人吃KFC
吃KFC
5个人吃KFC
多态
# 多态
# java:子类引用指向父类对象
# python:用一个函数来调用类里面的对象,但是不确定调用的是哪个对象
class F1(object):
def show(self):
print("F1.show")
class S1(F1):
def show(self):
print("S1.show")
class S2(S1):
def show(self):
print("S2.show")
def func(obj): #对象作为了参数
obj.show()
# 创建对象
obj1 = F1()
obj2 = S1()
obj3 = S2()
# 函数的调用
func(obj3)
=====================
S2.show
静态方法
# @staticmethod
# 可以通过 类名.函数名 对象名.函数名 调用
class A():
@staticmethod
def eat():
print("涮锅")
# a=A()
# a.eat() #对象名.函数名
A.eat() # 类名.函数名
IO流
# io流的使用 更改阅读模式 w r a
file = open("a.txt", "w") # w:写
file.write("admin")
file.flush() # 刷新
file.close() # 关流
file = open("a.txt", "a") # a:追加
file.write(" root")
file.flush() # 刷新
file.close() # 关流
file = open("a.txt", "r") # r:读
sr = file.read()
print(sr)
导包
from 练习.daobao.Father import Father
import math
a = math.pow(2, 4)
print(a)
f = Father()
f.eat()
# Father.eat()
模块
Pymysql
导入pymysql
import pymysql
#数据库连接信息
db = pymysql.Connect("localhost", "root", "123456", "lianxi")
# 创建光标
cur = db.cursor()
# 增删改
sql = "insert into exam(Course) values('英语')" # sql
cur.execute(sql)
db.commit()
# 查
# sql = "select * from exam where ID = 1"
# cur.execute(sql) # 执行sql语句
# res = cur.fetchall() # 查询多条数据的使用
# for i in res:
# print(i)
db.close()
封装
import pymysql
class jdbc():
# 连接
def connect(self, host, username, pwd, datebase):
global connect #global 定为全局变量
global cur
connect = pymysql.Connect(host, username, pwd, datebase)#连接信息
cur = connect.cursor()
# 查询操作
def select(self, sql):
cur.execute(sql)
res = cur.fetchall()
for i in res:
print(i)
# 增删改操作
def update(self, sql):
cur.execute(sql)
connect.commit()
# 关流操作
def close(self):
connect.close()
#创建对象
jd = jdbc()
#调用函数
jd.connect("localhost", "root", "123456", "lianxi")
jd.select("select * from exam where ID = 3")
# jd.select("select * from exam where ID = %d" % (1))
jd.update("update exam set Course='物理' where ID = 8")