简体   繁体   English

如何在Java中使用类和方法

[英]How to use classes and methods in Java

I'm fairly new to Java and coding, but up until this point in the Java material, I haven't ran into any problems. 我对Java和编码还很陌生,但是到Java材料为止,我还没有遇到任何问题。 What I really can't wrap my head around is how classes and methods actually work. 我真正无法确定的是类和方法的实际工作方式。 I've been attempting to implement this over the last several hours to no avail: 在过去的几个小时中,我一直在尝试实施此方法,但无济于事:

Implement the class called Cylinder shown in UML below. 实现下面的UML中显示的名为Cylinder的类。 The constructor accepts and initializes the radius and height for the Cylinder, while accessors and mutators allow them to be changed after object construction. 构造函数接受并初始化Cylinder的半径和高度,而访问 mutator则允许在构造对象之后对其进行更改。 The class also include methods that calculate and return the volume and surface area of the Cylinder. 该类还包括计算并返回圆柱体的体积和表面积的方法。 Lastly, it contains a toString method that returns the name of the shape, its radius, and its height. 最后,它包含一个toString方法,该方法返回形状的名称,其半径和高度。 Create a main method which instantiates 4 Cylinder objects (any parameters), display them with toString() , change one parameter (your choice) in each, and display them again. 创建一个实例化4个Cylinder对象(任何参数)的main方法,使用toString()显示它们,在每个对象中更改一个参数(您的选择),然后再次显示它们。

UML: UML:

在此处输入图片说明

This is the code that I currently have: 这是我目前拥有的代码:

class Cylinder {
    private double radius;
    private double height; 


    // Method #1
    private void rad (){

    }

    // Constructor
    public Cylinder (double radius, double height){
        this.radius = radius; 
        this.height = height;
    }


    // Method #2 for calculating volume.
    double calcVolume (){
        double volume = Math.PI * Math.pow(radius, 2) * height; 
        return volume; 
    }

    // Method #3 for calculating surface area.
    double calcArea (){
        double area = (2 * Math.PI * radius * height) + (2 * Math.PI * Math.pow(radius, 2));
        return area;
    }

    // toString method.
    public String toString (){
        StringBuilder sb = new Stringbuilder();
        sb.append(radius).append(height);
        return sb.toString();

    }
}

public class Murray_A03Q1 { 公共课Murray_A03Q1 {

public static void main(String[] args) {

    Cylinder cylinder1 = new Cylinder(5, "can");
    System.out.println(cylinder1);

    Cylinder cylinder2 = new Cylinder(6, "cup");

    Cylinder cylinder3 = new Cylinder(7, "jar");

    Cylinder cylinder4 = new Cylinder(8, "paper roll");
}       

} }

What I really don't understand is how to use the 'get' and 'set' methods. 我真正不了解的是如何使用“ get”和“ set”方法。 Additionally, I'm not completely sure how to implement the toString method. 另外,我不确定如何实现toString方法。

The following errors that I can't figure out how to correct are: 我无法弄清楚如何纠正以下错误:

  1. The constructor Cylinder() is undefined for - Cylinder cylinder1 = new Cylinder(); 对于以下对象,未定义构造函数Cylinder() -Cylinder cylinder1 = new Cylinder();

  2. Stringbuilder can't be resolved to a type for - StringBuilder sb = new Stringbuilder(); Stringbuilder无法解析为sb = new Stringbuilder();的类型sb = new Stringbuilder();

Thank you for your help! 谢谢您的帮助!

What I really don't understand is how to use the 'get' and 'set' methods. 我真正不了解的是如何使用“ get”和“ set”方法。

The purpose of getters and setters is for encapsulation. 获取器和设置器的目的是用于封装。 They allow you to get or set the values of the class' variables without having to declare them as being public. 它们允许您获取或设置类变量的值,而不必将其声明为公共变量。

For example, if you were to have 例如,如果您要

public double radius;
public double height;

You will be able to access them as 您将能够以以下身份访问它们

cylinder1.radius = 1;
cylinder2.height = 10;
int a = cylinder3.radius;
int b = cylinder3.height + cylinder4.radius;

etc. 等等

Now if instead we had 现在如果相反

private double radius;
private double height;

The above code will fail. 上面的代码将失败。 Which is why we need getters and setters. 这就是为什么我们需要吸气剂和吸气剂。 As the names imply, getters "get" the variable. 顾名思义,getter“获取”变量。

public double getHeight() {
    return height;
}

While setters "set" the variable. 当设置员“设置”变量时。

public void setHeight(double height) {
    this.height = height;
}

As for why we do it this way, theres a lot of information on that. 至于为什么我们要这样做, 有很多信息。

Additionally, I'm not completely sure how to implement the toString method. 另外,我不确定如何实现toString方法。

As for how to implement the toString() method, all it has to do is return a String . 至于如何实现toString()方法,它要做的就是返回String But as for what the String contains, there is no hard rule for that. 但是对于String包含的内容,没有硬性规定。 A good start would be to return the radius and height, like you have done. 一个好的开始就是像您一样返回半径和高度。

The constructor Cylinder() is undefined for - Cylinder cylinder1 = new Cylinder(); 对于以下对象,未定义构造函数Cylinder()-Cylinder cylinder1 = new Cylinder();

Your constructor is public Cylinder (double radius, double height) . 您的构造函数是public Cylinder (double radius, double height) However, you are trying to make a cylinder that's an int and a String . 但是,您正在尝试使圆柱体为intString

Cylinder cylinder1 = new Cylinder(5, "can");

Either 要么

  • Change the constructor to something like public Cylinder(double radius, String name); 将构造函数更改为public Cylinder(double radius, String name); or 要么
  • The instantiate your Cylinders with two doubles, a radius and a height. 用两个双精度半径和高度实例化圆柱体。 eg 例如

     Cylinder cylinder1 = new Cylinder(1.0, 2.0); 

Stringbuilder can't be resolved to a type for - StringBuilder sb = new Stringbuilder(); Stringbuilder无法解析为-StringBuilder sb = new Stringbuilder();的类型。

The only problem here is that you forgot to capitalize the b . 唯一的问题是您忘记了b大写。 It's StringBuilder not Stringbuilder . 它是StringBuilder而不是Stringbuilder

An example of get and set methods: getset方法的示例:

 double getRadius() {
    return radius;
 }

 void setRadius(r) {
    radius = r;
 }

The constructor Cylinder() is undefined for - Cylinder cylinder1 = new Cylinder(); 对于以下对象,未定义构造函数Cylinder()-Cylinder cylinder1 = new Cylinder();

Something is attempting to invoke the default constructor (a constructor with no parameters). 正在尝试调用默认构造函数(无参数的构造函数)。 You can either find out exactly where this error is called, or else add a default constructor: 您可以确切地找到此错误的调​​用位置,或者添加默认的构造函数:

Cylinder() {
    this.radius = 0;
    this.height = 0;
}

Stringbuilder can't be resolved to a type for - StringBuilder sb = new Stringbuilder(); Stringbuilder无法解析为-StringBuilder sb = new Stringbuilder();的类型。

StringBuilder is actually the class java.lang.StringBuilder . StringBuilder实际上是java.lang.StringBuilder类。 Place this text at the top of your file to help it resolve the name. 将此文本放在文件顶部以帮助其解析名称。

import java.lang.StringBuilder;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM