[英]Why does daes date1.compareTO(date2)==0 even if dates are the same doesnt give me what i want?
[英]Why a list give me a value, yet the other doesnt?
public class INode
{
private int value;
private INode right, down;
private int row, col;
public INode(int value)
{
this.value = value;
}
public int getValue()
{
return value;
}
public void setValue(int value)
{
this.value = value;
}
public INode getRight()
{
return right;
}
public void setRight(INode right)
{
this.right = right;
}
public INode getDown()
{
return down;
}
public void setDown(INode down)
{
this.down = down;
}
public int getRow()
{
return row;
}
public void setRow(int row)
{
this.row = row;
}
public int getCol()
{
return col;
}
public void setCol(int col)
{
this.col = col;
}
}
我可以得到a = 8的值,但是對於頭部來說,即使我使用構造函數進行設置,仍然可以給我value = null ...不知道為什么。
驅動程序是:
import java.util.*;
public class List
{
public static INode head;
public List()
{
head = new INode(8);
}
public static void main (String[] args)
{
INode a = new INode(8);
int data = a.getValue();
System.out.println(data);
System.out.println(head.getValue());
}
}
請幫幫我。 不明白為什么當我使用構造函數時,不能將值分配給節點,但是當我創建實例時,我可以...
謝謝大家,愛你們! 很大的幫助!
您沒有實例化類List
。 將您的代碼更改為
public INode head; // remove static
public List() {
head = new INode(8);
}
並修改您的主要方法:
public static void main (String[] args) {
INode a = new INode(8);
int data = a.getValue();
System.out.println(data);
List l = new List(); // create new List instance
System.out.println(l.head.getValue()); // get the head from List instance
}
另一種有效的替代方法是只更改一行:
public static INode head = new INode(8); // create instance during class initialization
我建議看一下類(靜態)變量和實例變量之間的區別,例如在Java教程中 (摘錄如下):
實例變量(非靜態字段)從技術上講,對象將其各自的狀態存儲在“非靜態字段”中,即聲明為不帶static關鍵字的字段。 非靜態字段也稱為實例變量,因為它們的值對於類的每個實例(換句話說,對於每個對象)都是唯一的。 一輛自行車的currentSpeed與另一輛自行車的currentSpeed無關。
類變量(靜態字段)類變量是使用static修飾符聲明的任何字段; 這告訴編譯器,無論該類被實例化了多少次,該變量確實存在一個副本。 可以將定義特定類型自行車的齒輪數的字段標記為靜態,因為從概念上講,相同數量的齒輪將應用於所有情況。 代碼靜態int numGears = 6; 將創建這樣一個靜態字段。 另外,可以添加關鍵字final以指示檔位數量永遠不會改變。
您應該在聲明時或在靜態初始化程序塊中初始化靜態變量。 而不是在構造函數中。
僅當實例化List
類時,將在沒有任何地方使用構造函數。 將類加載到備注中時,將執行static initializer
塊。 因此,在加載類時,您的INode
將被初始化。
public static INode head;
static {
head = new INode(8);
}
要不就: -
public static INode head = new INode(8);
static
變量是所有實例共有的。 如果對一個實例進行了更改,則更改將反映在所有實例中。 因此,在使用它們之前,請確保您確實想要這樣做。 如果可能,請將您的INode
聲明non-static
變量。 並在使用它之前實例化List class
。
public INode head = new INode(8);
然后在您的主要方法中,您可以像這樣訪問它:-
List list = new List();
System.out.println(list.head.getValue());
您必須在main方法中初始化List對象
public static void main (String[] args)
{
new List();
INode a = new INode(8);
int data = a.getValue();
System.out.println(data);
System.out.println(head.getValue());
}
您可以以任何一種方式執行此操作..您可以創建類列表的實例,也可以初始化head以指向INode的對象。 這取決於您需要的業務邏輯
public static void main(String[] args) {
INode a = new INode(8);
int data = a.getValue();
System.out.println(data);
List list = new List();
System.out.println(list.head.getValue());
head = new INode(6);
System.out.println(head.getValue());
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.