从Java中的另一个类运行方法

时间:2021-05-23 22:22:08

What I am trying to do is run a method from another class, my new Link().populateList();. I've tried extending my Link class and a filling the Link() and populateList() with different parameters to no avail. Can anyone push me in the right direction? Here is my code down below:

我想要做的是从另一个类运行一个方法,我的新Link()。populateList();.我试过扩展我的Link类,用不同的参数填充Link()和populateList()无济于事。任何人都可以把我推向正确的方向吗?以下是我的代码:

Reverse.java Class having main() method

Reverse.java类有main()方法

package main;

public class Reverse {

    public static void main(String[] args){
        new Link().populateList();
    }
}

Link.java

package main;

public class Link {
    public Link next;

    public String name;
    public int age;

    public Link(String name, int age){
        this.name = name;
        this.age = age;
    }

    public void displayInformation(){
        System.out.println("Name: "+ name + ", Age: " + age);
    }

    public void populateList(){
        LinkedList people = new LinkedList();

        people.insertLinks("John", 21);
        people.insertLinks("David", 27);
        people.insertLinks("Abraham", 19);
    }
}

LinkedList.java

package main;

public class LinkedList {

    public Link first;

    public boolean isEmpty(){
        return(first == null);
    }
    public void insertLinks(String name, int age){
        Link link = new Link(name, age);

        link.next = first;
        first = link;
    } 
    public void display(){
        Link link = first;

        while(link != null){
            link.displayInformation();
        }
    }
}

1 个解决方案

#1


3  

public void populateList(){
    LinkedList people = new LinkedList();

    people.insertLinks("John", 21);
    people.insertLinks("David", 27);
    people.insertLinks("Abraham", 19);
}

The method is being run, but it doesn't do anything visible. It creates a linked list, adds three entries, and then throws the list away. It doesn't call people.display(), it doesn't return people;, nothing.

该方法正在运行,但它没有做任何可见的事情。它创建一个链表,添加三个条目,然后抛出列表。它不会调用people.display(),它不返回人物;没有。

#1


3  

public void populateList(){
    LinkedList people = new LinkedList();

    people.insertLinks("John", 21);
    people.insertLinks("David", 27);
    people.insertLinks("Abraham", 19);
}

The method is being run, but it doesn't do anything visible. It creates a linked list, adds three entries, and then throws the list away. It doesn't call people.display(), it doesn't return people;, nothing.

该方法正在运行,但它没有做任何可见的事情。它创建一个链表,添加三个条目,然后抛出列表。它不会调用people.display(),它不返回人物;没有。