这句话是什么意思并且......?

发布于 2024-11-27 07:57:08 字数 498 浏览 0 评论 0原文

最近我看到这样一句话:

InputStream in = new FileInputStream(Filename);

这句话是什么意思?

这里的in指的是什么?

inFileInputStream 的对象吗?

编写语句: 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

递刀给你 2024-12-04 07:57:08

该声明利用了继承的效果。您正在创建一个 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 the in 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 class InputStream. After this line, you can invoke any methods declared in the InputStream class on this object. Even though the object is really a FileInputStream, 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.

情未る 2024-12-04 07:57:08

是的 inInputStream 类型的对象的名称(这是一个抽象类) 可能有许多类扩展该抽象类(包括 FileInputStream code>) - 每个都实现 InputStream 所需的组件。

您可以创建您正在使用的类的确切类型的特定对象,但是通过使用抽象版本或接口 - 您可以保证该类具有一组特定的函数,但您可以轻松地切换实际实现而无需更改代码。 (继承在行动!)

例如,上面的行将来可以改为使用:

InputStream in = new SocketInputStream();

有关更多信息,请阅读 InputStream 背景。

Yes in is the name of an object of type InputStream (which is an abstract class) There could be many classes that extend that abstract class (including FileInputStream) - each of which implement the required components of InputStream.

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:

InputStream in = new SocketInputStream();

For more information read the InputStream background.

同展鸳鸯锦 2024-12-04 07:57:08

in 是存储 InputStream 实例时变量的名称。

in is the name of the variable whereas you're storing an instance of InputStream.

走野 2024-12-04 07:57:08

您基本上是打开一个文件进行读取。

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.

烈酒灼喉 2024-12-04 07:57:08

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.

清风夜微凉 2024-12-04 07:57:08
  1. 标识符“in”指的是一个InputStream对象。在本例中,它确实引用构造函数创建的 FileInputStream 对象。这是可能的,因为...

  2. InputStream 是一个抽象类。在 Java 中,抽象类可以定义方法(和构造函数),但在扩展之前无法实例化。扩展抽象类的子类必须调用抽象类的构造函数。

FileInputStream 扩展了InputStream。

  1. 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...

  2. 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.

临走之时 2024-12-04 07:57:08

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....

聚集的泪 2024-12-04 07:57:08

以下是关于为什么抽象类具有可能相关的构造函数的现有解释:

为什么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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文