回 帖 发 新 帖 刷新版面

主题:如何引入包中的类?

包声明:
package package1;
//接口 面积
interface  shape_area
{
    void area();
}
//抽象类 shape
abstract class shape
{
    abstract void width();
}
//类 矩形
class rectangle  extends shape implements shape_area
{
    public double wid;
    public double len;
    rectangle(double x,double y)
    {
         wid=x;
         len=y;        
    }
    public void width()
     {
        System.out.println("The rectangle's area is "+(wid+len)*2);
     }

    public void  area()
    {
        System.out.println("The rectangle's area is "+wid*len);      
    }
}
// 类 三角形
 class triganle extends shape implements shape_area
{
    double x,y,z;
    triganle(double a,double b,double c)
    {
        x=a;
        y=b;
        z=c;
    }
    public void width()
    {
        System.out.println("The triangle's wid is "+(x+y+z));
    }   
    
    public void area()
    {
        double d;
        d=(x+y+z)/2;
        System.out.println("The triangle's area is "+Math.sqrt(d*(d-x)*(d-y)*(d-z)));
    }
}
//类 圆
class circle extends shape implements shape_area
{
    double radius;
    circle(double x)
    {
        radius=x;
    }
    public void area()
    {
        System.out.println("The area of the circle is "+3.14*radius*radius);
    }
    public void width()
    {
        System.out.println("The width of the circle is "+3.14*2*radius);
    }
}
//类 四边型
 class polygon extends shape implements shape_area
{
    double m,n,p,q;//四条边的长度
    double x;
    polygon(double a,double b,double c,double d,double e)
    {
        m=a;n=b;p=c;q=d;x=e;
    }
    
    public void area()
    {
                double d,sum=0;
                d=(m+n+x)/2;
                sum+=Math.sqrt(d*(d-m)*(d-n)*(d-x));
                d=(p+q+x)/2;
                sum+=Math.sqrt(d*(d-p)*(d-q)*(d-x));
        System.out.println("The area of the polygon is "+sum);
    }
    public void width()
    {
        System.out.println("The width of the polygon is "+m+n+q+p);
    }
}

包使用:
import package1.*;
class graphshape
{
    public static void main(String args[])
    {
       rectangle rec1=new rectangle(2.0,3.0);
           rec1.area();
           circle cir1=new circle(2.0);
           cir1.area();
           triganle tri1=new triganle(3.0,4.0,5.0);
           tri1.width();
           polygon pol1=new polygon(5,6,7,8,9);
           pol1.area();
           pol1.width();
    }


错误:
The type rectangle is not visible
The type circle is not visible
The type triganle is not visible
The type polygon is not visible

回复列表 (共5个回复)

沙发

请各位大侠解释一下错误的原因,感激不尽,先在这里谢过了!

板凳

用import语句只能导入包中的public声明的类.

3 楼

你的编译有问题.我用你的源代码没有问题啊.可能是你的机子问题!嘿嘿!
包:
package computeArea;

interface  shape_area
{
    void area();
}
//抽象类 shape
abstract class shape
{
    abstract void width();
}
//类 矩形
class rectangle  extends shape implements shape_area
{
    public double wid;
    public double len;
    rectangle(double x,double y)
    {
         wid=x;
         len=y;        
    }
    public void width()
     {
        System.out.println("The rectangle's area is "+(wid+len)*2);
     }

    public void  area()
    {
        System.out.println("The rectangle's area is "+wid*len);      
    }
}
// 类 三角形
 class triganle extends shape implements shape_area
{
    double x,y,z;
    triganle(double a,double b,double c)
    {
        x=a;
        y=b;
        z=c;
    }
    public void width()
    {
        System.out.println("The triangle's wid is "+(x+y+z));
    }   
    
    public void area()
    {
        double d;
        d=(x+y+z)/2;
        System.out.println("The triangle's area is "+Math.sqrt(d*(d-x)*(d-y)*(d-z)));
    }
}
//类 圆
class circle extends shape implements shape_area
{
    double radius;
    circle(double x)
    {
        radius=x;
    }
    public void area()
    {
        System.out.println("The area of the circle is "+3.14*radius*radius);
    }
    public void width()
    {
        System.out.println("The width of the circle is "+3.14*2*radius);
    }
}
//类 四边型
 class polygon extends shape implements shape_area
{
    double m,n,p,q;//四条边的长度
    double x;
    polygon(double a,double b,double c,double d,double e)
    {
        m=a;n=b;p=c;q=d;x=e;
    }
    
    public void area()
    {
                double d,sum=0;
                d=(m+n+x)/2;
                sum+=Math.sqrt(d*(d-m)*(d-n)*(d-x));
                d=(p+q+x)/2;
                sum+=Math.sqrt(d*(d-p)*(d-q)*(d-x));
        System.out.println("The area of the polygon is "+sum);
    }
    public void width()
    {
        System.out.println("The width of the polygon is "+m+n+q+p);
    }
}
主程序:
package computeArea;

public class ComputeAllArea
{
    public static void main(String[] args)
    {
        rectangle rec1=new rectangle(2.0,3.0);
        rec1.area();
        circle cir1=new circle(2.0);
        cir1.area();
        triganle tri1=new triganle(3.0,4.0,5.0);
        tri1.width();
        polygon pol1=new polygon(5,6,7,8,9);
        pol1.area();
        pol1.width();

    }

}
answer:
The rectangle's area is 6.0
The area of the circle is 12.56
The triangle's wid is 12.0
The area of the polygon is 40.97495135372843
The width of the polygon is 5.06.08.07.0

4 楼

//包1:
package package1;
//接口 面积
public interface  shape_area
{
    void area();
}
//包2:
package package2;
public abstract class shape
{
    abstract void width();
}
//类 rectangle:
import package1.shape_area;
import package2.shape;
class rectangle  extends shape implements shape_area
{
    public double wid;
    public double len;
    rectangle(double x,double y)
    {
         wid=x;
         len=y;        
    }
    public void width()
     {
        System.out.println("The rectangle's area is "+(wid+len)*2);
     }

    public void  area()
    {
        System.out.println("The rectangle's area is "+wid*len);      
    }
}
控制台下包1和包2均生成相应的包,但是类编译rectangle出现错误:rectangle不是抽象的,并且未覆盖package2.shape中的抽象方法width()  
请问怎么修改?

5 楼

是shape类修改之后没有重新编辑吧?看起来没错啊.

我来回复

您尚未登录,请登录后再回复。点此登录或注册