1、定义MySQL类
import random
import hashlib
class MySQL:
def __init__(self,host,port):
# self.id=id
self.id=self.create_id()
self.host=host
self.port=port
def create_id(self):
front=self.host+self.port
self.id=hashlib.md5(front)
print(self.id.hexdigest())
return self.id
hosts=input('输入host')
ports=input('输入port')
Mysql=MySQL(hosts,ports)
print(MySQL.create_id)
conn=MySQL('192.168.100.1',3306)
print(conn.create_id)
2、定义一个类:圆形,该类有半径,周长,面积等属性,将半径隐藏起来,将周长与面积开放
import math
class circle:
def __init__(self, rad):
self.__rad=rad
# self.perimeter=perimeter
# self.area=area
@property
def perimeter(self):
return self.__rad * math.pi * 2
@property
def area(self):
return math.pi * self.__rad**2
c=circle(10)
print(c.area)
print(c.perimeter)
3、使用abc模块定义一个phone抽象类 并编写一个具体的实现类
import abc
class phone(metaclass=abc.ABCMeta):
@abc.abstractmethod
def name(self):
pass
@abc.abstractmethod
def prize(self):
pass
@abc.abstractmethod
def memory(self):
pass
class oneplus(phone):
def name(self):
print('One Plus 7 Pro')
def prize(self):
print(4299)
def memory(self):
print('256GB')
class oppo(phone):
def name(self):
print('Oppo Reno 2')
def prize(self):
print(2999)
def memory(self):
print('128GB')