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