About the package of calculators

Source: Internet
Author: User

Encapsulating code

  InterfaceChapter {intAnswer {Get; } }    classCharp:chapter {Private intX1; Private intx2; Private stringOperator;  Public intresult;  PublicCharp (intX1,intX2,stringOperator) {             This. X1 =X1;  This. x2 =x2;  This. Operator =Operator; }         Public intAnswer {Get {                if(Operator = ="+") {result= x1 +x2; }                if(Operator = ="-") {result= x1-x2; }                if(Operator = ="*") {result= x1 *x2; }                 if(Operator = ="/") {result= x1/x2; }                returnresult; }                }    }}

Call

CLASS3 n =NewCLASS3 ();               N.cc (); First. Text=N.ll[oppo]; Both. Text=N.llp[oppo]; Xx. Text=N.lli[oppo]; Charp a=NewCharp (int. Parse (first. Text),int. Parse (both. Text), XX.                                                    Text); if(Results. text==convert.tostring (A.answer)) {MessageBox.Show ("Answer Right"); }                    Else{MessageBox.Show ("answer the wrong"); }

Summarize

This is my encapsulation of the code, the calculation of the algorithm encapsulated, as for teacher Chen said the strategy model, is a polymorphic usage, and this calculator algorithm, I feel that with a common class can be achieved. I saw someone else's use of the method or something, anyway, I just use an interface to inherit and then return the value directly. For the teacher to talk about the design pattern, I feel is a multi-state performance, in the factory model, various modes are polymorphic of various classes, in the face of different situations using different design patterns is also an object-oriented embodiment, for the package, I now the biggest feeling on the structure of the code is not so cumbersome, more clearly organized. The revolution is not yet successful, we still need to work hard

About the package of calculators

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.