码迷,mamicode.com
首页 > 其他好文 > 详细

面向对象

时间:2018-05-21 12:28:29      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:utf-8   ***   atime   ons   ict   完成   inpu   附加   类的方法   

"""

class OldboyStudent():

def __init__(self,name,age,sex):
self.name = name
self.age = age
self.sex = sex


s1 = OldboyStudent(‘李坦克‘,‘男‘,18)
s2 = OldboyStudent(‘欧阳‘,‘men‘,18)
s3 = OldboyStudent(‘li‘,‘men‘,22)


a = s1.name
print(a)
s2.name = ‘王三炮‘
print(s2.name)
s2.course = ‘python‘
b = s2.__dict__
print(b)
del s2.course
c = s2.__dict__
print(c)
print(id(OldboyStudent))
print(id(s1.name))
print(id(s2.name))
"""

"""
class OldboyStudent():
school = ‘oldboy‘
def __init__(self,name,age,sex):
self.name = name
self.age = age
self.sex = sex

def learn(self):
print("%s is learning"%self.name)

def eat(self):
print("%s is eating"%self.name)

def sleep(self):
print("%s is sleeping" %self.name)



s1 = OldboyStudent(‘李坦克‘,‘男‘,18)
s2 = OldboyStudent(‘欧阳‘,‘men‘,18)
s3 = OldboyStudent(‘li‘,‘men‘,22)


# OldboyStudent.learn(s1)
# OldboyStudent.eat(s2)
# OldboyStudent.sleep(s3)
# a = OldboyStudent.school
# print(a)

s1.learn()
"""
"""
class student():
def __init__(self,name,age,high):
self.name = name
self.age = age
self.high = high

def learn(self):
print("好好学习!!!!")

def eat(self):
print("好好吃饭")

def sleep(self):
print("好好睡觉!!!")


s2 = student(‘xiaohui‘,18,170)
s2.learn()
s2.sleep()
"""


import time
class libai():
def __init__(self,name,age):
self.name =name
self.age = age

def mingcheng(self):
print("我的名字是 %s " %self.name)

def gjl(self):
a = self.age + 2
return a

def smz(self):
b = 100
return b




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

def mingcheng(self):
print("我的名字是 %s " % self.name)

def gjl(self):
a = self.age + 14
return a

def smz(self):
b = 100
return b



s1 = libai(‘李白‘,20)
s2 = hanxin(‘韩信‘,19)

def ooo():
H = 100
L = 100

d = 1
while True:
d+=1
print(‘李白打韩信‘)
H =H - s1.gjl()
print("韩信的生命值",H)
time.sleep(1)
print(‘韩信打李白‘)
L = L-s2.gjl()
print("李白的生命值",L)
time.sleep(1)
if H <= 0:
print("韩信已经死亡")
break
elif L <= 0:
print("李白已经死亡")
break
else:
print("第 " + str(d) + "回合开始")


print("游戏结束!!")

username = input("你叫什么名字?")
lll = input("你好" + username +"这是一个韩信和李白的凄惨爱情故事 " ‘输入 ok 开始游戏!!!‘)
if lll == ‘ok‘:
ooo()


"""
class One:
pass

class Two:
pass
class Three(One):
pass

class Four(Two):
pass

a = Three.__bases__
print(a)

b = Four.__bases__
print(b)

c = One.__bases__
print(c)

d = Two.__bases__
print(d)

"""

class animal:
def __init__(self,name):
self.name = name
def eat(self):
print(‘吃‘)

def sleep(self):
print("睡觉")

def jiao(self):
print("叫")


class pople(animal):
def f1(self):
print(‘玩游戏‘)

class pig(animal):
def f2(self):
print("打滚")

class dog(animal):
def f3(self):
print("看家护院")

a = pople.sleep(‘xiaoming‘)
r = dog(‘buqi‘)
r.jiao()

pople.f1("xiaohong")

pople.__f1__

