【问题标题】:Java method call in method方法中的Java方法调用
【发布时间】:2013-11-22 07:26:48
【问题描述】:

我正在学习java。我以前学过 ruby​​,所以我正在使用一些我知道有效的 ruby​​ 代码并尝试转换为 java,但是我对这个有点迷茫,所以提前感谢。我尝试了扩展,但仍然无法正常工作:(

我创建了一个包含姓名、性别、年龄和公民身份的访问者类。我试图在 House 类中有一个新方法 AdmitNewVisitor,我可以在测试中手动插入一个新的访问者。所以,这段代码在下面可以工作,但是下一个代码将无法工作,我不知道为什么,我已经搜索了几个小时,但我的书没有示例。

我目前在做什么

    public void AdmitNewVisitor( )
    {
        for (int i = 0; i < 2; i++)
        {
            numVisitors++;
            visitors[numVisitors - 1] = new Visitor("Grates, Brill", "M", 66, "Alien");
        }
    }

我想做的事

我已更改 i

    public void AdmitNewVisitor(String theName, String theGender, int theAge, String theCitizenship )
    {
        for (int i = 0; i < numVisitors; i++)
        {
            numVisitors++;
            visitors[numVisitors - 1] = new Visitor(theName, theGender, theAge, theCitizenship);
        }
    }

新的测试方法

t.AdmitNewVisitor("Grates, Brill", "M", 66, "Alien");

在 ruby​​ 中你会这样做

def admit_new_visitor the_name, the_gender, the_age, the_citizenship
  @num_visitors += 1
    @visitors[@num_visitors - 1] = Visitor.new(the_name, the_gender, 
  the_age, the_citizenship)
end

编辑:忘记指定错误,抱歉漫长的一天 当我执行 toString() 时,错误是没有显示任何内容,没有错误消息,只是打印我的字符串并且没有访问者应该出现的访问者。 我现在看到我不应该在循环中运行它,因为这没有意义,当我尝试时

public void AdmitNewVisitor(String theName, String theGender, int theAge, String theCitizenship)
{
    numVisitors++;
    visitors[numVisitors - 1] = new Visitor(theName, theGender, theAge, theCitizenship);
}

我收到错误:

error: constructor Visitor in class Visitor cannot be applied to given types
     {
     ^
Required: String, String, int, String
found: no arguements
reason: actual and formal arguments differ in length

我相信是指Visitor类中的这行代码,所有变量在Visitor中都是public的

public Visitor(String theName, String theGender, int theAge, String theCitizenship)

编辑:每个人都非常乐于助人,但是由于我是从头开始学习并且还没有进入列表,有没有办法按照我尝试的方式做到这一点?蚂蚁一口咬掉大象


编辑 3 个访客类

import java.util.*;

public class Visitor
{
    // Define Instance Variables
    public String name;
    public String gender;
    public int age;
    public String citizenship;
    public int currentRoom;

    // Define Constructor
    public Visitor(String theName, String theGender, int theAge, String theCitizenship)
    {
        name = theName;
        gender = theGender;
        age = theAge;
        citizenship = theCitizenship;
        currentRoom = 0;
    }

    public String toString( )
    {
        String output = String.format("%-20s %-15s %d", name, citizenship, currentRoom);
        return output;
    }
}

Edit 4 忘了这个,包含在 AdmintNewVisitor 所在的 HouseTour 类中

public Visitor[ ] visitors = new Visitor[100];

编辑:已解决

嗯,我一直在修改我的代码,我想我知道问题所在。感谢所有人,但特别感谢@Dan Temple,当我不那么累的时候,我可能应该在这方面工作,所以我确切地知道我哪里出错了,但我最终使用了下面的代码,之前定义了 numVisitors = 0 所以 numVisitors++ 先出现,然后减 1 以进入数组。我相信,我可能是错的,问题是在列出我的 AdmitNewVisitor 之前列出涉及访问者的公共 String toString() 方法,或者我只是累了。在 ruby​​ 中,在我的 new_visitor 方法之前列出我的 to_s(java 中的 toString())没有问题。再次感谢,很高兴知道我并不孤单:{o :{/ :)

public void AdmitNewVisitor(String theName, String theGender, int theAge, String theCitizenship)
{
    numVisitors++;  
    visitors[numVisitors - 1] = new Visitor(theName, theGender, theAge, theCitizenship);    
}

【问题讨论】:

  • SSCCE
  • 我想如果我没有发布别人会说的所有内容,嘿发布更多代码!
  • 发布足够多的代码来指出您的问题,仅此而已。
  • 好的,我现在要编辑和删除,谢谢提示
  • “但是下一个代码将无法正常工作,我不知道为什么” - 请告诉我们它不起作用是什么意思?你有什么错误吗?意外输出?

标签: java ruby methods call instance


【解决方案1】:

Java 的乐趣在于它的面向对象特性。 您可以实现 Ruby 所做的一切,但让 List 为您完成所有繁重的工作。

如果您创建并维护访问者列表,那么您可以简单地创建访问者对象并将其添加到列表中。 然后列表有一个size() 方法,可以让您在需要时获取访问者的数量。

在我的示例中,我使用了 ArrayList(最常见的类型),但有许多列表(实际上还有其他集合)可以使用。 This page 可能很有用。

import java.util.ArrayList;
import java.util.List;

public class Visitors {

    private final List<Visitor> visitors;

    public Visitors() {
        this.visitors = new ArrayList<Visitor>(); //Initialise the list within the constructor here.
    }

    public void AdmitNewVisitor(final String theName, final String theGender, final int theAge, final String theCitizenship ) {
        //create your new visitor with the new keyword. Then add it to the list of visitors.
        final Visitor visitor = new Visitor(theName, theGender, theAge, theCitizenship);
        this.visitors.add( visitor );
    }

    public Integer getNumberOfVisitors() {
        //There's no need to maintain the number of visitors yourself, you can let the List do it.
        return this.visitors.size();
    }
}

编辑:

当然,这依赖于具有正确构造函数的访问者对象:

public class Visitor {

    private final String theName;
    private final String theGender;
    private final int theAge;
    private final String theCitizenship;

    public Visitor(final String theName, final String theGender, final int theAge, final String theCitizenship) {
        this.theName = theName;
        this.theGender = theGender;
        this.theAge = theAge;
        this.theCitizenship = theCitizenship;
    }

    public String getTheCitizenship() {
        return theCitizenship;
    }

    public int getTheAge() {
        return theAge;
    }

    public String getTheGender() {
        return theGender;
    }

    public String getTheName() {
        return theName;
    }
}

编辑:

您可以维护自己的数组和访问者数量,但您需要考虑以下几点:

  • 需要将访问者的数量定义为实例/静态字段。 (就我个人而言,我会使用实例并在使用时实例化访客类。)
  • 您一次只添加一个访问者,因此您的 addNewVisitor 方法中无需循环。
  • 数组从 0 开始索引,所以第一个访问者在索引 0 处,最后一个访问者在索引 ( this.visitors.length - 1 ) 处。在将访问者添加到数组后增加访问者的数量将避免任何索引算法。
  • 在维护阵列时,请记住它不会自行扩展。如果您有一个大小为 10 的数组,但是您尝试将访问者添加到索引 11(您会得到第 11 个访问者!),那么您将需要将数组内容复制到更大的数组中。这就是我们在 Java 中使用列表的原因。他们为我们处理这个动态尺寸问题。

按照cmets的要求,没有代码解决方法,就是上面的提示。

编辑:

使用数组添加解决方案并手动维护访问者数量。我暂时忽略数组的动态大小问题。 (数组将被初始化为很大)

据我所知,您需要我之前给出的访问者类。这是一个单独的类,用于监视访问者数组(或列表)的整个类。 此类代表您的访问者对象。在您的 Ruby 中,您有 Visitor.new(the_name, the_gender, the_age, the_citizenship)。这在 Java 中变为 new Visitor(theName, theGender, theAge, theCitizenship)

@num_visitors 需要在添加访问者的方法之外定义,因为它是一个全局维护的值,它不仅仅存在于 admitNewVisitor 方法中。

唯一需要注意的是,我在添加访问者后增加 numberOfVisitors 以避免进行索引运算。

public class Visitors {

    private int numberOfVisitors = 0;
    private final Visitor[] visitors;

    public Visitors() {
        this.visitors = new Visitor[250];
    }

    public void AdmitNewVisitor(final String theName, final String theGender, final int theAge, final String theCitizenship ) {
        final Visitor visitor = new Visitor(theName, theGender, theAge, theCitizenship);
        this.visitors[this.numberOfVisitors] = visitor;
        this.numberOfVisitors++;
    }

    public Integer getNumberOfVisitors() {
        return this.numberOfVisitors;
    }
}

对此的测试可能如下所示:

@Test
public void thatnumberOfVisitorsIncreasesWhenAVisitorIsAdmitted() {
    final Visitors visitors = new Visitors();

    Assert.assertEquals(visitors.getNumberOfVisitors(), (Integer) 0);

    visitors.AdmitNewVisitor("name", "male", 50, "British");

    Assert.assertEquals(visitors.getNumberOfVisitors(), (Integer) 1);

    visitors.AdmitNewVisitor("name2", "female", 48, "British");

    Assert.assertEquals(visitors.getNumberOfVisitors(), (Integer) 2);
}

希望对您有所帮助。 测试总是很重要,但在学习时不要太拘泥于测试方法的格式/语法。

【讨论】:

  • 好的,谢谢,我会查看列表。我正在使用的这本书有点小步骤,就我个人而言,如果我不明白如何按照我尝试的方式去做,我知道这是可以做到的,它会让我发疯,有没有以我尝试的方式去做,我认为这是可能的?
  • 这可能吗,我不是说解决它,我什至不知道谷歌搜索什么了,我什至不知道我尝试的方式的正确措辞在java中
  • 不用担心@thwerp。希望这些编辑可以帮助您了解您所处的位置。继续学习!
  • 谢谢@Dan Temple,我的意思是不解决,因为我知道人们来这里只是想要严格的答案,但我想学习,但是地狱,我会接受答案,这样我就可以学习(如果这使得任何意义),这本书看不到任何地方,列表目前与我在文本中所处的位置相差无几,一旦我通过我的方法解决它,我肯定会使用列表方法,我现在只是一只乌龟
  • 好吧,我放弃了,我不能用我的方法解决它,如果你知道我用我的方法做错了什么并且你有时间,你能告诉我吗?我知道这要求很多,因为您非常乐于助人,我无法超越它:(
【解决方案2】:

我认为您不想在方法中保留 for 循环。

    for (int i = 0; i < numVisitors; i++)
    {
        numVisitors++;
        visitors[numVisitors - 1] = new Visitor(theName, theGender, theAge, theCitizenship);
    }

这将永远循环,因为您在循环中增加了 numVisitors,同时使用它作为限制。即使它没有永远循环也没有意义,因为它会用同名的访问者填充您的访问者数组。因此,如果您有 5 位访问者并且您接纳了一位新访问者“George”,那么您将有 6 个“Georges”。

如果您删除 for 循环,它将向您的数组添加一个新的访问者。

【讨论】:

    【解决方案3】:

    好吧,给你一些提示:

    • 在java中你应该使用List而不是简单的数组来避免IndexOutOfBounds异常。
    • 在 ruby​​ 中,数组类似于 javas 列表,因此您只需 push 您的新访问者即可。
    • 这里有一个不定式循环:HouseTour#AdmitNewVisitor

    你想要一个代码?得到一些:

    // java 7.
    
    public class HouseTour {
        // Define Instance Variables
        private List<Visitor> visitors = new LinkedList<>;
        //...
    
        /*
        *  renamed method to java-style.
        */
        public void admitNewVisitor (String theName, String theGender, 
                                    int theAge, String theCitizenship){
           numVisitors ++;
           visitors.push(new Visitor(theName, theGender, theAge, theCitizenship));
        }
    
    }
    

    您可以在 LinkedList 和 ArrayList 之间进行选择。当您想经常调整它的大小时,首先需要它。当您想要经常访问其元素时,需要第二个。

    祝你好运!

    【讨论】:

    • 我是否必须使用此方法更改我的访问者类。由于我是n00b,没听说过这个,从最底层开始,现在我来了……
    猜你喜欢
    • 1970-01-01
    • 2012-09-28
    • 2016-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-03
    • 2013-12-13
    • 1970-01-01
    相关资源
    最近更新 更多