类对接口的实现:
package zuoye67;
public class Circle implements Figure{
private int bian1;
private int bian2;
private int bian3;
public void setbian1(int bian1){
this.bian1 = bian1;
}
public int getbian1(){
return bian1;
}
public void setbian2(int bian2){
this.bian2 = bian2;
}
public int getbian2(){
return bian2;
}
public void setbian3(int bian3){
this.bian3 = bian3;
}
public int getbian3(){
return bian3;
}
public Circle(){
}
public Circle (int bian1,int bian2,int bian3){
this.bian1 = bian1;
this.bian2 = bian2;
this.bian3 = bian3;
}
public double area() {
int a = bian1;
int b = bian2;
int c = bian3;
if(a>0&&b>0&&c>0&&(a+b)>c&&(a+c)>b&&(b+c)>a){
int p = (bian1+bian2+bian3)/2;
return Math.sqrt(p*(p-bian1)*(p-bian2)*(p-bian3));
}
else return 0;
}
public double circumference() {
int a = bian1;
int b = bian2;
int c = bian3;
if(a>0&&b>0&&c>0&&(a+b)>c&&(a+c)>b&&(b+c)>a){
return bian1+bian2+bian3;
}else return 0;
}
}
使用implements需要实现所有抽象方法。 使用abstract Arraylist可实现部分实现(刚才c++老师才讲java(狗头保命)) 接口的定义:
package zuoye67;
public interface Figure {
double area();
double circumference();
}
|