# 示范代码
"""
class A(object):
def test(self):
print("from A")

class B(A):
def test(self):
print("from B")

class C(A):
def test(self):
print(‘from C‘)

class D(B):
def test(self):
print(‘from D‘)

class E(C):
def test(self):
print("from E")

def F(D):
def test(self):
print("from F")

pass

f1 = F("xiaohui")
f1.test
print(F.test)
"""


# 下面两个都是继承
# 在子类中调用父类的方法
"""
class Vehice:
Country = ‘china‘

def __init__(self,name,speed,load,power):
self.name = name
self.speed = speed
self.load = load
self.poert = power

def run(self):
print(‘开车了!!!!‘)

class Subway(Vehice):
def __init__(self,name,speed,load,power,line):
Vehice.__init__(self,name,speed,load,power)
self.line = line

def run(self):
print("地铁%s 号线欢迎您" %self.name)
Vehice.run(self)

line1 = Subway(‘中国地铁‘,‘300m/s‘,‘100人/厢‘,‘电‘,‘1‘)
line1.run()


"""



# 用了super
"""
class Vehice:
Country = ‘china‘

def __init__(self,name,speed,load,power):
self.name = name
self.speed = speed
self.load = load
self.poert = power

def run(self):
print(‘开车了!!!!‘)

class Subway(Vehice):
def __init__(self,name,speed,load,power,line):
super().__init__(name,speed,load,power)
self.line = line

def run(self):
print("地铁%s 号线欢迎您" %self.name)
Vehice.run(self)



class Mobike(Vehice):
def dan(self):
print("哇呜呜呜呜呜 ,小单车来了")


line1 = Subway(‘中国地铁‘,‘300m/s‘,‘100人/厢‘,‘电‘,‘1‘)
line1.run()
"""


# 组合
"""
class Equip:
def fire(self):
print("release Fire skill")

class Riven:
camp = ‘Noxus‘
def __init__(self,nickname):
self.nicknsme = nickname
self.equip = Equip()

r1 = Riven(‘ouayng‘)

r1.equip.fire()#可以使用组合的类产生的对象所持有的方法

"""
# 用组合的方式建立了类与组合的类之间的关系,他是一种‘有‘的关系,比如教授有生日,教授教python和linux课程
# 教授有学生s1,s2,s3
# example
class pople:
def __init__(self,name,age,sex):
self.name = name
self.age = age
self.sex = sex

class Course:
def __init__(self,name,period,price):
self.name = name
self.period = period
self.price = price

def tell_info(self):
print(‘<%s %s %s>‘%(self.name,self.period,self.price))

class Teacher(pople):
def __init__(self,name,age,sex,job_title):
super().__init__(name,age,sex)
self.job_title = job_title
self.course = []
self.student = []

class Student(pople):
def __init__(self,name,age,sex):
super().__init__(name,age,sex)
self.course = []


egon = Teacher(‘egon‘,18,‘men‘,‘沙河霸道金牌讲师‘)
s1 = Student(‘张三‘,20,‘men‘)

# 创建课程
python = Course(‘python‘,‘3mons‘,3000.0)
linux = Course(‘linux‘,‘2mons‘,4000.0)


# 为老师和学生添加课程
egon.course.append(python)
egon.course.append(linux)
s1.course.append(python)

# 为老师添加学生
egon.student.append(s1)


for i in egon.course:
print(i)

for i in egon.student:
print(i)

r = egon.student
print(r)





# 多态与多态性
# 多态指的是一类事物有多种形态,比如动物有多种形态:人,猪,狗

"""
s = str(‘hello‘)
l = list([1,2,3])
t = tuple((4,5,6))

s.__len__()
l.__len__()
t.__len__()

len(s)
len(l)
len(t)
"""
# 封装
# 封装 = 隐藏
# 在python中,用双下划线(__)开头的方式将属性隐藏起来
# 其实这仅仅是一种变形操作
# 类中所有双下划綫开头的名称如__x都会自动变形成: _类名__x的形式:

#正常情况
"""
class A:
def fa(self):
print(‘from A‘)

def test(self):
self.fa()

class B(A):
def fa(self):
print(‘from B‘)

b = B()
b.test()
"""
# 吧fa定义成私有的,即__fa
"""
class A:
def __fa(self):
print(‘from A‘)

def test(self):
self.__fa()

class B(A):
def __fa(self):
print(‘from B‘)

b = B()
b.test()
"""

# 封装不是单纯意义的隐藏
# 1、封装数据
# 将数据隐藏起来不是目的。隐藏起来然后对外提供操作该数据的接口,
# 然后我们可以在接口附加上对该数据上操作的限制,以此完成对数据属性操作的严格控制
"""
class Teacher:
def __init__(self,name,age):
self.__name = name
self.__age = age

def tell_info(self):
print(‘姓名:%s,年龄:%s‘ %(self.__name,self.__age))

def set_info(self,name,age):
if not isinstance(name,str):
raise TypeError(‘姓名必须是字符串类型‘)

if not isinstance(age,int):
raise TypeError("年龄必须是整数型")
self.__name = name
self.__age = age

t = Teacher("ouyang",18)
t.tell_info()

r = Teacher("xiaohui",19)
r.set_info(‘ll‘,12)

"""

# 2、封装方法:目的是隔离复杂度
# 取款是功能,而这个功能有很多的功能组成:插卡,密码认证,输入金额,打印账单,取钱
# 而对于使用者来说,只需要知道取款这个功能即可,其余功能我们都可以隐藏起来,
# 很明显这么做隔离了复杂度,同时也提升了安全性
"""
class ATM:
def _card(self):
print(‘插卡‘)

def _suth(self):
print(‘用户认证‘)

def _input(self):
print(‘输入取款金额‘)

def _print_bill(self):
print("打印账单")

def _take_money(self):
print(‘取款‘)


def withdraw(self):
self._card()
self._suth()
self._input()
self._print_bill()
self._take_money()

a = ATM()
a.withdraw()


"""


# *****特性(property)******

# property 是一种特殊的属性,访问他时会执行一段功能(函数)然后返回值
# Ex:BMI指数 188页
"""
class People:
def __init__(self,name,weight,height):
self.name = name
self.weight = weight
self.height = height

@property
def bmi(self):
return self.weight / (self.height**2)

p1 = People(‘ouyang‘,75,1.75)
print(p1.bmi)
"""

# ex:‘晓晖‘
"""
class People:
def __init__(self,name,weight,height):
self.name = name
self.weight = weight
self.height = height

@property
def bmi(self):
return self.weight/(self.height**2)

p1 = People(‘xiaohui‘,65,1.60)
print(p1.bmi)

"""


# example 二
# 圆的周长和面积
"""
import math
class Circle:
def __init__(self,radius):
self.radius = radius

@property
def area(self):#area面积
return math.pi*self.radius**2

@property
def perimeter(self):#perimeter周长
return math.pi*2*self.radius
c = Circle(10)
print(c.radius)
print(c.area)
print(c.perimeter)
"""

# 绑定方法

# 绑定给对象的方法
# 绑定给类的方法(classmethod)
# classmethod是给类用的,即绑定到类,类在使用时会将类本身当做参数传给
# 类方法的第一个参数(即是对象来调用也会将类当做第一个参数传入)
# python为我们内置了函数classmethod来把类中的函数定义成类方法


# 非绑定方法 在内部用staticmethod 装饰的函数即非绑定方法,就是普通函数
# statimethod不与类或对象绑定,谁都可以调用,没有自动传值效果
"""
import hashlib
import time
class MySQL:
def __init__(self,host,port):
self.id = self.create_id()
self.host = host
self.port = port


@staticmethod
def create_id():
m = hashlib.md5(str(time.time()).encode( ‘utf-8‘))
return m.hexdigest()

print(MySQL.create_id())
conn = MySQL(‘127.0.0.1‘,3306)
print(conn.create_id())



"""






面向对象

标签:utf-8   ***   atime   ons   ict   完成   inpu   附加   类的方法   

原文地址:https://www.cnblogs.com/ouyang99-/p/9066073.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!