类相关
Posted dorami
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了类相关相关的知识,希望对你有一定的参考价值。
import pymysql
import redis
class Syy:
country = ‘China‘
def __init__(self):
self.money = 5000
def chaogu(self):
self.money += 50000
print(‘炒股!‘,self.money)
#节省代码
class Xs(Syy):
def chaogu(self):
super().chaogu()
print(‘唱歌‘)
sdb = Xs()
sdb.chaogu()
#ip,端口号、密码
class BaseDb:#基类
def __init__(self,ip,port,password,db):
self.ip = ip
self.port = port
self.password = password
self.db = db
def conn(self):
pass
class MySQL(BaseDb):
def __init__(self,user,ip,port,password,db):#方法重写
super().__init__(ip,port,password,db)
self.user = user
self.conn()
def conn(self):
self.conn = pymysql.connect(host=self.ip,port=self.port,user=self.user,
password=self.password,db=self.db)
self.cur = self.conn.cursor()
class Redis(BaseDb):
def conn(self):
self.r = redis.Redis(host=self.ip,db=self.db,
password=self.password,port=self.port)
class SqlServer(BaseDb):
pass
def shop(self): print(‘4s店‘) def baoxian(): print(‘保险公司‘) def shuwuju(): print(‘税务局‘) def jiaoguansuo(): print(‘交管所‘) class BuyCar: #买车处 def shop(self): print(‘4s‘) def baoxian(self): print(‘保险‘) def shuwuju(self): print(‘税务局‘) def jiaoguansuo(self): print(‘交管所‘) class Person: country = ‘China‘ def __init__(self,name,leg=2):#构造函数,类在实例化的时候,自动执行的 self.name = name #属性 self.leg = leg print(‘self的内存地址‘,id(self)) def __del__(self):#析构方法,实例被销毁的时候自动执行析构方法里面的代码。 print(‘什么时候执行这里的方法呢‘) def fly(self):#方法 print(‘%s在飞‘%self.name) self.bird = ‘猫头鹰‘ def eat(self):#s print(‘[%s]吃饭‘%self.name) print(‘我要吃【%s】‘%self.bird) @classmethod def saniao(cls): print(‘撒尿‘) def la(self): print(‘拉了%s次‘%self.count) def say(self): print(‘我的名字是【%s】,我有【%s】条腿‘%(self.name,self.leg)) print(‘国籍是%s‘%self.country) class Car: country = ‘China‘ def run(self): print(‘跑‘) self.act() @classmethod def shuoming(cls): print(‘怎么造汽车,你猜!‘) bmw = cls() bmw.run() cls.act() @staticmethod def act(): print(‘搞活动,好评送你10块钱‘) @property def name(self): ‘select name from user‘ return ‘大黄蜂‘ c = Car() print(c.name)
以上是关于类相关的主要内容,如果未能解决你的问题,请参考以下文章
Java全栈JavaSE:18.常用类之System类Math类数组相关操作字符串相关操作
JAVA IO流相关代码(Serializable接口,管道流PipedInputStream类,RandomAccessFile类相关代码)