JAVA课没好好听,书上没例题看不懂,貌似接口这东西很弱智啊……但我怎么就是弄不出来啊!!
interface Exchange{
public double area();
public void girth();
}
abstract class Shape{
abstract protected double area();
abstract protected void girth();
}
class Rectangle extends Shape implements Exchange{
float width,length;
Rectangle(float w,float l){
width=w;
length=l;
}
public double area(){
return width*length;
}
public void girth(){
double g=(width+length)*2;
System.out.println("and the girth is:"+g);
}
}
class Circle extends Shape implements Exchange{
float radius;
Circle(float r){
radius=r;
}
public double area(){
return radius*radius*3.14;
}
public void girth(){
double g=radius*2*3.14;
System.out.println("and the girth is:"+g);
}
}
public class Shape_ex_2 { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Exchange rc=new Rectangle(6,12);
Exchange cir=new Circle(4);
System.out.println("Rectangle's area is:"+rc.area());
rc.girth();
System.out.println("Circle's area is:"+cir.area());
cir.girth();
} }
interface Exchange{
public double area();
public void girth();
}
abstract class Shape{
abstract protected double area();
abstract protected void girth();
}
class Rectangle extends Shape implements Exchange{
float width,length;
Rectangle(float w,float l){
width=w;
length=l;
}
public double area(){
return width*length;
}
public void girth(){
double g=(width+length)*2;
System.out.println("and the girth is:"+g);
}
}
class Circle extends Shape implements Exchange{
float radius;
Circle(float r){
radius=r;
}
public double area(){
return radius*radius*3.14;
}
public void girth(){
double g=radius*2*3.14;
System.out.println("and the girth is:"+g);
}
}
public class Shape_ex_2 { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Exchange rc=new Rectangle(6,12);
Exchange cir=new Circle(4);
System.out.println("Rectangle's area is:"+rc.area());
rc.girth();
System.out.println("Circle's area is:"+cir.area());
cir.girth();
} }










