• Python-VBA编程500例-033(入门级)


    角色定位(Role Positioning)在编程中的实际应用场景主要体现在以下几个方面:

    1、权限管理:在开发企业级应用或复杂的系统时,角色定位用于定义和管理用户的权限。例如,一个系统可能有管理员、普通用户、访客等不同角色,每个角色有不同的访问和操作权限。通过角色定位,可以方便地为用户分配相应的角色,从而控制其对系统资源的访问和操作。

    2、工作流程设计:在业务流程自动化或工作流系统中,角色定位用于定义参与流程的各个实体(如员工、部门、系统服务等)的职责和权限。通过为每个角色分配特定的任务和责任,可以确保业务流程的顺畅进行,并减少人为错误。

    3、游戏开发:在游戏开发中,角色定位是核心要素之一。每个角色(如玩家角色、NPC、怪物等)都有独特的属性和行为。通过角色定位,可以定义角色的技能、属性、行为模式等,从而构建丰富的游戏世界和玩法。

    4、模拟系统:在模拟系统中,如经济模拟、社会模拟等,角色定位用于模拟不同实体(如消费者、生产者、政府等)的行为和互动。通过为角色设置不同的属性、规则和行为模式,可以模拟出真实世界的复杂性和多样性。

    5、多用户协作系统:在多人协作的系统中,如项目管理工具、在线协作平台等,角色定位用于明确每个用户的职责和权限。通过为不同角色分配不同的任务和功能,可以促进团队成员之间的协作和沟通,提高工作效率。

    6、安全审计和日志记录:通过角色定位,可以追踪和记录系统中每个角色的操作和行为。这对于安全审计和故障排查非常有帮助,可以及时发现潜在的安全隐患或问题,并采取相应的措施。

    7、软件架构和模块化:在软件设计和开发中,角色定位也可以用于划分模块和组件的职责。通过将不同的功能或服务分配给不同的角色,可以实现代码的解耦和复用,提高软件的可维护性和可扩展性。

    综上所述,角色定位在编程中具有广泛的应用场景,它可以帮助我们更好地管理和控制系统的复杂性,提高系统的安全性、稳定性和可维护性。

    1、角色定位:
    1-1、Python:
    1. # 1.问题描述:
    2. # 家庭模式是一种常见的设计模式,实现一个家庭Family,不同人物的角色定位.
    3. # 2.问题描述:
    4. # 输入:
    5. # Family fy = Family();
    6. # Role role = fy.getRole("Father");
    7. # role.talk();
    8. # 输出:I am a powerful father.
    9. # 输入:
    10. # Family fy = Family();
    11. # Role role = fy.getRole("Mother");
    12. # role.talk();
    13. # 输出:I am a kind and benevolent mother.
    14. # 3.代码实现:
    15. class Role:
    16. def talk(self):
    17. raise NotImplementedError("子类需要实现这个方法!")
    18. class Father(Role):
    19. def talk(self):
    20. print("I am a powerful father.")
    21. class Mother(Role):
    22. def talk(self):
    23. print("I am a kind and benevolent mother.")
    24. class Brother(Role):
    25. def talk(self):
    26. print("I am a sunny and handsome brother.")
    27. class Sister(Role):
    28. def talk(self):
    29. print("I am a tender and considerate sister.")
    30. class Grandfather(Role):
    31. def talk(self):
    32. print("I am a kind and affable grandfather. ")
    33. class Grandmother(Role):
    34. def talk(self):
    35. print("I am a kind and gentle grandmother. ")
    36. class FamilyRoles:
    37. def getRole(self, role):
    38. Roles = {
    39. 'Father': Father,
    40. 'Mother': Mother,
    41. 'Brother': Brother,
    42. 'Sister': Sister,
    43. 'Grandfather': Grandfather,
    44. 'Grandmother': Grandmother
    45. }
    46. return Roles.get(role)() if role in Roles else None
    47. # 主函数
    48. if __name__ == '__main__':
    49. fy = FamilyRoles()
    50. role1 = 'Father'
    51. role2 = 'Mother'
    52. role3 = 'Brother'
    53. role4 = 'Sister'
    54. role5 = 'Grandfather'
    55. role6 = 'Grandmother'
    56. for role_name in [role1, role2, role3, role4, role5, role6]:
    57. role = fy.getRole(role_name)
    58. if role:
    59. print(f"输入:role= {role_name},\n输出:")
    60. role.talk()
    61. else:
    62. print(f"输入:role= {role_name},\n输出:未找到该角色!")
    63. # 4.运行结果:
    64. # 输入:role= Father,
    65. # 输出:
    66. # I am a powerful father.
    67. # 输入:role= Mother,
    68. # 输出:
    69. # I am a kind and benevolent mother.
    70. # 输入:role= Brother,
    71. # 输出:
    72. # I am a sunny and handsome brother.
    73. # 输入:role= Sister,
    74. # 输出:
    75. # I am a tender and considerate sister.
    76. # 输入:role= Grandfather,
    77. # 输出:
    78. # I am a kind and affable grandfather.
    79. # 输入:role= Grandmother,
    80. # 输出:
    81. # I am a kind and gentle grandmother.
    1-2、VBA:
    1. ' 以下为6个类模块,在EXCEL中,按选Alt + F11进入VBE编辑器界面,点击菜单栏“插入”类模块并命名即可.
    2. ' Class Module: Father
    3. Option Explicit ' 声明此VBA模块中所有变量都必须明确声明
    4. Public Sub talk()
    5. ' 使用Debug.Print方法输出字符串到VBA的立即窗口中
    6. Debug.Print "I am a powerful father."
    7. End Sub
    8. ' Class Module: Mother
    9. Option Explicit ' 声明此VBA模块中所有变量都必须明确声明
    10. Public Sub talk()
    11. ' 使用Debug.Print方法输出字符串到VBA的立即窗口中
    12. Debug.Print "I am a kind and benevolent mother."
    13. End Sub
    14. ' Class Module: Brother
    15. Option Explicit ' 声明此VBA模块中所有变量都必须明确声明
    16. ' 定义一个公共子程序talk
    17. Public Sub talk()
    18. ' 使用Debug.Print方法输出字符串到VBA的立即窗口中
    19. Debug.Print "I am a sunny and handsome brother." ' 输出:"I am a sunny and handsome brother."
    20. End Sub
    21. ' Class Module: Sister
    22. Option Explicit ' 声明此VBA模块中所有变量都必须明确声明
    23. Public Sub talk()
    24. ' 使用Debug.Print方法输出字符串到VBA的立即窗口中
    25. Debug.Print "I am a tender and considerate sister."
    26. End Sub
    27. ' Class Module: Grandfather
    28. Option Explicit ' 声明此VBA模块中所有变量都必须明确声明
    29. Public Sub talk()
    30. ' 使用Debug.Print方法输出字符串到VBA的立即窗口中
    31. Debug.Print "I am a kind and affable grandfather."
    32. End Sub
    33. ' Class Module: Grandmother
    34. Option Explicit ' 声明此VBA模块中所有变量都必须明确声明
    35. Public Sub talk()
    36. ' 使用Debug.Print方法输出字符串到VBA的立即窗口中
    37. Debug.Print "I am a kind and gentle grandmother."
    38. End Sub
    39. ' 以下为标准模块部分,在EXCEL中,按选Alt + F11进入VBE编辑器界面,点击菜单栏“插入”标准模块并命名即可.
    40. Rem 执行程序,功能:通过调用类模块FamilyRoles,实现家庭角色的各自定位,在立即窗口中输出结果.
    41. Sub TestRun()
    42. ' 创建一个FamilyRoles类的新实例,赋值给fy变量
    43. Dim fy As New FamilyRoles
    44. ' 声明一个Variant类型的数组,用于存储角色名称
    45. Dim roleNames As Variant
    46. ' 声明一个Variant变量,用于循环中临时存储每个角色名称
    47. Dim roleName As Variant
    48. ' 声明一个Object类型的变量,用于存储从FamilyRoles类中获取的角色对象
    49. Dim role As Object
    50. ' 初始化roleNames数组,包含多个角色名称
    51. roleNames = Array("Father", "Mother", "Brother", "Sister", "Grandfather", "Grandmother")
    52. ' 遍历roleNames数组中的每个角色名称
    53. For Each roleName In roleNames
    54. ' 通过调用FamilyRoles类的getRole方法,获取对应角色名称的角色对象,并赋值给role变量
    55. Set role = fy.getRole(roleName)
    56. ' 检查是否成功获取到了角色对象
    57. If Not role Is Nothing Then
    58. ' 如果成功获取,则在立即窗口中输出角色名称,并调用该角色的talk方法
    59. Debug.Print "输入: role = " & roleName & " " & vbCrLf & "输出:"
    60. role.talk
    61. Else
    62. ' 如果未成功获取,则在调试窗口输出未找到该角色的信息
    63. Debug.Print "输入: role = " & roleName & " " & vbCrLf & "输出:未找到该角色!"
    64. End If
    65. Next roleName
    66. End Sub
    67. '结果输出:
    68. '输入: role = Father
    69. '输出:
    70. 'I am a powerful father.
    71. '输入: role = Mother
    72. '输出:
    73. 'I am a kind and benevolent mother.
    74. '输入: role = Brother
    75. '输出:
    76. 'I am a sunny and handsome brother.
    77. '输入: role = Sister
    78. '输出:
    79. 'I am a tender and considerate sister.
    80. '输入: role = Grandfather
    81. '输出:
    82. 'I am a kind and affable grandfather.
    83. '输入: role = Grandmother
    84. '输出:
    85. 'I am a kind and gentle grandmother.

    注意:1-2中的代码需粘贴到你的VBA编辑器中,按F5执行TestRun程序,在立即窗口中输出结果。

    2、相关文章:

    2-1、Python-VBA编程500例-031(入门级) 

    2-2、Python-VBA编程500例-032(入门级) 

    Myelsa的Python算法之旅(高铁直达):Myelsa的Python算法之旅(高铁直达)-CSDN博客
    欢迎访问个人主页:非风V非雨-CSDN博客
    欢迎志同道合者一起交流学习,我的QQ:94509325/微信:

  • 相关阅读:
    px、rpx、em以及rem的区别与用法
    图论思想与UML应用 (上 )开放封闭原则
    Vim学习笔记01~04
    英汉互译在线翻译器-大家都在用的英汉互译翻译器
    音视频开发(一)ffmpeg 简单学习
    还在想消除笔怎么用?这有几个易操作的软件
    MySQL的预编译入门
    [附源码]计算机毕业设计少儿节目智能推荐系统Springboot程序
    Fury:一个基于JIT动态编译的高性能多语言原生序列化框架
    网络安全设备默认密码
  • 原文地址:https://blog.csdn.net/ygb_1024/article/details/137434100