繁体   English   中英

方法中的Java方法调用

[英]Java method call in method

我正在学习Java。 我以前已经学过ruby,所以我使用了一些我知道可以正常工作的ruby代码,并尝试转换为java,但是我对此有些迷茫,所以在此先感谢。 我尝试了延伸但仍然无法正常工作:(

我创建了一个访问者类,其中包含姓名,性别,年龄和公民身份。 我正在House类中尝试使用新方法AdmitNewVisitor,可以在其中手动在测试中插入新的Visitor。 因此,该代码在下面起作用,但是下一个代码将不起作用,我不知道为什么,我已经搜索了数小时,而我的书没有示例。

我目前在做什么

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

我想做的事

我更改了完整代码中定义的<numVisitors(最多100个)

    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");

在红宝石中,你会做

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 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;
    }
}

编辑4忘记了这一点,包括在AdmintNewVisitor所在的HouseTour类中

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

编辑:解决

好吧,我一直在修改我的代码,我想我知道这个问题。 多亏了所有@Dan Temple的帮助,当我不那么累的时候,我应该可以解决这个问题,所以我确切地知道我哪里出了问题,但是我最终使用了下面的代码,之前定义了numVisitors = 0,所以首先是numVisitors ++,然后减1以获得数组中的位置。 我相信,而且我可能是错的,问题是在列出我的公共String toString()方法之前,该方法涉及到访问者,然后才列出我的AdmitNewVisitor,或者也许我很累。 在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);    
}

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

如果创建并维护访问者列表,则只需创建“访问者”对象并将其添加到列表中即可。 然后,该列表具有size()方法,该方法将允许您在需要时获取访问者的数量。

在我的示例中,我使用了ArrayList(最常见的类型),但是可以使用许多列表(甚至其他集合)。 此页面可能很有用。

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();
    }
}

编辑:

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

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;
    }
}

编辑:

您可以通过维护自己的访问者人数和人数来做到这一点,但是您需要考虑以下几点:

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

根据注释中的要求,没有代码解决方案,只有上述提示。

编辑:

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

从我的收集中,您需要我之前提供的访问者类。 对于您的整体类而言,这是一个单独的类,用于监视访问者的数组(或列表)。 此类表示您的访问者对象。 在您的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);
}

希望能有所帮助。 测试始终很重要,但是在学习过程中,不要对测试方法的格式/语法感到困惑。

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

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

这将永远循环,因为您在循环中增加了numVisitors,同时将其用作限制。 即使它不会永远循环,也没有任何意义,因为它将用相同的名为Visitor填充您的visitors数组。 因此,如果您有5位访客,并且您接纳了一个新访客“乔治”,那么您将在那里有6位“乔治”。

如果删除for循环,它将为您的数组添加一个新的Visitor。

好吧,一些提示给您:

  • 在Java中,应使用List而不是简单数组,以避免IndexOutOfBounds异常。
  • 在ruby数组中,它类似于Java列表,因此您应该push新访问者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之间选择。 当您想经常调整大小时,首先需要。 当您要经常访问其元素时,需要第二个。

祝好运!

暂无
暂无

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

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