【问题标题】:How does a class use a method defined in another class in Java? [duplicate]一个类如何使用 Java 中另一个类中定义的方法? [复制]
【发布时间】:2016-03-30 11:13:28
【问题描述】:

如何在boxweight类中使用Box类定义的getvolume()方法?我知道我必须实例化 Box 类才能使用它定义的方法,但是怎么做呢?

class Box {
    private int lenght;
    private int breadth;
    private int height;
    int price;

    Box(int l, int b, int h) {
        lenght= l;
        breadth= b;
        height= h;
    }

    public Box(int p) {
        price= p;
    }

    double getvolume() {
        return lenght*height*breadth;
    }

    void setsize(int $l, int $b, int $h  ) {
        lenght= $l;
        breadth= $b;
        height= $h;
    }
}

public class boxclassdemo {
    public static void main(String[] args) {
        Box mybox1=new Box(10,10,10);
        Box mybox2=new Box(5,5,5);
        Box mybox3=new Box(20);

        System.out.println(mybox1.getvolume());
        System.out.println(mybox2.getvolume());
        System.out.println(mybox3.price);
    }
}

boxweight 类:

public class boxweight  {
    int weight;
    int length,breadth,height;  
    public static void main(String[] args) {
        boxweight myboxx =  new boxweight();
        myboxx.weight= 25;
        myboxx.length=10;
        myboxx.breadth=20;
        myboxx.height=30;
    }
}

【问题讨论】:

  • 创建一个Box类的实例并使用它
  • 您需要定义 Box 类的一个对象并调用该实例的方法
  • 并且您想了解 java 编码约定。关于如何命名类、方法等有规则;你似乎不认识他们。对于您的实际问题:这就像超级基础知识;从排序...那好吧,你不应该问其他人。您应该阅读书籍和教程并了解 Java 的基础知识;而不是依靠其他人向您解释。
  • @sᴜʀᴇsʜᴀᴛᴛᴀ 你能详细说明一下吗?
  • @SrujanSujju 那个“东西”是关于这个网站如何运作的礼貌信息。但似乎你没有得到它,所以再说一遍:这不是一个你只需要“帮助”的网站,其他人会花费他们的空闲时间来提供它。例如,预计您会进行先前的研究。此外:你期望别人帮助你;所以你想让它尽可能简单。不遵守约定只会让其他人更难阅读您的代码。所以,我们会帮助你;但你甚至不愿意以最好的方式提供意见?!那么,您为什么认为我们应该帮助您?

标签: java


【解决方案1】:

您可以在Oracle Java Tutorial on Object Creation 上找到您的问题以及您可能很快会遇到的许多其他问题的答案

As you know, a class provides the blueprint for objects; you create an object from a class. Each of the following statements taken from the CreateObjectDemo program creates an object and assigns it to a variable:

Point originOne = new Point(23, 94);
Rectangle rectOne = new Rectangle(originOne, 100, 200);
Rectangle rectTwo = new Rectangle(50, 100);

The first line creates an object of the Point class, and the second and third lines each create an object of the Rectangle class.

实际使用这些也是如此,the very next tutorial:

Code that is outside the object's class must use an object reference or expression, followed by the dot (.) operator, followed by a simple field name, as in:

objectReference.fieldName

我建议你要么开始阅读这些教程,要么给自己买一本好的 java 书,那里有很多。

【讨论】:

    【解决方案2】:

    看起来你想使用继承。如果您这样定义 BoxWeight,请注意类名后面的“extends Box”:

    public class BoxWeight extends Box {
        int weight;
    
        BoxWeight(int l, int b, int h, int w) {
            super(l, b, h);
            weight = w;
        }
    }
    

    然后您可以将 boxweight 对象视为 Box 对象,这意味着您可以使用它的公共方法,如下所示:

    public static void main(String[] args) {
        BoxWeight myboxx =  new BoxWeight(10, 20, 30, 25);
        double volume = myboxx.getvolume();
    }
    

    【讨论】:

      【解决方案3】:

      (顺便说一下,类名的第一个字母通常是向上的。
      如果您使用 Eclipse 或其他 IDE,它会在您执行以下步骤时帮助您创建。

      Box box = new Box(25, 10, 20, 30);
      那么,
      System.out.println(box.getvolume());

      【讨论】:

      • 请花时间更好地格式化您的答案。提示:垂直间距(称为段落)通常使阅读文本更容易。那么:你不能从他的代码中说他需要一个 import ...
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-20
      • 1970-01-01
      • 2023-03-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多