(20) Interpreter mode-code implementation

Source: Internet
Author: User

Introduction

concept: given a language, define a representation of its grammar and define an interpreter that uses that representation to interpret sentences in the language.

Application Scenario: language Interpreter (translates the code we can read into an ugly machine code)

Benefits: use complex things in a simple way.

Example

Interpreter

 Package Note.com.interpreter; /**  @author*/Publicclass  interpreter    {  publicvoid  say (String lag) {        if("NH". Equals (lag)) {            System.out.println ("Hello");     }}

The person who called the Interpreter

 PackageNote.com.interpreter; Public classpeople {Privateinterpreter interpreter;  Publicpeople (interpreter interpreter) { This. Interpreter =interpreter; }        /** Simple language input*/     Public voidsay (String lag) {Interpreter.say (lag);//Invoke Interpreter    }     Publicinterpreter Getinterpreter () {returninterpreter; }     Public voidSetinterpreter (Interpreter interpreter) { This. Interpreter =interpreter; }}

Test class

 Package Note.com.interpreter;  Public class interpretertest {    publicstaticvoid  main (string[] args) {         New interpreter ();         New people (i);        People.say ("NH");}    }

Results

How are you doing

(20) Interpreter mode-code implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.