我有:

/*
 * File: NameSurferEntry.java
 * --------------------------
 * This class represents a single entry in the database.  Each
 * NameSurferEntry contains a name and a list giving the popularity
 * of that name for each decade stretching back to 1900.
 */

import acm.util.*;
import java.util.*;

public class NameSurferEntry implements NameSurferConstants {

/* Constructor: NameSurferEntry(line) */
/**
 * Creates a new NameSurferEntry from a data line as it appears
 * in the data file.  Each line begins with the name, which is
 * followed by integers giving the rank of that name for each
 * decade.
 */
    public NameSurferEntry(String line) {
        findName(line);
        findDecades(line);
    }
...


作为一个班级。

我将如何在另一个类中调用方法NameSurferEntry。

最佳答案

NameSurferEntry是构造函数,而不是方法。创建非默认构造函数将隐藏默认的空构造函数。所以

// asume line to be a string containing a line
NameSurferEntry entry = new NameSurferEntry(line);


将是创建NameSurferEntry对象的唯一方法。

10-06 12:38