访问Java中的私有静态嵌套类中的修饰符 [英] Access modifiers inside a private static nested class in Java

查看:184
本文介绍了访问Java中的私有静态嵌套类中的修饰符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Java中有一个private static嵌套类。访问修饰符在这个类中的字段和方法的意义是什么?我已尝试公开和私人,对我的申请没有影响。

  public class MyList< T> {
private static class Node {//列表节点
private Object item;
private Node next;
private node prev;

private Node(Node next){
this.next = next;
}

私人静态节点doStuff(){}
}
}

$两种嵌套类:1.静态(嵌套类)和2.非静态(也称为内部类)

$

b
$ b

现在,Outer类 MyList 可以访问内部类的所有成员 Node ,但是当你想要某些外部类访问它的限制时,你实际上使用类 Node (嵌套类)的成员的访问说明符。



有趣的是: Source1 Source2


I have a "private static" nested class in Java. What is the significance of access modifiers for fields and methods inside this class? I've tried both public and private with no effect on my application.

public class MyList<T>{
    private static class Node{ // List node
        private Object item;
        private Node next;
        private Node prev;

        private Node(Node next){
            this.next = next;
        }

        private static Node doStuff(){}
    }
}

解决方案

Two kinds of nested classes: 1. Static (nested class) and 2. Non-static (also called inner class)

Now, the Outer class, MyList can access all the members of the inner class Node, but you actually use the access specifiers for the members of the class Node (nested class) when you want restrictions of some external class accessing it.

Interesting reads: Source1, Source2

这篇关于访问Java中的私有静态嵌套类中的修饰符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