Home > Back-end >  Java code
Java code

Time:09-17

Write an abstract class of the Java method to calculate the user to enter the average of the three Numbers,

CodePudding user response:

Then you will be a class definition for the abstract, afterward again write a calculation inside draw the value of the method is not good

CodePudding user response:

 import Java. Math. BigDecimal; 

Public class Test1 {

The static abstract class AbsDigitalProcessor {

Public void doProcess (int a, b int, int) c {
BigDecimal result=calculate (a, b, c);
System. The out. Println (result. ToPlainString ());
//get the result to do something
}

Protected the abstract BigDecimal calculate (int a, b int, int) c;
}

Static class AvaDigitalProcessor extends AbsDigitalProcessor {

@ Override
Protected BigDecimal calculate (int a, b int, int) c {
Return new BigDecimal ((a + b + c)/(3);
}
}

Static class CountDigitalProcessor extends AbsDigitalProcessor {

@ Override
Protected BigDecimal calculate (int a, b int, int) c {
Return new BigDecimal (a + b + c);
}
}

Static class SubDigitalProcessor extends AbsDigitalProcessor {

@ Override
Protected BigDecimal calculate (int a, b int, int) c {
Return new BigDecimal (a - b - c);
}
}

Public static void main (String [] args) {
New AvaDigitalProcessor (.) doProcess (1, 2, 3);
New CountDigitalProcessor (.) doProcess (1, 2, 3);
New SubDigitalProcessor (.) doProcess (1, 2, 3);
}

}


Like this?

CodePudding user response:

Abstraction is the abstract
  • Related