首页/文章/ 详情

ANSA二次开发_Python基础-面向对象结构

12天前浏览744

通过class的代码形式,批量创建PART。

    import ansafrom ansa import*class UserName ():  def __init__(self):    window=guitk.BCWindowCreate("Input_Values", guitk.constants.BCOnExitDestroy)    grid=guitk.BCGridLayoutCreate(window, 1, 1)    self.line_1=guitk.BCLineEditCreate(grid, 'name')    guitk.BCGridLayoutAddWidget(grid, self.line_1, 0, 1, guitk.constants.BCAlignVCenter+guitk.constants.BCAlignLeft)    self.label_1=guitk.BCLabelCreate(grid, "part name")    guitk.BCGridLayoutAddWidget(grid, self.label_1, 0, 0, guitk.constants.BCAlignVCenter+guitk.constants.BCAlignLeft)    guitk.BCDialogButtonBoxCreate(window)    guitk.BCWindowSetAcceptFunction(window, self.AcceptFunc, None)    guitk.BCWindowSetRejectFunction(window, self.RejectFunc, None)    guitk.BCShow(window)    def AcceptFunc (self, win, data):    self.name=guitk.BCLineEditGetText(self.line_1)    return 1          def RejectFunc (self, win, data):    return 1    def createpart():  window=UserName()  for value in window.name.split() :    base.CreateEntity(constants.NASTRAN,"ANSAPART",{"Name":value})createpart ()
    python的基础知识

    继承

    继承允许我们定义一个继承另一个类的属性和方法的类。

    封装

    封装是OOP的一个核心概念,它隐藏了对象的内部状态和实现细节,仅仅暴露出操作对象的接口。

    多态

    多态给我们提供了一种使用共有接口的方式,不同的对象可以以自己的方式去实现这些接口。
      # 基类/父类class Animal:    def __init__(self, name):        self.name = name  # 封装:属性name被封装在Animal类中    def speak(self):  # 多态:不同的动物会以它们自己的方式发出声音        raise NotImplementedError("Subclass must implement abstract method")# 子类继承自Animalclass Dog(Animal):    def speak(self):        return f"{self.name} says Woof!"class Cat(Animal):    def speak(self):        return f"{self.name} says Meow!"# 创建Animal类的实例animals = [Dog('Max'), Cat('Felix')]# 多态:调用相同的接口speak,执行不同的操作for animal in animals:    print(animal.speak())
      继承:Dog和Cat类继承自Animal类。这意味着它们可以继承Animal类的所有属性和方法。
      封装:Animal类封装了属性name和方法speak()。name属性在Animal类的构造函数中初始化,而speak()方法则被设计为在子类中实现。
      多态:通过Animal类的speak()方法展示了多态。尽管每个子类都有一个speak()方法,但是Dog和Cat类以自己的方式实现了这个方法。
      当我们遍历animals列表并调用speak()方法时,Python自动识别对象的实际类型,并调用相应类的speak()方法。

      来源:FEAer
      ACTNastran二次开发ANSApython清华 DeST
      著作权归作者所有,欢迎分享,未经许可,不得转载
      首次发布时间:2024-04-21
      最近编辑:12天前
      FEAer
      本科 | CAE工程师 到点就下班的CAE打工人
      获赞 66粉丝 72文章 61课程 2
      点赞
      收藏

      作者推荐

      未登录
      还没有评论

      课程
      培训
      服务
      行家

      VIP会员 学习 福利任务 兑换礼品
      下载APP
      联系我们
      帮助与反馈