In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you "how to add definition class attributes and hidden variables in Jython class". The content is simple and easy to understand, and the organization is clear. I hope it can help you solve your doubts. Let Xiaobian lead you to study and learn this article "how to add definition class attributes and hidden variables in Jython class".
Jython classes--add attributes to class instances
In Jython (unlike the Java language), customers can add fields (also known as attributes) to instances. This only changes one instance. To add a field (x) to an instance, simply set the new value on the instance, as follows:
x.attr1 = 1 x.attr2 = 2 : x.attrN = n
Jython classes--define class properties and methods
All variables bound in a class are class attributes (or variables). All functions defined in a class are methods. A method takes an instance of a class, commonly called self, as ***(perhaps the only) argument. For example, to define some class properties and methods, you can enter:
class MyClass: attr1 = 10 # class attributes attr2 = "hello" def method1(self): print MyClass.attr1 # reference the class attribute def method2(self, p1, p2): print MyClass.attr2 # reference the class attribute def method3(self, text): self.text = text # instance attribute print text, self.text # print my argument and my attribute method4 = method3 # make an alias for method3
Note that in a class, you should qualify all references to class attributes with the class name (e.g. MyClass.attr1) and all references to instance variables with the self variable (e.g. self.text). Outside a class, you should qualify all references to class attributes with class name (e.g. MyClass.attr1) or instance with instance (e.g. x.attr1) and all references to instance attributes (e.g. x.text, where x is an instance of the class).
Jython classes--hidden variables
To implement data hiding, it is often necessary to create a "private" variable that can only be accessed by the class itself. Jython provides a naming convention that makes it difficult to access properties and methods from outside the class. If you declare a name of the form__xxx or__xxx_yyy (preceded by an underscore), the Jython parser will automatically mangle the declared name (i.e., add the class name) to create a hidden variable. For example:
class MyClass: __attr = 10 # private class attribute def method1(self): pass def method2(self, p1, p2): pass def __privateMethod(self, text): self.__ text = text # private attribute
Note that unlike C++ and Java languages, all references to instance variables must be qualified with self, and this is not used implicitly.
The above is "Jython class how to add definition class attributes and hidden variables" all the content of this article, thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.