在java中,有三种访问级别:
那么为什么java编译器允许这种情况发生呢?
TestBlah.java:
public class TestBlah {
public static void main(String[] args) {
Blah a = new Blah("Blah");
Bloo b = new Bloo("Bloo");
System.out.println(a.getMessage());
System.out.println(b.getMessage()); //Works
System.out.println(a.testing);
System.out.println(b.testing); //Works
}
}
Run Code Online (Sandbox Code Playgroud)
Blah.java:
public class Blah {
protected String message;
public Blah(String msg) {
this.message = msg;
}
protected String getMessage(){
return(this.message);
}
}
Run Code Online (Sandbox Code Playgroud)
Bloo.java:
public class Bloo extends Blah {
public Bloo(String testing) {
super(testing);
}
}
Run Code Online (Sandbox Code Playgroud) 我不应该能够调用实例化对象的私有方法.我想知道为什么下面的代码有效.
public class SimpleApp2 {
/**
* @param args
*/
private int var1;
public static void main(String[] args) {
SimpleApp2 s = new SimpleApp2();
s.method1(); // interesting?!
}
private void method1() {
System.out.println("this is method1");
this.method2(); // this is ok
SimpleApp2 s2 = new SimpleApp2();
s2.method2(); // interesting?!
System.out.println(s2.var1); // interesting?!
}
private void method2() {
this.var1 = 10;
System.out.println("this is method2");
}
}
Run Code Online (Sandbox Code Playgroud)
我知道可以从类中访问私有方法.但是,如果类中的方法实例化同一个类的对象,那么范围规则是否应该应用于该实例化对象?
可以像main这样的静态方法访问类的非静态成员,如本例所示?
Collection接口的最小完整定义仅包含两个方法:iterator()和size(),它们是抽象的AbstractCollection.
为什么所有其他方法都没有在Java 8中默认?兼容性应该不是问题,例如,Iterator.remove()在Java 7之前是抽象的,但是从Java 8开始就是默认的.
AbstractCollection当我希望Collection实现成为另一个类层次结构的成员时,子类化有时会很不方便.这不是Java中实际需要接口中的默认方法的原因之一吗?
大约同样的问题Map,List以及其他基本的接口,包括Java集合框架.
UPDATE
Paul Sandoz:
通常,如果有一个令人信服的理由来帮助实现,我们只将接口上的现有抽象方法转换为非抽象方法,例如Iterator.remove.
这些不是Collection上的新方法,并且AbstractCollection中已经有实现.将这些抽象转换为非抽象方法的优势并不是特别引人注目,因为它最有可能从AbstractCollection继承或提供更有效的实现.
可以将AbstractCollection上的所有非抽象方法移动到Collection.如果我们从一张白纸开始,这是我们可能做的.(注意,对于AbstractList上的所有非抽象方法,都不能这样做.)
http://mail.openjdk.java.net/pipermail/core-libs-dev/2014-February/025118.html
我上课A。此类包含两个方法,比如say method1()和method2()。现在,我还有另外两个名为B和的类C。两者都将包含相同的Aclass 实例。
现在,我想以“ B class can only callmethod1()and my otherC”类可以调用的方式来限制访问method2()。设计方案如下
class A {
void method1(){/*-------------*/}
void method2(){/*-------------*/}
}
Run Code Online (Sandbox Code Playgroud)
现在,如果我创建的实例A并与我的B和C类共享
class B {
A ob;
public B(A ob) {
this.ob=ob;
}
public void process() {
ob.method1(); //only call method1()
ob.method2(); //can't access it.
}
}
class C {
A ob;
public C(A ob) {
this.ob=ob;
}
public void …Run Code Online (Sandbox Code Playgroud) 为什么一个包中的子类不能通过超类的引用访问它的超类(在另一个包中)的受保护成员?我正在努力解决这一问题.请帮我
package points;
public class Point {
protected int x, y;
}
package threePoint;
import points.Point;
public class Point3d extends Point {
protected int z;
public void delta(Point p) {
p.x += this.x; // compile-time error: cannot access p.x
p.y += this.y; // compile-time error: cannot access p.y
}
Run Code Online (Sandbox Code Playgroud) 有人可以帮助我使用下面的代码构建没有任何错误,但当我运行它我得到"错误:在类程序中找不到主要方法,请将主方法定义为:public static void main(String [] args)" ...
任何想法为什么?
import java.util.*;
public class Program
{
// Create an array/database for 100 prizes We can increase this number
private Prize[] prizeDatabase = new Prize[100];
// variable to store the next available position in the database
private int currentArrayPosition = 0;
/**
This method displays the menu to the user
*/
private void DisplayMenu()
{
System.out.println("Please select an option from the following:");
System.out.println("1. Enter details of a prize");
System.out.println("2. Print the details stored …Run Code Online (Sandbox Code Playgroud) 我一直在深入研究Java中的String类.Java中的字符串由字符数组支持.
要创建具有初始值的字符串,我们将构造函数称为:
/* String creation */
String s = new String("example");
Run Code Online (Sandbox Code Playgroud)
String类中的构造函数代码是:
public String(String original) {
this.value = original.value;
}
Run Code Online (Sandbox Code Playgroud)
有人可以解释一下"original.value"的逻辑.从源代码中,我理解它返回字符数组.但java如何生成它?
与我的另一个问题略有关联:以下内容有何区别:
private class Joe
protected class Joe
public class Joe
class Joe
Run Code Online (Sandbox Code Playgroud)
再一次,最后两个之间的差异是我最感兴趣的.
我有这段代码
public class Base {
private int x=10;
void show(){
System.out.println(x);
}
}
public class Child extends Base {
public static void main(String[] args) {
Child c1=new Child();
c1.show();
}
}
Run Code Online (Sandbox Code Playgroud)
这段代码工作正常,输出为10.任何人都可以详细说明这个私有数据成员如何在子类中访问..
class SomeClass1 {
void method1() { }
public void method2() { }
private void method3() { }
protected void method4() { }
}
class DemoClass{
public static void main(String[] parameters) {
SomeClass1 sc = new SomeClass1();
sc.method1();
sc.method2();
sc.method4();
}
}
Run Code Online (Sandbox Code Playgroud)
受保护的方法只能由继承超类的类访问.正如我们在这里看到的,DemoClass不会扩展SomeClass.但是,它能够访问受保护的方法.这怎么可能?
java ×10
class ×1
collections ×1
core ×1
inheritance ×1
java-8 ×1
protected ×1
subclass ×1
visibility ×1