This question already has an answer here: In Java, difference between package private, public, protected, and private 26 answers The first answer is basically correct - protected members can be accessed by classes from the same package subclasses of the declaring class from other packages However, there is a little trick: 6.6.2 Details on protected Access A protected member or const
这个问题在这里已经有了答案: 在Java中,封装私有,公共,受保护和私有26个答案之间的区别 第一个答案基本上是正确的 - protected成员可以被访问 来自同一个包的类 来自其他包的声明类的子类 但是,有一个小技巧: 6.6.2受保护访问的详细信息 对象的受保护成员或构造函数可以从仅在负责实现该对象的代码声明的包中进行访问。 这意味着来自其他包的子类不能访问其超类的任意实例的protected成员,它们只能在它们
I know that we cannot override static methods in Java, but can someone explain the following code? class A { public static void a() { System.out.println("A.a()"); } } class B extends A { public static void a() { System.out.println("B.a()"); } } How was I able to override method a() in class B ? You didn't override anything here. To see for yourself, T
我知道我们不能在Java中重写静态方法,但有人可以解释下面的代码吗? class A { public static void a() { System.out.println("A.a()"); } } class B extends A { public static void a() { System.out.println("B.a()"); } } 我如何能够覆盖B类中的方法a() ? 你没有在这里覆盖任何东西。 要亲自看看,请尝试在类B和Java中的public static void a()之前放置@Override注释,并引发错
If I have a java class which is package-private (declared with "class", not "public class"), there is really no difference if the methods inside are declared public or protected or package-private, right? So which should I use, or when should I use which? I'm a bit confused. If I have a java class which is package-private (declared with "class", not "pub
如果我有一个java类,它是package-private(用“class”声明,而不是“public class”),如果里面的方法声明为public或protected或者package-private,那么真的没有区别吗? 那么我应该使用哪一个,或者什么时候应该使用哪个? 我有点困惑。 如果我有一个java类,它是package-private(用“class”声明,而不是“public class”),如果里面的方法声明为public或protected或者package-private,那么真的没有区别吗? 好吧,也许不
Slightly related to my other question: What is the difference between the following: private class Joe protected class Joe public class Joe class Joe Once again, the difference between the last 2 is what I'm most interested in. A public class is accessible to a class in any package. A class with default access ( class Joe ) is only visible to other classes in the same package. The pri
与我的另一个问题略有关系:以下几点有什么区别: private class Joe protected class Joe public class Joe class Joe 最后两点之间的差异再次是我最感兴趣的。 任何包装内的课程都可以访问公共课程。 具有默认访问权限的class Joe ( class Joe )只对同一个包中的其他类可见。 私有和受保护的修饰符只能应用于内部类。 私有类只对其封闭类和其他内部类在同一个封闭类中可见。 受保护的类对同一包中的其他类以及扩
I am a little confused over the term "package private" that some of the documentation uses, along with the usage of "default access." Aren't package private and default access both synonymous with protected? Yes, it's almost the same. The protected modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addit
我对某些文档使用的术语“软件包专用”以及“默认访问”的用法有点困惑。 不是包私有和默认访问都是受保护的同义词吗? 是的,它几乎是一样的。 受保护的修饰符指定该成员只能在其自己的包内访问(与包私有一样), 另外 ,还可以在另一个包中访问该类的子类。 “默认”访问修饰符(它们都没有明确给出)是“包私有”,这意味着只有同一包中的东西才能访问它们。 然而,在同一个软件包中,对类之间的继承关系没有任何意义 - 它纯
This question already has an answer here: difference between abstraction and encapsulation? 38 answers Encapsulation is probably the most misunderstood concept of OOP. Encapsulation is NOT data hiding! "Encapsulation" comes from "capsule". It means putting things together, closing them in a package, and the "things" we are talking about here are data and fu
这个问题在这里已经有了答案: 抽象和封装之间的区别? 38个答案 封装可能是OOP最容易被误解的概念。 封装不是数据隐藏! “封装”来自“胶囊”。 它意味着把所有东西放在一起,把它们封装在一个包中,我们在这里讨论的“事物”是数据和功能。 没有封装的编程意味着处理数据的函数“浮动”在代码的某个地方,虽然它们处理数据,甚至将特定类型作为输入,但它们与数据分离。 让我举一个例子,而不关注“公共”等:如果你有一个
I've got a nested loop construct like this: for (Type type : types) { for (Type t : types2) { if (some condition) { // Do something and break... break; // Breaks out of the inner loop } } } Now how can I break out of both loops. I've looked at similar questions, but none concerns Java specifically. I couldn't apply these solution
我有这样的嵌套循环结构: for (Type type : types) { for (Type t : types2) { if (some condition) { // Do something and break... break; // Breaks out of the inner loop } } } 现在我怎么能摆脱两个循环。 我看过类似的问题,但没有一个关于Java。 由于大多数使用gotos,我无法应用这些解决方案。 我不想将内部循环置于不同的方法中。 更新:当我完成循环块
I'm looking for a class in java that has key-value association, but without using hashes. Here is what I'm currently doing: Add values to a Hashtable . Get an iterator for the Hashtable.entrySet() . Iterate through all values and: Get a Map.Entry for the iterator. Create an object of type Module (a custom class) based on the value. Add the class to a JPanel. Show the panel.
我正在寻找一个具有键值关联的java类,但不使用散列。 这是我目前正在做的事情: 将值添加到Hashtable 。 获取Hashtable.entrySet()的迭代器。 遍历所有值和: 获取迭代器的Map.Entry 。 根据值创建一个Module (一个自定义类)类型的对象。 将该类添加到JPanel。 显示面板。 问题在于我无法控制返回值的顺序,因此无法按给定顺序显示值(不对订单进行硬编码)。 我会为此使用ArrayList或Vector ,但后来在代
How would you initialise a static Map in Java? Method one: static initialiser Method two: instance initialiser (anonymous subclass) or some other method? What are the pros and cons of each? Here is an example illustrating two methods: import java.util.HashMap; import java.util.Map; public class Test { private static final Map<Integer, String> myMap = new HashMap<Integer, St
你将如何初始化Java中的静态地图? 方法一:静态初始化器 方法二:实例初始化(匿名子类)或其他方法? 每个的优缺点是什么? 以下是一个说明两种方法的示例: import java.util.HashMap; import java.util.Map; public class Test { private static final Map<Integer, String> myMap = new HashMap<Integer, String>(); static { myMap.put(1, "one"); myMap.put(2, "two");
This question already has an answer here: What is a serialVersionUID and why should I use it? 21 answers Why serialVersionUID must be declared as static, final and of type long? Because the specification says so. Does serialVersionUID need to be unique? No. It only differentiates between different versions of your class (eg compiled at different points in time). If you're writing
这个问题在这里已经有了答案: 什么是serialVersionUID,为什么我应该使用它? 21个答案 为什么serialVersionUID必须声明为static,final和long类型? 因为规范是这样说的。 serialVersionUID是否必须是唯一的? 不可以。它只区分你班级的不同版本(例如,在不同的时间点编译)。 如果你正在编写一个新的类,你可以设置serialVersionUID = 1L; 无任何问题。 当两个或更多类包含相同的serialVersionUID时会发生什