简体   繁体   English

尝试编写一个不带参数并返回双精度值的公共方法 costMultiplier()

[英]Trying to write a public method costMultiplier() that takes no arguments and returns a double

The method should return 1.2 for postcodes beginning “WC1A” or “EC1A”, and 1.0 otherwise.对于以“WC1A”或“EC1A”开头的邮政编码,该方法应返回 1.2,否则返回 1.0。 I am not sure if I have done it right.我不确定我是否做得对。

public double costMultiplier(final String postCode) {
    double multiplier = 1.0d;  // Default`

  String pc = postCode.trim().toUpperCase(); ` 

    if (pc.startsWith("WC1A") || pc.startsWith("EC1A")) {
        multiplier = 1.2;  // For London
    }

    return multiplier;
}

Also would I supply a specific postal code to this method, for example:我还会为此方法提供特定的邮政编码,例如:

double multiplier = costMultiplier("EC1A 9DT");
System.out.println("Determined Multiplier = " + multiplier);

Also how can I use a string method such as substring() for the answer and not sure whether public is best or private?另外,我如何使用诸如 substring() 之类的字符串方法作为答案,而不确定 public 是最好的还是私有的?

Okay, a few things.好的,有几件事。

First, for a short list of postal codes, your example code looks good.首先,对于邮政编码的简短列表,您的示例代码看起来不错。 Is it not working?它不工作吗?

As for substring... There are two forms of substring.至于子串……子串有两种形式。 One takes a beginning index (starting at 0) and goes through the end of the string.一个开始索引(从 0 开始)并通过字符串的末尾。 The other takes a start and stop.另一个开始和停止。 The ending value is AFTER the last character, so for example:结束值在最后一个字符之后,例如:

String pc = oldString.substring(0,4);

This is actually convenient.这其实很方便。 When grabbing the beginning of the string, then the second parameter is the index to end at, but it's also the length.当抓取字符串的开头时,第二个参数是结束的索引,但它也是长度。 So for a 10-character string, you'll get the first four characters, at indexes 0 through 3.因此,对于 10 个字符的字符串,您将获得前四个字符,索引为 0 到 3。

So, for your specific problem:因此,对于您的具体问题:

public double costMultiplier(String postCode) {
    double retVal = 1.0;
    String first = postCode.trim().substring(0,4).toUpperCase();
    if (first.equals("WC1A") || first.equals("EC1A")) {
        retVal = 1.2;
    }

    return retVal;
}

Now, as for private versus public... It depends on who is going to call it.现在,至于私人与公共……这取决于谁将调用它。 Is this method only for internal use?这个方法只供内部使用吗? Then make it private.然后将其设为私有。 If you need to be able to call it from another class, then make it public.如果您需要能够从另一个类调用它,请将其设为公开。 This smells like something you could make public.闻起来像是你可以公开的东西。 Private methods are typically those that do the internals of your class's work.私有方法通常是执行类工作内部的方法。

At your stage of learning, you could just make all your methods public, and start thinking about public versus private interfaces later.在您的学习阶段,您可以将所有方法公开,然后开始考虑公共接口与私有接口。 It's sort of advanced.有点高级啊But basically, think of it this way.但基本上,这样想。

Public methods are what you're exposing to the outside world.公共方法是您向外界公开的内容。 Imagine someone else using your code.想象一下其他人使用您的代码。 Imagine you're going to have coworkers who don't want to know how your code works;想象一下,你的同事不想知道你的代码是如何工作的; they're only going to use it.他们只会使用它。 Public methods are the ones they can count on.公共方法是他们可以依赖的方法。 Private methods can change, and no one cares but the people working on the code.私有方法是可以改变的,除了处理代码的人,没有人关心。

It's more complicated than that, but I've never seen that good of an explanation.它比那更复杂,但我从来没有见过这么好的解释。 It really is up to your experience to guide you, and right now, you don't have any experience to use as a guide.这真的取决于你的经验来指导你,而现在,你没有任何经验可以用作指导。

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

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