15面向对象编程

#___author:  
#date: 2021/6/16
'''''
#面向过程和面向对象的区别
#面向过程思维:适合编写小规模的程序
#面向对象思维:适合编写大规模的程序
#面相对象和面向过程的总结
#1.都是解决问题的思维方式,都是代码组织的方式
#2.解决简单问题可以使用面向过程
#3.解决复杂问题:宏观上使用面向对象把握,微观处理上任然是面向过程
#类的定义:
class Student: #类名一般首字母大写,多个单词采用驼峰原则
def __init__(self,name,age):
self.name = name
self.age = age
def nian(self):
print("姓名:{0},年龄:{1}".format(self.name,self.age))
a = Student("杨正",21) #通过类名()调用构造函数
a.nian()
'''''
'''''
#构造函数__init__()
#对象包含三个部分:1.id(id(identity识别码)),2.type(对象类型),
# 3.value(对象的值)(1)属性(attribute)(2)方法(method)
#实例属性
class Student: #类名一般首字母大写,多个单词采用驼峰原则
def __init__(self,name,age):
self.name = name
self.age = age
def nian(self):
print("姓名:{0},年龄:{1}".format(self.name,self.age))
a = Student("杨正",21)
a.nian()
a.bad =6000
a.dasw = 200
print(a.bad)
'''''
'''''
#实例方法
def 方法名(self [,形参列表]):
函数体
对象.方法名([实参列表])
#其他操作
1.dir(obj)可以获得对象的所有属性,方法
2.obj.__dict__对象的属性字典
3.pass 空语句
4.isinstance(对象,类型) 判断“对象”是不是“指定类型”
'''''
'''''
#类对象
class Student:
pass
print(type(Student))
print(id(Student))
a1 = Student
b1 = a1()
print(b1)
#类属性和类方法
#类属性
#类属性的定义方式:
#class 类名():
# 类变量名 = 初始值
import time
time1 = time.time()
class Student:
company = "SXT" #类属性
count = 0 #类属性

def __init__(self,name,age):
self.name =name #实例属性
self.age = age
Student.count =Student.count+1

def a1(self): #实例方法
print("公司名字{0}".format(Student.company))
print("姓名:{0},年龄{1}".format(self.name,self.age))

a = Student("张三",18) #a是实例对象,自动调用__init__()方法
a.a1()
print("一共创建{0}:Student对象".format(Student.count))
time2 = time.time()
print("时间{0}".format(time2-time1))
#类方法
#@classmethod
#def 方法名(self [,形参列表]):
# 函数体
class Student:
pad = "TST"
def __init__(self,name,age):
self.name = name
self.age = age
@classmethod
def pr(cls): #类方法
print(cls.pad)
print() #类方法和静态方法中,不能调用实例变量,实例方法
Student.pr()
'''''
'''''
#静态方法
#@staticmethod
#def 方法名(self [,形参列表]):
# 函数体
class Student:

a ="TSMT" #类属性
@staticmethod
def b(c,d): #静态方法
print("公司名字:{0}".format(Student.a))
print("{0}+{1}={2}".format(c,d,(c+d)))
return c+d

Student.b(20,30)
'''''
'''''
#__del__(析构方法)和垃圾回收机制
class Person:
def __del__(self):
print("销毁{0}".format(self))
p1 = Person()
p2 = Person()
p3 = Person()
del p1
print("程序结束")
'''''
'''''
#__call__方法和可调用对象
#测试可调用方法__call__()
class SalaryAccount:
#计算工资
def __call__(self, salary):
print("发工资了")
yearSalary = salary*12
daySalary = salary//22.5
hiurSalary = daySalary//8
return dict(yearSalary=yearSalary,daySalary=daySalary,hiurSalary=hiurSalary)

a = SalaryAccount()
print(a(30000))
'''''
'''''
#方法没有重载
#测试方法的动态性
class Person:
def word(self):
print("努力上班")
def word1(v):
print("{0}玩游戏".format(v))
def word2(s):
print("努力赚钱")
Person.word1 = word1 #类调用不在类里的函数
Person.word2 = word2
p1 = Person()
p1.word()
p1.word1()
p1.word2()
'''''
#私有属性和私有方法(实现封装)
class Employee:
__a = 100 #私有类属性
def __init__(self,name,age):
self.name =name
self.__age = age #私有属性
def __word(self): #私有方法
print("努力钻钱")
e = Employee("杨正",18)
print(e.name)
print(e._Employee__age) #调用私有属性
e._Employee__word() #调用私有方法
print(Employee._Employee__a)
posted @ 2021-07-28 09:26  幻奇  阅读(43)  评论(0)    收藏  举报