Translation Tipatterns - Multiple Programming Language (Multiple Languages) -1

xiaoxiao2021-03-06  45

Multiple programming languages ​​Multiple Languages-1 This chapter we discussed the benefits of crossing the language boundaries. In general, for a problem solving, more than one programming language is more convenient than death, which will be more convenient. This chapter you will see that it is very difficult or tricky problem for some language. If you use another language, it may be easy and quickly resolved. If you can combine multiple languages, you can create products faster more. The most direct application above this is that it is called the Interpreter design mode, which can add an interpretation language to your program, allowing the end user to make a solution. For Java, the simplest and most powerful way to achieve the above ideas is through JYTHON, which is a Python language written in Java's purely word code. Interpreter solves a specific issue - Create a scripting language for the user. But sometimes temporarily go to another language to solve some of the problems you face will be simpler and fast. It is not to say that you want to create an interpreter, you only need to write some code in another language. Reiterate again, Jython is a good example in this regard, and CORBA also allows you to cross the language boundary. Interpreter Mode Motivation Interpreter Motivation If the program needs to be greater flexibility at runtime, for example, in order to describe the desired system behavior by creating a script, you can use Interpreter design mode. In this case, you (need) Create a language interpreter and embed it into your program. Don't forget, each design pattern allows one or more factors that can vary, so it is important to know which factor is changed. Sometimes you are your program's end user (instead of program writing personnel) requires greater flexibility when they have some aspects of their configuration programs. In other words, they need to do some simple programming. The Interpreter mode provides this flexibility by adding a language interpreter. The problem is to develop your own language and build an interpreter for it is a time-consuming job, and it will spread your application's energy. You have to ask yourself whether you want to write the application or create a new language. The best solution is the code reuse: embed an interpreter that has been built and debugged. The Python language can be embedded with profitable applications without having to sign any license agreement and paying royalty, and do not have to follow any additional conditions. Basically, you are not subject to any restrictions when using Python. Python is a language that is very easy to learn. Its logic is very strong, easy to read and write, it supports functions and objects, there is a lot of available libraries, almost run on all platforms. You can download Python at www.python.org and find more information there. In order to solve problems related to Java, we focus on a special version of Python Jython. It is completely generated by Java's bytecode, so it is very simple to put it into your application, and it has good portability as Java. It and a very clean interface between Java: Java can call the Python class, and Python can also call the Java class. Python is designed from the beginning, it is a true pure face-to-object language (C and Java violates the principle of pure object-oriented) in different ways). Since Python is incremented (SCALES UP), even if you create a very large program, you will not lose control on its code.

To install Python, visit www.python.org. You can do it according to those links and instructions. To install Jython, visit http://jython.sourceforge.net. Download is a .class file, you will start a setup program when you use Java. You also need to add Jython.jar to your classpath. You can visit http://www.bruceeckel.com/tipatterns/building-code.html. Find a further installation instructions.

Python Overview In order to let you get started, the following is about Python's short introduction, this introduction is for experienced programmers (if you are reading this book, you must be an experienced programmer). You can refer to www.python.org more comprehensive Python document (especially very useful HTML page: Python Quick Reference Manual), in addition, you can refer to numerous Python books, such as Mark Lutz and David Aschor written "Learning Python" (O'Reilly, 1999). Python is often a scripting language, but the scripting language seems to have a lot of limitations, especially for the fields they solve. Unlike this, Python is a language that supports scripting programming. It is really great to write scripts, you will even want to replace all your batch files, shell scripts, and some simple programs with Python scripts. But it far exceeds a scripting language. Python is designed to be such a language, which is very clean with the code it written, and it is easy to read. You will find that even for a long time, you are very easy to read the code you wrote, and reading the code of others. This is at a certain degree of syntax, but it is important for code readability to indent - for Python, the scope is determined by the scope. For example: ## interpreter: if.pyResponse = "yes" if Response == "yes": print "affirmative" VAL = 1Print "Continuing ..." ## ~ '#' indicates a note until the end of the line, Just like the '//' comment in C and Java. First we noticed that the basic syntax of Python is a C style; please pay attention to the IF statement above. But the IF statement of the C language, you have to enclose the condition in parentheses, and don't need it in Python (but if you use parentheses it will not complain). The conditional statement is ending with a colon, which means that it is a set of indentational statements, which is part of the "Ten" of the IF statement. In the above example, first, a "print" statement sent the result to the standard output, next to a statement that assigns a variable called VAL. Then the next statement is not indent, so it is not part of the IF statement. The indentation can be nested to any level, just like C or java's braces, but with C , Java is different here that there is no parenthery, where there is no problem (there is no argument) - Compiler Force all The human code is formatted in the same way, which is also a major reason for Python has a strong readability. Typically, Python has only one statement per line (you can place multiple statements in one line using a semicolon), which is not necessary. Even if you look at the shortest example above, you will also find that this language is designed as simple as possible, while also has good readability. Built-in containers For languages ​​like C and Java, the container is attached in the form of a library, and it is not integrated with the language.

