In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the example analysis of single inheritance and multi-inheritance in Jython, which is very detailed and has certain reference value. Friends who are interested must read it all!
Inheritance of Jython
The ability of class inheritance is the foundation of object-oriented programming. Jython supports single inheritance and multiple inheritance. Single inheritance means that there is only one parent class, and multiple inheritance means that there can be multiple parent classes.
Inheritance is implemented by deriving other classes. These classes can be other Jython classes or Java classes. You can derive from any number of pure Jython classes or Java instances, but only one Java class can be inherited (directly or indirectly). There is no need to provide a parent class.
All properties or methods in the parent class exist in all subclasses and can be used by the class itself or by all customers (assuming it is a publicly variable). All instances of a subclass can be used everywhere you can use an instance of a parent class-- here is an example of polymorphism. These capabilities make reuse, rapid development, and easy extension possible.
Here are some examples of inheritance:
Class Class1: pass # no inheritance class Class2: pass class Class3 (Class1): pass # single inheritance class Class4 (Class3,Class2): pass # multiple inheritance from java import awt from java import io # inherita Java class and interface and a Jython class class MyPanel (awt.Panel, io.Serializable, Class2)::
Inheritance of Jython-using a class as a value
You can also assign classes to variables (including function parameters). This makes it quite easy to write dynamic class-based code, as you can see in the following generic class instance factory:
Def instanceMaker (xclass, * args): return apply (xclass, args): X = instanceMaker (MyClass) # same as: X = MyClass ()
Inheritance of Jython-- init method with inheritance
The _ _ init__ method of a subclass must call all _ _ init__ methods defined by its parent class, which does not happen automatically. The following two examples show how to use the _ _ init__ method with inheritance.
Class Class1 (SuperClass): def _ init__ (self): # no arguments SuperClass.__init__ (self) # init my super-class self.data = [] # set implicit data class Class2 (SuperClass): def _ _ init__ (self, v1, v2): # 2 required arguments SuperClass.__init__ (self V1) # init my super-class with v1 self.v2 = v2
Here are some examples of initialization with multiple inheritance:
Class Class1 (Super1, Super2): def _ init__ (self): # no arguments Super1.__init__ (self) # init each super-class Super2.__init__ (self) self.data = [] # set implicit data class Class2 (Super1, Super2): def _ init__ (self, v1, v2) V3): # 3 required arguments # note you may do work before calling the super _ _ init__ methods self.v3 = v3 # set data from parameter Super1.__init__ (self, v1) # init each super-class Super2.__init__ (self, v2)
Inheritance of Jython-- calling parent method
You can call all parent class methods with class name qualification, as follows:
Class Class1: def method1 (self):: class Class2 (Class1): def method1 (self): # override method1: Class1.method1 (self) # call my super-class method: def method2 (self):: class Class3 (Class2): def method1 (self): # override method1: Class2.method1 (self) # call my Super-class method: def method3 (self)::
Notice that the secondary method definition (in Class2 and Class3) overrides the parent class definition. A subclass method is not required to call its parent method, but if it does not, it must completely replace the function of the parent method.
Inheritance of Jython-- calling method
There are two syntax for calling methods (suppose there is an instance of MyClass referenced by the variable mci):
Mci.someMethod (...) MyClass.someMethod (mci,...)
The * form is commonly used in class customer coding, while the second form is more commonly used to call parent class methods in subclasses.
The above is all the contents of the article "sample Analysis of single inheritance and Multi-inheritance in Jython". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow 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.