这句话是什么意思并且......?
最近我看到这样一句话:
InputStream in = new FileInputStream(Filename);
这句话是什么意思?
这里的in
指的是什么?
in
是 FileInputStream
的对象吗?
编写语句: InputStream is = new InputStream();
会产生错误,因为 InputStream 是一个抽象类,但为什么我们有这个类的构造函数?--->InputStream()
Recently i have come across this statement :
InputStream in = new FileInputStream(Filename);
What does this statement mean ?
In this what does in
refer to.?
Is in
the object of FileInputStream
?
writing the statement : InputStream is = new InputStream();
produces an error b'coz InputStream is an abstract class but then why we have a constructor for this class ?--->InputStream()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
该声明利用了继承的效果。您正在创建一个
FileInputStream
对象的新实例,并将其分配给in
变量。就该变量的用户而言,他们只能看到
InputStream
类型的对象 - 它可以是抽象类InputStream
的任何子类。在此行之后,您可以调用此对象的InputStream
类中声明的任何方法。即使该对象实际上是一个 FileInputStream,您也看不到这一点,因此无法调用这些方法(无需转换)。构造函数的存在使得子类可以调用它来实例化和设置所有输入流所需的任何实例方法。您不能调用它,但子类可以调用它。
This statement is leveraging the effects of inheritance. You are creating a new instance of a
FileInputStream
object and assigning it to thein
variable.As far as users of this variable go, they only see an object of the type
InputStream
- it could be any subclass of the abstract classInputStream
. After this line, you can invoke any methods declared in theInputStream
class on this object. Even though the object is really aFileInputStream
, you can't see this, and therefore can't invoke those methods (without casting).The constructor exists so that subclasses can call it to instantiate and set up any instance methods that all input streams need. You can't call it, but subclasses can invoke it.
是的
in
是InputStream
类型的对象的名称(这是一个抽象类) 可能有许多类扩展该抽象类(包括FileInputStream
code>) - 每个都实现InputStream
所需的组件。您可以创建您正在使用的类的确切类型的特定对象,但是通过使用抽象版本或接口 - 您可以保证该类具有一组特定的函数,但您可以轻松地切换实际实现而无需更改代码。 (继承在行动!)
例如,上面的行将来可以改为使用:
有关更多信息,请阅读 InputStream 背景。
Yes
in
is the name of an object of typeInputStream
(which is an abstract class) There could be many classes that extend that abstract class (includingFileInputStream
) - each of which implement the required components ofInputStream
.You could create a specific object of the exact type of class you're using, but by using the abstract version or an interface - you're guarenteed the class has a certain set of functions but you can easily switch actual implementation without changing code. (Inheritance in action!)
For example the above line in the future could instead use:
For more information read the InputStream background.
in
是存储InputStream
实例时变量的名称。in
is the name of the variable whereas you're storing an instance ofInputStream
.您基本上是打开一个文件进行读取。
in
是您分配给输入流的变量。它属于InputStream 类。You are basically opening a file for reading.
in
is the variable that you are assigning to the input stream. It is of class InputStream.in
是此处选择的变量名,它包含一个类为 FileInputStream 的对象。抽象类InputStream定义构造函数的原因是为了强制(非抽象)子类实现它。in
is the variablename chosen here and it holds an object who's class is FileInputStream. The reason why the abstract class InputStream defines a constructor is so that it forces (non-abstract) subclasses to implement it.标识符“in”指的是一个InputStream对象。在本例中,它确实引用构造函数创建的 FileInputStream 对象。这是可能的,因为...
InputStream 是一个抽象类。在 Java 中,抽象类可以定义方法(和构造函数),但在扩展之前无法实例化。扩展抽象类的子类必须调用抽象类的构造函数。
FileInputStream 扩展了InputStream。
The identifier "in" refers to an InputStream object. In this case, it does refer to the FileInputStream object that was created by the constructor. That is possible because...
InputStream is an abstract class. In Java, an abstract class can have defined methods (and constructors), however it can not be instantiated until it is extended. Subclasses that extend an abstract class must call the abstract class's constructor.
FileInputStream extends InputStream.
InputStream是一个抽象类,因此不能直接实例化。您只能分配它的子类的实例,在本例中是 FileInputStream。
下一个明显的问题是为什么不能将其分配给 FileInputStream?这个问题的答案是理解继承、抽象类和松散耦合的概念。简单来说,明天如果您想更改代码,而不是从文件中读取它,而是从 URL 中读取它...您可以通过将 FileInputStream 更改为 URLInputStream(你必须写这个)它也扩展了抽象InputStream类....
InputStream is an abstract class and so it cannot be directly instantiated. You can only assign an instance of a subclass of it which in this case a FileInputStream.
The next obvious question is why can't you assign it to a FileInputStream? Answer to that question would be to understand inheritance, abstract class and concepts of loose coupling. To be simple, tomorrow if you want to change your code such that instead of reading it from file, to read it say from an URL... You can do that without effecting rest of your code by just changing your FileInputStream to a URLInputStream(u have to write this one) which also extends the abstract InputStream class....
以下是关于为什么抽象类具有可能相关的构造函数的现有解释:
为什么Java中的抽象类有构造函数?
除了这个补充之外,我同意Rudu的答案,in是我们程序中引用类FileInputStream的实例的局部变量。事实上,在赋值中它仅绑定到 InputStream 类型的局部变量,这意味着在此范围内它仅被编译器视为此类型。如上所述,您只能调用接口/抽象类..InputStream 上存在的方法。
因此,虽然在运行时这将是 FileInputStream 类型的实例,但由于 java 静态类型系统以及继承和赋值的实现,它在编译时的范围内只能用作 InputStream。
Here is an existing explanation for why abstract classes have constructors which may be relevant:
Why do abstract classes in Java have constructors?
Other than this addition I agree with Rudu's answer, in, is a local variable in our program that references an instance of the class FileInputStream. The fact that in the assignment it is only bound to a local variable of type InputStream means that within this scope it is treated as this type only by the compiler. Meaning as mentioned, you can only call methods that exist on the interface/ abstract class .. InputStream.
So while at runtime this will be an instance of type FileInputStream, because of java static type system and implementation of inheritance and assignments, it is at compile time within scope only usable as an InputStream.