In Python, the importance of the container is an important position of the programming language to construct them into language cores to recognize: Links (LISTS) and associated arrays (ie mapping tables, dictionaries, hash tables) It has become a basic data type. This makes this language more elegant. In addition, the For statement automatically traverses the chain table, not just couting through a sequence of numbers. Think carefully, this is actually very meaningful, because in most cases, you are using a For loop to single-step traversal (Step Through) one array or container. Python is automatically allowed to use an iterator that uses an iterator on a sequence in a "Works Through" to standardize this. Here is an example: ## interpreter: list.pylist = [1, 3, 5, 7, 9, 11] Print ListList.Append (13) for x in list: Print X ## ~

The first line of code created a list. You can print it out, the result of printing should be the same as you put in, as a comparison, recall in the second edition of Thinking in Java, in order to print the array, we must create a special array2 class). The linked list here is similar to the container class in Java - you can add new elements to the inside (in the above example, use append () to add), the linked list will automatically adjust your size. The FOR statement creates an X iterator that acts on each element of the linked list. You can use the Range () function to create a linked list that stores the number, so if you really need the for statement in the analog C, you can do this. Note that the above code does not have any type declaration - the object is as long as you have a name, and Python will infer its type according to the way you use them. It feels like the purpose of designing Python is to let you only knock on the keyboard when absolutely necessary. After using Python for a short period of time, you will find that for non-Python's programming language, it is essential, and let you race your brains, braces, and other additional predicates, do not describe the intention of the program . Function Functions creates a function in Python, you need to use the DEF keyword, next to the function name and parameter list, then a colon indicates the beginning of the function. The following code is rewritten into a function: ## interpreter: myfunction.pydef myfunction (response): val = 0 if Response == "yes": print "affirmative" val = 1 Print "Continuing ..." Return Val Print MyFunction ("NO") #### 注意 到 到 类)))))) 是 是 只 是 只 是 指))) 信息 指 是 指 是 是 是 是 指, 指 指,,,,, Types of. Python is a weaker-type language, which means that it has lowered the requirements for type information. For example, you can incorporate and return to different types for the same function. ## Interpreter: Differentreturns.Pydef DifferentReturns (arg): if arg == 1: Return "One" if arg == "one": return 1 Print DifferentReturns (1) Print Differentreturns ("one") ## ~ For incoming Some functions, the only limit is that the operation of the function must be applied to this object, in addition to this, the function does not care about the object. In the above example, the same function applies the ' ' operator to the numbers and strings. ### interpreter: Sum.pydef SUM (Arg1, Arg2): Return Arg1 Arg2 Print SUM (42, 47) Print SUM ('spam', "eggs") ## ~ When the ' ' operator is applied to the string It means that it means connecting two strings (yes, Python supports operator overload, and it is doing this in this regard).

The example on string Strings also shows a little bit of Python string processing, which is the best in the language I have ever seen. You can use single quotes or double quotes to represent strings, this is very good, because if you use double quotes to bring a string, you can embed single quotes, but in turn. ## interpreter: strings.pyprint "That isn't a horse" print 'you are not a "viking"' print "" You're Just Pounding Twococonut Halves TOGETHER. "" "Print ''" OH NO! " He Exclaimed. "It's the blemange!" '' Print r'c: / python / lib / utils' ## ~ Please remember that Python is not named according to the name of the snake, in fact it is a monty big puree flight The circus, so the above example is theoretically, it needs to include Python-Esque References. Three double quotes are used, this syntax indicates that all things between them come, including new rows. This is especially useful when handling such a WEB page (Python is a great CGI language), because you can simply use three consecutive dual quotes to cause the entire page you want, not Do any other editor. The R characters on the right side of a string indicate that this string is a "RAW string", which means that the backslash is explained in terms of literal, so you don't have to add an additional backslash. Python's string replaces the simple simple because it uses a replacement syntax similar to the Printf (), for any string (But for any string at all ???). You only need to add one after the string '%' And the value used to do alternative. ## interpreter: StringFormatting.pyval = 47print "The number IS% D"% VALVAL2 = 63.4s = "VAL:% D, VAL2:% F"% (VAL, VAL2) Print S ## ~ You can see the top In two cases, if there is more than one parameter, you can enclose them with parentheses (this forms a tip), which is a linked list that cannot be changed). All formatting operations suitable for printf () can be used here, including control of the number of small digits and alignment. Python also has a very complex regular expression (syntax).

