gpt4 book ai didi

java - 为什么我的 "private"关键字在 var "first"上不起作用?

转载 作者:行者123 更新时间:2023-12-02 09:21:19 26 4
gpt4 key购买 nike

这些天我正在使用 CS61b。我被访问控制的讲座困住了。我的变量first和类IntNode上的“private”关键字无法正常工作。

在 Google 上搜索,但一无所获。

public class SLList {
private IntNode first;

/**
* If the nested class never uses any instance variables or methods of the outer
* class, declare it static.
*/
private static class IntNode {
public IntNode next;
public int item;

public IntNode(int i, IntNode n) {
next = n;
item = i;
}

}

public SLList(int x) {
first = new IntNode(x, null);
}



public void addFirst(int x) {
first = new IntNode(x, first);
}

public int getFirst() {
return first.item;
}
/** ----------------SIZE---------------------- */
private int size(IntNode L) {
if (L.next == null) {
return 1;
}

return 1 + size(L.next);
}

public int size() {
return size(first);
}
/**-------------------SIZE------------------- */


/**---------------add LAST ------------------*/
/** how to solve null pointer expectation? */
public void addLast(int x) {
IntNode p=first;
while(p.next!=null){
p=p.next;
}
p.next=new IntNode(x, null);
}
/**---------------add LAST ------------------*/

public static void main(String[] args) {
SLList L = new SLList(5);
L.addFirst(10);
L.addFirst(15);
System.out.println(L.getFirst());
System.out.println(L.size());
L.addLast(20);
L.first.next.next = L.first.next; /** <----- I can still get√ access to first. */

}
}

我预计会出现错误:首先在 SLList 中有私有(private)类,但我没有错。

最佳答案

参见Java Language Specification §6.6.1 :

A member (class, interface, field, or method) of a reference type, or a constructor of a class type, is accessible only if the type is accessible and the member or constructor is declared to permit access:

  • If the member or constructor is declared public, then access is permitted.

  • All members of interfaces lacking access modifiers are implicitly public.

  • Otherwise, if the member or constructor is declared protected, then access is permitted only when one of the following is true:

    • Access to the member or constructor occurs from within the package containing the class in which the protected member or constructor is declared.

    • Access is correct as described in §6.6.2.

  • Otherwise, if the member or constructor is declared with package access, then access is permitted only when the access occurs from within the package in which the type is declared.

    A class member or constructor declared without an access modifier implicitly has package access.

  • Otherwise, the member or constructor is declared private, and access is permitted if and only if it occurs within the body of the top level type (§7.6) that encloses the declaration of the member or constructor.

(强调我的)

由于您对 first 的访问位于同一顶级类型内,因此您可以毫无问题、错误或任何其他情况地访问它。

关于java - 为什么我的 "private"关键字在 var "first"上不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58676685/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com