有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

导入Java中的类?

为了让这个程序运行,我必须将“derp”方法从Shape类导入到rectangle类中。但不管我自己或导师怎么努力,我们都无法让它发挥作用。我实际上需要完成的是,Shape中的“derp”方法需要能够在矩形中工作。然而,引进这种方法的任务让我们感到困惑

public abstract class shape{

    shape(){

    }
    shape(int length, int width, int thing ){
        length = 0;
        width = 0;
    }
    public int derp(int thing, int length) {
        thing = (int) Math.random() * 9 ;
        length = thing;
        return length;
    }
}

public class Rectangle extends shape {

public static void main(String args[])
{
        shape.getLength(Length, Width);
            //r1 will take all default value
Rectangle r1 = Rectangle();        

//r2 will take all supplied value
Rectangle r2 = Rectangle(4, 5);  

//r3 will take supplied length.  width will take default value
Rectangle r3 = Rectangle(10);    

//r4 will take the same value of r2
Rectangle r4= r2;           

//the rest of the code
}
private static void Rectangle(int width2, int length2) {
    // TODO Auto-generated method stub

}

}


共 (4) 个答案

  1. # 1 楼答案

    不能将类外的方法导入其他类。从逻辑上讲,您希望Rectangle扩展形状(根据命名约定,也应该是Shape),但您的做法正好相反

    然而,在你的代码中有很多东西是没有意义的,所以你可能想用清晰的英语解释你想要完成什么

  2. # 2 楼答案

    好的。。所以你的类形状延伸到了矩形。。。我们将忽略这一不同寻常的逻辑

    Rectangle.derp(int thing, int length, int width);// wrong
    

    首先,不能通过在调用中声明参数来调用方法derp。你可以打这样的电话,例如:

    int thing = 1, length = 2, width = 3;
    Rectangle.derp(thing, length, width);//with condition that derp method 
    //declaration to be public static
    
    Rectangle r1 = Rectangle();//wrong
    

    这就是类构造函数。除非你想让你的班级成为单身学生,否则你就把它宣布为公共的。。但我怀疑。你可以用“new”键对其进行实例化,如下所示:

    Rectangle r1 = new Rectangle();
    

    具有1个和2个参数的参数也一样

    此处是矩形类的完整代码:

    public class Rectangle {
        public Rectangle(int width2, int length2) {
            // TODO: Process width and length here
        }
    
        public Rectangle() {
            // TODO: process default values here
        }
    
        public Rectangle(int value) {
            // TODO: Process value here
        }
    
        public static void derp(int thing, int length, int width) {
    
        }
    
        public static void main(String args[]) {
    
            int thing = 1, length = 2, width = 3;
            Rectangle.derp(thing, length, width);
    
            // r1 will take all default value
            Rectangle r1 = new Rectangle();
    
            // r2 will take all supplied value
            Rectangle r2 = new Rectangle(4, 5);
    
            // r3 will take supplied length. width will take default value
            Rectangle r3 = new Rectangle(10);
    
            // r4 will take the same value of r2
            Rectangle r4 = r2;
    
            // the rest of the code
        }
    
    }
    
  3. # 3 楼答案

    1. Rectangle.derp(int thing, int length, int width);

      这不是方法声明。这种语法完全错误。您是在尝试将derp声明为可以使用的方法,还是在尝试调用derp?作为一名读者,这一点尚不清楚

    2. 从逻辑上讲,Rectangle应该扩展Shape,而不是反过来

    3. 这没什么用:

      shape(int length, int width, int thing ){
          length = 0;
          width = 0;
      }
      

      您只是将参数分配给不同的值

    休息一下,花些时间学习Java的语法

  4. # 4 楼答案

    Rectangle应该扩展Shape,而不是相反

    同时删除这一行

    Rectangle.derp(int thing, int length, int width);