Like other things in Python, define a class only requires a minimum amount of additional syntax. Use the Class keyword (defined class) to create a DEF creation method (Methods) internally in the class definition. Here is an example: ## interpreter: SimpleClass.pyclass Simple: DEF __INIT __ (Self, Str): Print "Inside the Simple Construction" self.s = str # two methods: Def show (self): Print Self.s Def Showmsg (Self, MSG): Print Msg ':', Self.Show () # calling another method if __name__ == "__main__: # create an object: x = Simple (" constructor argument ") x.show () x .Showmsg ("a message" ## ~

The above two methods use "Self" as their first parameters. There is a hidden first parameter in C and Java, that is, pointing to a pointer to the object that calls this method, you can access it through the THIS keyword. The Python method also uses the current object reference, but when defining a method, you must explicitly specify this reference as the first parameter. Traditionally, this reference is called Self, but you can also use any monitors you want to use (but if you don't have Self, it is likely to make many people feel confused). If you need to use the object's field or other method, you must use Self in the expression. However, when you are a method of calling an object like X.show (), you don't need to pass the Self reference to the object - Python has been done well. The first method of the above has its own special, all the indicators starting with double underscores and ends are special. For the above situation, it defines the constructor that it will be called automatically when the object is created, just like C and Java. However, in the second half of the above, you will see that the creation of objects is like a function call using the class name. Python loose (spre) grammar will make you feel that the NEW keyword in C or Java is actually not required. All of the code of the latter part is spaced apart by an IF statement, this IF statement check __name__ this thing is equal to __main__. Again, the double undersca means a special name. The reason for using IF is because each file is likely to be used as library modules in another program (modules immediately). In that case, you only need those classes that define, and don't want the code in the lower half of the file being executed. This special IF statement is only true when you run this file directly, that is, if you enter: python simpleclass.py, if this file is imported as a module by another program, then The code __main__ will not be executed. It may make you feel a bit strange that you have to define member variables within the method, rather than being defined outside of the method like C or Java (if you define member variables like C / Java, they are hidden Contains a static member variable). To create a member variable of an object, you only need to be inside a method (usually in the constructor, but not all) - use the Self keyword - give it a name, when running that method Will allocate space for member variables. In C or Java, this seems a bit strange, because you must decide how much space needs to be taken in advance for C or Java, but Python's approach has proven to be a very flexible programming method.

Inherited inheritance because Python is a weak type (Weakly Typed), it doesn't really care about the interface - it is just applied to an object (in fact, Java's Interface keyword is a waste in Python). That is to say that Python's inheritance and C or Java are different, and the two are often established by inheriting simple buildings. For Python, the only reason to use inheritance is to inherit an implementation - in order to reuse the code belonging to the base class. If you intend to inherit from a certain class, you must tell Python to introduce the new class into your new file. Python is controlled by Name Spaces like Java, and the style is similar to Java (nevertheless, Python retains its consistent simplicity). When you create a file, you have implicitly created a module that is the same as the file (similar to the package in Java). As a result, the package keyword does not need in Python. When you want to use a module, just use the Import keyword and give the module's name. Python searches PythonPath, Java search ClassPath with the same method (but for some reason, Python does not have a defect like Java) and then read the search file. To reference a function or class of a module, you need to give the module name, then a period, then a function name or class name. If you feel that the exact name is too trouble, you can use: #from module import name (s) here, "Names (s) can be a set of names separate from comma. You can list the names of the inherited class by listening to the inheritance class, so that the inheritance is inherited from a class (or multiple classes --Python supports multiple inheritance). Note that the SIMPLE class belongs to the SimpleClass file (that is, the module), introduces it to the new namespace by using the Import statement.

转载请注明原文地址:https://www.9cbs.com/read-55844.html

New Post(0)