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类的
的构造方法,这样是对的吗?还有,这样做有什么意义吗?

此处附上源代码:
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;

}

所有类的父类为Object 当你实例化的时候,在调用该类的构造方法的时候,首先都会调用父类的构造方法。
super。调用父类的构造方法。也可以调用父类的方法

如果不写构造方法,默认会提供一个无参构造方法,手写重载构造方法,必须第一行调用super,对于这个语法现象的理解我觉得 是在做类的内存初始化时(比如new)需要先初始他父类的方法和属性

不写也是可以的,你可以试一试去掉super,

写不写无所谓,子类构造方法第一行默认是调用父类的无参构造方法的,这里只是显式的将其写出来了。但是调用父类有参构造器时,必须显式的写出super(xxx)。
此处super()无意义,只是将省略的东西,写出来而已。

:这是这个类的有参构造函数,调用父类的构造函数,而且这是java语法规定,父类定义了构造函数,那么子类的构造函数的第一句代码必须是super()的。