'''
Welcome to LearnPython.NET
File Name: PDataType.py
Download from:https://www.learnpython.net/cn/python-code-samples.html
Author: LearnPython.Net
Editor: CoderChiu
'''
from enum import Enum
#定义车辆的类型;
class VehicleType(Enum):
Car = 1
Ambulance = 2
#定义路段
class ExpresswayLocation(Enum):
ShenZhen = 1
HuiZhou = 2
def ExpresswaySpeedCheck(nSpeed, enumCarType, enumLocation):
if enumCarType == VehicleType.Ambulance:
return True
else:
if enumLocation == ExpresswayLocation.ShenZhen:
return ExpresswaySpeedCheckinShenZhen(nSpeed)
elif enumLocation == ExpresswayLocation.HuiZhou:
return ExpresswaySpeedCheckinHuiZhou(nSpeed)
else:
return False
#枚举类型的赋值操作
enumCarType = VehicleType.Car
enumLocation = ExpresswayLocation.ShenZhen
#小汽车当然速度79,运行在深圳,检查是否合法;
print(ExpresswaySpeedCheck(79, enumCarType, enumLocation))
class ExpresswayCheck:
def __init__(self, strLocation, nSpeedMin, nSpeedMax):
self.Location = strLocation
self.MinSpeed = nSpeedMin
self.MaxSpeed = nSpeedMax
def showMeTheSpeed(self):
print(self.Location, "valid speed is ", self.MinSpeed, "~", self.MaxSpeed, "KM/H.")
def checkSpeed(self, nCurSpeed):
print(nCurSpeed, " is Valid.")
classEC = ExpresswayCheck("HuiZhou", 60, 120)
classEC.showMeTheSpeed()
classEC.checkSpeed(120)
>>> %Run PDefClass.py
HuiZhou valid speed is 60 ~ 120 KM/H.
120 is Valid.