本系列博文基于廖雪峰老师的官网Python教程,笔者在大学期间已经阅读过廖老师的Python教程,教程相当不错,官网链接: 廖雪峰官方网站.请需要系统学习Python的小伙伴到廖老师官网学习,笔者的编程环境是Anaconda+Pycharm,Python版本:Python3.
1.面向对象简介
employee1 = {"name": "Willard", "salary": 10000}
employee2 = {"name": "ChenJD", "salary" : 12000}
employee3 = {"name": "LiangXinYu", "salary" : 15000}
def printSalary(employee):
print("%s : %s" % (employee["name"], employee["salary"]))
print("FUXI Technology员工薪资表")
print("--------------------------")
printSalary(employee1)
printSalary(employee2)
printSalary(employee3)
print("--------------------------")
print("--------------------------")
class Employee(object):
def __init__(self, name, salary):
self.name = name
self.salary = salary
def printSalary(self):
print("%s : %s" % (self.name, self.salary))
Willard = Employee("Willard", 10000)
ChenJD = Employee("ChenJD", 12000)
LiangXinYu = Employee("LiangXinYu", 15000)
print("面向对象打印员工薪资表")
print("FUXI Technology员工薪资表")
print("--------------------------")
Willard.printSalary()
ChenJD.printSalary()
LiangXinYu.printSalary()
FUXI Technology员工薪资表
--------------------------
Willard : 10000
ChenJD : 12000
LiangXinYu : 15000
--------------------------
--------------------------
面向对象打印员工薪资表
FUXI Technology员工薪资表
--------------------------
Willard : 10000
ChenJD : 12000
LiangXinYu : 15000
2.类和实例
class Employee(object):
pass
Willard = Employee()
ChenJD = Employee()
print("创建了Willard实例:",Willard)
print("创建了ChenJD实例:",ChenJD)
print("-------------------------------------------------------------------")
Willard.salary = 15000
print("给Willard实例绑定了salary属性:",Willard.salary)
class Employee(object):
def __init__(self,name,salary):
self.name = name
self.salary = salary
def printEmployeeSalary(self):
print("%s : %s" % (self.name, self.salary))
Willard = Employee("Willard", 15000)
Willard.printEmployeeSalary()
创建了Willard实例: <__main__.Employee object at 0x0000023B08BD4898>
创建了ChenJD实例: <__main__.Employee object at 0x0000023B08BD45C0>
-------------------------------------------------------------------
给Willard实例绑定了salary属性: 15000
Willard : 15000
3.数据封装
class Employee(object):
def __init__(self, name, salary):
self.name = name
self.salary = salary
def printSalary(self):
print("%s : %s" % (self.name, self.salary))
Willard = Employee("Willard", 15000)
print("Willard实例:")
Willard.printSalary()
print("------------------------------")
class Employee(object):
def __init__(self, name, salary):
self.name = name
self.salary = salary
def printSalary(self):
print("%s : %s" % (self.name, self.salary))
def salaryToRank(self):
if self.salary >= 20000:
print("Rank A Engineer.")
elif self.salary >= 15000:
print("Rank B Engineer.")
elif self.salary >= 10000:
print("Rank C Engineer.")
else:
print("Rank D Engineer.")
Willard = Employee("Willard", 20000)
print("Willard的工资为:")
Willard.printSalary()
print("-----------------------------------")
print("Willard工资对应的工程师等级:")
Willard.salaryToRank()
Willard实例:
Willard : 15000
------------------------------
Willard的工资为:
Willard : 20000
-----------------------------------
Willard工资对应的工程师等级:
Rank A Engineer.
4.访问限制
class Employee(object):
def __init__(self, name, salary):
self.name = name
self.salary = salary
def printSalary(self):
print("%s : %s" % (self.name, self.salary))
Willard = Employee("Willard", 15000)
print("Willard的薪资为:",Willard.salary)
print("---------------------------------------------")
Willard.salary = 20000
print("外部修改salary属性,Willard薪资变为:", Willard.salary)
print("---------------------------------------------")
class Employee(object):
def __init__(self, name, salary):
self.__name = name
self.__salary = salary
def printSalary(self):
print("%s : %s" % (self.__name, self.__salary))
Willard = Employee("Willard", 12000)
print("Willard的薪资为:")
Willard.printSalary()
print("---------------------------------------------")
print("---------------------------------------------")
class Employee(object):
def __init__(self, name, salary):
self.__name = name
self.__salary = salary
def printSalary(self):
print("%s : %s" % (self.__name, self.__salary))
def getName(self):
return self.__name
def getSalary(self):
return self.__salary
def setSalary(self, salary):
if salary <= 0:
raise ValueError("Input Error.")
else:
self.__salary = salary
ChenJD = Employee("ChenJD", 15000)
print("ChenJD的薪资信息为:")
ChenJD.printSalary()
print("---------------------------------------------")
print("ChenJD的薪资为:",ChenJD.getSalary())
print("---------------------------------------------")
ChenJD.setSalary(20000)
print("ChenJD的薪资重新设置:",ChenJD.getSalary())
Willard的薪资为: 15000
---------------------------------------------
外部修改salary属性,Willard薪资变为: 20000
---------------------------------------------
Willard的薪资为:
Willard : 12000
---------------------------------------------
---------------------------------------------
ChenJD的薪资信息为:
ChenJD : 15000
---------------------------------------------
ChenJD的薪资为: 15000
---------------------------------------------
ChenJD的薪资重新设置: 20000
|