草庐IT

without_protection

全部标签

java - com.w3c.dom.Document without <?xml version ="1.0"encoding ="UTF-8"standalone ="no"?>

我正在创建一个com.w3c.dom.Document来自String使用此代码:DocumentBuilderFactorydocFactory=DocumentBuilderFactory.newInstance();DocumentBuilderdocBuilder=docFactory.newDocumentBuilder();Documentdoc=docBuilder.parse(newInputSource(newStringReader("")));当我System.out.println(xmlToString(document)),我明白了:一切正常,但我不希望XM

java - Java 最终类中 protected 方法的用例是什么?

考虑来自官方OpenJDKsource的这段代码java.awt.font.TextLayout的:publicfinalclassTextLayout{/*...*/protectedvoidhandleJustify(floatjustificationWidth){//nevercalled}}这里的用例是什么?为什么通常编写这样的代码可能有意义? 最佳答案 protected成员仍然可以通过同一包中的代码访问。我的猜测是这个类在一些早期的(可能甚至不是公共(public)的)版本中曾经是非最终的,然后变成了最终的,并且pro

Java servlet 和 IO : Create a file without saving to disk and sending it to the user

我希望可以帮助我解决文件创建/响应问题。我知道如何创建和保存文件。我知道如何通过ServletOutputStream将该文件发送回用户。但我需要的是创建一个文件,而不是将其保存在磁盘上,然后通过ServletOutputStream发送该文件。上面的代码解释了我拥有的部分。任何帮助表示赞赏。提前致谢。//ThisCreatesafile//Stringtext="Thesedaysrunawaylikehorsesoverthehill";Filefile=newFile("MyFile.txt");Writerwriter=newBufferedWriter(newFileWrit

java - 使用 protected 构造函数匿名初始化类

假设我们有一个类:publicclassSomeClass{protectedSomeClass(){}}在位于不同包中的MainClass中,我尝试执行两行:publicstaticvoidmain(String[]args){SomeClasssac1=newSomeClass();SomeClasssac2=newSomeClass(){};}因为protected构造函数,在这两种情况下我都认为程序会失败。令我惊讶的是,匿名初始化工作正常。有人能解释一下为什么第二种初始化方法可以吗? 最佳答案 你的匿名类SomeClasss

java - Java中 protected 类结构?

我想知道Java中是否有一种语言特性,其中父类(superclass)的方法对于子类的成员是不可见的:publicclassSubclassextendsprotectedSuperclass什么的。我举个例子。这是你的父类(superclass)。publicclassA{publicStringgetA(){...}publicStringgetB(){...}publicStringgetC(){...}publicvoidsetA(Stringa){...}publicvoidsetB(Stringb){...}publicvoidsetC(Stringc){...}}如果您想

java - 使用基类实例在派生类中访问的 protected 成员

我在派生类中创建了基类的实例并试图访问protected成员。我可以直接访问派生类中的protected成员,而无需实例化基类。基类:packagecom.core;publicclassMyCollection{protectedIntegerintg;}同一包中的派生类-packagecom.core;publicclassMyCollection3extendsMyCollection{publicvoidtest(){MyCollectionmc=newMyCollection();mc.intg=1;//Works}}不同包中的派生类-packagesecondary;imp

java - 为什么 Java 构造函数必须是 public 或 protected 才能将类扩展到其包之​​外?

以下是我的ProtectedConstructor.java源码:packageprotectCon;publicclassProtectedConstructor{publicintnothing;ProtectedConstructor(){nothing=0;}}下面是UsingProtectedCon.java来源:packageother;importprotectcon.ProtectedConstructor;publicclassUsingProtectedConextendsProtectedConstructor{//**Line4**publicstaticvoi

java - 为什么 protected 方法不被Spring AOP拦截

我熟悉SpringAOP。正如我在Spring文档中读到的那样http://docs.spring.io/spring/docs/3.1.x/spring-framework-reference/html/aop.html,SpringAOP致力于代理的概念。在8.2.3.1SupportedPointcutDesignators部分,我发现了下面的注释Duetotheproxy-basednatureofSpring'sAOPframework,protectedmethodsarebydefinitionnotintercepted,neitherforJDKproxies(whe

java - 如何使用 protected 方法为 Java 类实现装饰器模式

包外的子类不能访问父类实例上的protected成员(只能访问子类本身或其子类的实例)。JLS链接:http://java.sun.com/docs/books/jls/third_edition/html/names.html#6.6.2这是一个例子。现有类如下所示:packagepackage1;publicabstractclassBaseImplementation{publicStringgetResource1(){returnprocessTemplate1(getBaseUrl());}publicStringgetResource2(){returnprocessTe

java - 为什么在Java中访问 protected 成员是这样实现的?

关于在Java中访问protected成员的问题已经被问过并回答过很多次,例如:Java:protectedaccessacrosspackages但是我不明白为什么要这样实现,参见《JavaProgrammingLanguage》(第4版)的解释:“限制背后的原因是:每个子类继承父类(superclass)的契约并以某种方式扩展该契约。假设一个子类作为其扩展契约的一部分,对父类(superclass)的protected成员的值施加约束父类(superclass)。如果不同的子类可以访问第一个子类对象的protected成员,那么它可以以破坏第一个子类契约的方式操纵它们,这是不允许的