6 接口与多态
作者:互联网
接口与多态
我的理解:
接口与多态 与 子类与多态非常类似
子类与多态:一个父类有很多个不同的子类,被许多个子类继承(是继承的概念)
接口与多态:一个接口由很多个子类实现(是实现的概念)
interface CompurerAverage{TRANSLATE with x English TRANSLATE with COPY THE URL BELOW Back EMBED THE SNIPPET BELOW IN YOUR SITE Enable collaborative features and customize widget: Bing Webmaster Portal Back
public double average(double a,double b);
}
class A implements CompurerAverage{
public double average(double a,double b){
double aver =0;
aver =(a+b)/2;
return aver;
}
}
class B implements CompurerAverage{
public double average(double a,double b){
double aver =0;
aver =Math.sqrt(a*b);
return aver;
}
}
public class Example6_4 {
public static void main(String[] args) {
CompurerAverage computer;
double a=11.23,b=22.78;
computer =new A();
double result = computer.average(a,b);
System.out.printf("%5.2f和%5.2f的算数平均值:%5.2f\n",a,b,result);
computer=new B();
result = computer.average(a,b);
System.out.printf("%5.2f和%5.2f的算数平均值:%5.2f\n",a,b,result);
}
}
标签:5.2,double,多态,接口,computer,aver,public 来源: https://www.cnblogs.com/xianbing2022/p/16444946.html