从另一个类创建一个类的实例 [英] create an instance of one class from another class

查看:161
本文介绍了从另一个类创建一个类的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了名为"bdrMenuView"的browserView类.它应该类似于"class bdrMenuView(BrowserView):".并且该类包含名为"createPictMenu"的方法.整个班级应该是

I created browserView class named as "bdrMenuView" . It should be like "class bdrMenuView(BrowserView):" . and the class contains the method named as "createPictMenu" . The whole class should be

    class bdrMenuView(BrowserView):
              def createPictMenu(self):

现在我又写了一个名为LogoViewlet的类.它应该类似于"Class LogoViewlet(ViewletBase):".并且该类包含名为"update"的方法.整个班级应该是

Now i have written one more class named as LogoViewlet . It should be like "class LogoViewlet(ViewletBase):" . and the class contains the method named as "update" . The whole class should be

    class LogoViewlet(ViewletBase):
              def update(self):

现在我想从另一个类调用browserView类的方法.我创建了一个像

Now i want to call the method of browserView class from another class. I created an instance of one class like

    class LogoViewlet(ViewletBase):
              def update(self):
                   a = bdrMenuView(self,BrowserView)      ---------> instance of BrowserView class
                   logoName = a.createPictMenu() 

我想知道我创建的内容是否正确.

I want to know whether it is correct or not which i created.

推荐答案

不,那是不正确的,完全没有道理.为什么将基类作为参数传递?请学习基本的Python.

No, that is not correct and makes absolutely no sense. Why do you pass in the baseclass as a parameter? Please learn basic Python.

视图的参数是上下文和请求.从另一个视图(一个viewlet)内部创建一个视图的最好方法是遍历该视图.您可以使用strictTraverse来做到这一点.

The parameters to views are the context and the request. The best way to make a view from inside another view (which a viewlet is) is to traverse to it. You can do this with restrictedTraverse.

确切的代码取决于您为视图注册的内容.例如,如果要查找的视图称为@@bdrmenu并注册了任何内容,则可以使用self.context.restrictedTraverse('@@bdrmenu')进行查找.

The exact code depends on what your view is registered for. For example, if the view you want to look up is called @@bdrmenu and registered for any content, you would look it up with self.context.restrictedTraverse('@@bdrmenu').

这篇关于从另一个类创建一个类的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