【发布时间】:2014-05-28 07:29:30
【问题描述】:
基本上,我想确保我做对了。我在另一个类中调用这些方法。这些方法指定返回一个变量,所以在实例化它们时,我应该将 null 放在括号中吗?我只是想确保我做对了。
public class IntSLList {
protected static IntSLLNode head, tail;
public void createInst(){
new IntSLList().isEmpty();
new IntSLList().addToHead(null);
new IntSLList().addToTail(null,null);
new IntSLList().deleteFromTail();
new IntSLList().printAll();
new IntSLList().isInList(null);
new IntSLList().delete(null);
}
public IntSLList() {
//code
}
public static boolean isEmpty() {
//code }
public static void addToHead(String AN) {
//code
}
public static void addToTail(String AN, Double AB) {
//code
}
public static String deleteFromHead() { // delete the head and return its info;
//code
}
public static String deleteFromTail() { // delete the tail and return its info;
//code
}
public static void printAll() {
//code
}
public static boolean isInList(String AN) {
//code
}
public static void delete(String AN){
//code
}}
提前谢谢大家!
【问题讨论】:
-
createInst()应该做什么? -
您确定希望您的字段和方法是静态的吗?此外,head 和 tail 的默认值为 null。
-
我不明白你想要达到什么目的。你在这里有一个构造函数。其余的都是方法。它们是静态方法,因此应该在类上调用它们:例如
IntSLList.isEmpty()。你作为参数传递的就是你想要作为参数传递的东西。如果你想传递null,你传递null。如果你想传递别的东西,你就传递别的东西。 -
您不“实例化”方法,而是调用它们。为什么要将 null 作为参数传递,除非它需要一个(任何类型的)对象作为参数,而你没有要传递的东西?
-
@UberPwnd:这不是一个静态方法的好理由。 Java 程序总是从运行 main 方法开始,该方法是静态的。根据您的推理,每个类的每个方法都是静态的,因为它是从静态的 main 调用的。您确实需要阅读有关对象、方法和构造函数的 Java 教程。
标签: java variables methods instantiation