death05 2016-07-15 07:35 采纳率: 16.7%
浏览 6210
已结题

Java中一个只实现了接口的类,在它的构造方法中调用了super()方法,这样做有什么意义呢

今天在看spring源码spring-core包中的EncodedResource.java时发现它的其中一个构造方法里出现了:
private EncodedResource(Resource resource, String encoding, Charset charset) {
super();
Assert.notNull(resource, "Resource must not be null");
this.resource = resource;
this.encoding = encoding;
this.charset = charset;
}
一开始并不知道super()到底做了什么,后来经别人提醒知道了原来这是在调用Object类的
的构造方法,这样是对的吗?还有,这样做有什么意义吗?

  • 写回答

6条回答 默认 最新

  • death05 2016-07-15 07:36
    关注

    此处附上源代码:
    EncodedResource.java
    /*

    • Copyright 2002-2016 the original author or authors. *
    • Licensed under the Apache License, Version 2.0 (the "License");
    • you may not use this file except in compliance with the License.
    • You may obtain a copy of the License at *
    • http://www.apache.org/licenses/LICENSE-2.0 *
    • Unless required by applicable law or agreed to in writing, software
    • distributed under the License is distributed on an "AS IS" BASIS,
    • WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    • See the License for the specific language governing permissions and
    • limitations under the License. */

    package org.springframework.core.io.support;

    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.Reader;
    import java.nio.charset.Charset;

    import org.springframework.core.io.InputStreamSource;
    import org.springframework.core.io.Resource;
    import org.springframework.util.Assert;
    import org.springframework.util.ObjectUtils;

    /**

    • Holder that combines a {@link Resource} descriptor with a specific encoding
    • or {@code Charset} to be used for reading from the resource. *
    • Used as an argument for operations that support reading content with

    • a specific encoding, typically via a {@code java.io.Reader}. *
    • @author Juergen Hoeller
    • @author Sam Brannen
    • @since 1.2.6
    • @see java.io.Reader
    • @see java.nio.charset.Charset
      */
      public class EncodedResource implements InputStreamSource {

      private final Resource resource;

      private final String encoding;

      private final Charset charset;

      /**

      • Create a new {@code EncodedResource} for the given {@code Resource},
      • not specifying an explicit encoding or {@code Charset}.
      • @param resource the {@code Resource} to hold (never {@code null}) */ public EncodedResource(Resource resource) { this(resource, null, null); }

      /**

      • Create a new {@code EncodedResource} for the given {@code Resource},
      • using the specified {@code encoding}.
      • @param resource the {@code Resource} to hold (never {@code null})
      • @param encoding the encoding to use for reading from the resource */ public EncodedResource(Resource resource, String encoding) { this(resource, encoding, null); }

      /**

      • Create a new {@code EncodedResource} for the given {@code Resource},
      • using the specified {@code Charset}.
      • @param resource the {@code Resource} to hold (never {@code null})
      • @param charset the {@code Charset} to use for reading from the resource */ public EncodedResource(Resource resource, Charset charset) { this(resource, null, charset); }

      private EncodedResource(Resource resource, String encoding, Charset charset) {
      super();
      Assert.notNull(resource, "Resource must not be null");
      this.resource = resource;
      this.encoding = encoding;
      this.charset = charset;
      }

      /**

      • Return the {@code Resource} held by this {@code EncodedResource}. */ public final Resource getResource() { return this.resource; }

      /**

      • Return the encoding to use for reading from the {@linkplain #getResource() resource},
      • or {@code null} if none specified. */ public final String getEncoding() { return this.encoding; }

      /**

      • Return the {@code Charset} to use for reading from the {@linkplain #getResource() resource},
      • or {@code null} if none specified. */ public final Charset getCharset() { return this.charset; }

      /**

      • Determine whether a {@link Reader} is required as opposed to an {@link InputStream},
      • i.e. whether an {@linkplain #getEncoding() encoding} or a {@link #getCharset() Charset}
      • has been specified.
      • @see #getReader()
      • @see #getInputStream() */ public boolean requiresReader() { return (this.encoding != null || this.charset != null); }

      /**

      • Open a {@code java.io.Reader} for the specified resource, using the specified
      • {@link #getCharset() Charset} or {@linkplain #getEncoding() encoding}
      • (if any).
      • @throws IOException if opening the Reader failed
      • @see #requiresReader()
      • @see #getInputStream() */ public Reader getReader() throws IOException { if (this.charset != null) { return new InputStreamReader(this.resource.getInputStream(), this.charset); } else if (this.encoding != null) { return new InputStreamReader(this.resource.getInputStream(), this.encoding); } else { return new InputStreamReader(this.resource.getInputStream()); } }

      /**

      • Open a {@code java.io.InputStream} for the specified resource, ignoring any
      • specified {@link #getCharset() Charset} or {@linkplain #getEncoding() encoding}.
      • @throws IOException if opening the InputStream failed
      • @see #requiresReader()
      • @see #getReader() */ @Override public InputStream getInputStream() throws IOException { return this.resource.getInputStream(); }

      @Override
      public boolean equals(Object other) {
      if (this == other) {
      return true;
      }
      if (!(other instanceof EncodedResource)) {
      return false;
      }
      EncodedResource otherResource = (EncodedResource) other;
      return (this.resource.equals(otherResource.resource) &&
      ObjectUtils.nullSafeEquals(this.charset, otherResource.charset) &&
      ObjectUtils.nullSafeEquals(this.encoding, otherResource.encoding));
      }

      @Override
      public int hashCode() {
      return this.resource.hashCode();
      }

      @Override
      public String toString() {
      return this.resource.toString();
      }

    }

    InputStreamSource.java
    /*

    • Copyright 2002-2012 the original author or authors. *
    • Licensed under the Apache License, Version 2.0 (the "License");
    • you may not use this file except in compliance with the License.
    • You may obtain a copy of the License at *
    • http://www.apache.org/licenses/LICENSE-2.0 *
    • Unless required by applicable law or agreed to in writing, software
    • distributed under the License is distributed on an "AS IS" BASIS,
    • WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    • See the License for the specific language governing permissions and
    • limitations under the License. */

    package org.springframework.core.io;

    import java.io.IOException;
    import java.io.InputStream;

    /**

    • Simple interface for objects that are sources for an {@link InputStream}. *
    • This is the base interface for Spring's more extensive {@link Resource} interface. *

    • For single-use streams, {@link InputStreamResource} can be used for any

    • given {@code InputStream}. Spring's {@link ByteArrayResource} or any
    • file-based {@code Resource} implementation can be used as a concrete
    • instance, allowing one to read the underlying content stream multiple times.
    • This makes this interface useful as an abstract content source for mail
    • attachments, for example. *
    • @author Juergen Hoeller
    • @since 20.01.2004
    • @see java.io.InputStream
    • @see Resource
    • @see InputStreamResource
    • @see ByteArrayResource
      */
      public interface InputStreamSource {

      /**

      • Return an {@link InputStream}.
      • It is expected that each call creates a fresh stream.

      • This requirement is particularly important when you consider an API such

      • as JavaMail, which needs to be able to read the stream multiple times when
      • creating mail attachments. For such a use case, it is required
      • that each {@code getInputStream()} call returns a fresh stream.
      • @return the input stream for the underlying resource (must not be {@code null})
      • @throws IOException if the stream could not be opened
      • @see org.springframework.mail.javamail.MimeMessageHelper#addAttachment(String, InputStreamSource) */ InputStream getInputStream() throws IOException;

    }

    评论

报告相同问题?

悬赏问题

  • ¥15 一道python难题
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度