接口作为Java中的一种类型?

时间:2022-03-31 09:42:39

From The Java Tutorials:

从Java教程:

In Java, a class can inherit from only one class but it can implement more than one interface. Therefore, objects can have multiple types: the type of their own class and the types of all the interfaces that they implement. This means that if a variable is declared to be the type of an interface, its value can reference any object that is instantiated from any class that implements the interface.

在Java中,一个类只能从一个类继承,但它可以实现多个接口。因此,对象可以有多种类型:它们自己类的类型和它们实现的所有接口的类型。这意味着如果一个变量被声明为接口的类型,那么它的值可以引用任何实现接口的类实例化的对象。

Can anyone provide me a basic pseudo type for this. I did not understand the bold lines.

有人能给我一个基本的伪类型吗?我不理解粗体字。

11 个解决方案

#1


51  

Let's declare two interfaces and a class that implements them both:

让我们声明两个接口和一个实现它们的类:

interface I1 { }

interface I2 { }

class C implements I1, I2 { }

objects can have multiple types

对象可以有多种类型

In the following code, it can be seen that a C instance has the type of C as well as I1 and I2:

在下面的代码中,可以看到一个C实例具有C的类型以及I1和I2的类型:

C c = new C();

boolean isC = (c instanceof C);   //true
boolean isI1 = (c instanceof I1); //true
boolean isI2 = (c instanceof I2); //true

Now let's declare a class B which implements I1 as well:

现在我们声明一个实现I1的类B:

class B implements I1 { }

if a variable is declared to be the type of an interface, its value can reference any object that is instantiated from any class that implements the interface.

如果一个变量被声明为接口的类型,那么它的值可以引用任何实现接口的类实例化的对象。

If we declare a variable of type I1, we can set it to an instance of C, and then reassign it to an instance of B:

如果我们声明类型为I1的变量,我们可以将它设置为C的实例,然后将它重新分配给B的实例:

I1 i1 = new C();
i1 = new B();

We can also reassign it to an instance of D, where D extends C:

我们也可以将它重新分配给D的一个实例,其中D扩展了C:

i1 = new D();

...

class D extends C { }

#2


10  

Consider the following example:

考虑下面的例子:

Serializable s = new ArrayList();

In Java, this is valid code, even though Serializable is an interface, because ArrayList implements Serializable. So in this case, we're treating s as a variable of type Serializable.

在Java中,这是有效的代码,尽管Serializable是一个接口,因为ArrayList实现了Serializable。在本例中,我们将s作为可串行化的变量。

Now suppose we follow up the above code with the following:

现在假设我们按照上面的代码进行如下操作:

s = "String object";

This is also valid becauseString also implements Serializable. Since we declared s as type Serializable, it can point to any object that implements that interface.

这也是有效的,因为string也实现了serizable。由于我们将s声明为Serializable类型,所以它可以指向实现该接口的任何对象。

#3


5  

objects can have multiple types

对象可以有多种类型

Consider the following snippet:

考虑下面的代码片段:

public class MyClass extends ParentClass implements Interface1, Interface2 {    
    //some code    
}

This class can be used in different places as follows:

本课程可在以下不同地方使用:

MyClass m1 = new MyClass();
ParentClass p = new MyClass();
Interface1 i1 = new MyClass();
Interface2 i2 = new MyClass();

variable is declared to be the type of an interface, its value can reference any object that is instantiated from any class that implements the interface.

变量被声明为接口的类型,它的值可以引用任何实现接口的类实例化的对象。

Consider the last two lines in the previous snippet, a variable of type Interface1 can reference any object that implements this interface, so if we have another class implements Interface1, say MyClass2, then

考虑前面代码片段中的最后两行,类型为Interface1的变量可以引用实现这个接口的任何对象,因此,如果我们有另一个实现了Interface1的类,比如MyClass2

Interface1 i1 = new MyClass();
Interface1 i2 = new MyClasss2();
i1 = i2;
i1 = new MyClass2();

All the previous assignments are valid because MyClass and MyClass2 implements Interface1

所有之前的作业都是有效的,因为MyClass和classmy2实现了Interface1

#4


3  

class Ball extends Rubber implements Jumping, Rolling, Squeezing {
   public void jump(){}
   public void roll(){}
   public void squeeze(){}
}

Ball b = new Ball();
Jumping j = new Ball();
j.jump();

#5


2  

The statements you quote (from where?) are true but misleading -- objects already have multiple types without interfaces.

您所引用的语句(来自何处?)是真实的,但具有误导性——对象已经有多种类型,没有接口。

For example, "bimmelim" has the type String, but it also has the type Object. Interfaces do not change that, except that "bimmelim" also has the type Serializable, CharSequence and others.

例如,“bimmelim”具有类型字符串,但它也有类型对象。接口不会改变这一点,除了“bimmelim”还具有可序列化、CharSequence和其他类型的类型。

Actually, it is possibly debatable whether we should say that "bimmelim" "has" the type Object, but certainly a reference to it will fit into an Object variables.

实际上,我们是否应该说“bimmelim”有“类型对象”是有争议的,但是对它的引用肯定会适合对象变量。

If a variable is declared to be type of an interface ... for example

如果一个变量被声明为接口的类型…例如

CharSequence x ;

... then its value can reference a String object such as "bimmelim", or it could possibly be a StringBuffer, which is another type that implements CharSequence.

…然后,它的值可以引用一个字符串对象,如“bimmelim”,或者可能是一个StringBuffer,这是实现CharSequence的另一种类型。

#6


2  

String implements multiple interfaces, so it has multiple types:

字符串实现多个接口,因此它有多种类型:

String s = "A String";
Comparable<String> comp = s;
CharSequece cs = s;
Serializable ser = s;

The interface CharSequence is implemented by multiple classes, so a CharSequence reference can hold all kinds of objects:

接口CharSequence是由多个类实现的,所以一个CharSequence reference可以容纳所有类型的对象:

CharSequence cs = "A String";
cs = new StringBuilder();
cs = new Segment();

#7


2  

objects can have multiple types

对象可以有多种类型

Example:

例子:

public class Foo implements Runnable, Callable<Integer> {
  public void run() {}
  public Integer call() {return 1;}
}

Foo foo = new Foo();
Runnable r = foo;
Callable<Integer> c = foo;

Example:

例子:

if a variable is declared to be the type of an interface, its value can reference any object that is instantiated from any class that implements the interface

如果一个变量被声明为接口的类型,那么它的值可以引用任何实现接口的类实例化的对象

Runnable r = new Foo();
r = Thread.currentThread();   //Thread implements Runnable

#8


2  

Very basic example-

非常基本的例子,

List<String> list1=new ArrayList<String>();

列表 <字符串> list1 = new ArrayList <字符串> ();

Since, ArrayList implements List therefore we can use List interface variable i.e list1 to refer to the object created by Arraylist.

因为ArrayList实现了List,所以我们可以使用List接口变量i。引用Arraylist创建的对象。

#9


1  

That the following is a correct assignment:

以下是正确的作业:

class AClass implements AInterface {
}


AInterface var = new AClass();

#10


1  

Consider the following class and interface definitions:

考虑以下类和接口定义:

public class A { }

public class B extends A implements I { }

public interface I { }

The following statements are all legal:

下列声明均属合法:

A first = new A();
B second = new B();
A third = new B();
I fourth = new B();

Because B implements I and extends A, it can be used as a value anywhere an "I" or an "A" is expected.

因为B实现了I并扩展了A,所以它可以在任何需要“I”或“A”的地方作为值使用。

#11


1  

Take the Collection interface from the standard Java libraries as an example. Any variable declared as of type Collection can then be assigned an object of a class which implements the Collection interface, e.g. ArrayList, Stack, ... see the linked doc for more examples.

以标准Java库的集合接口为例。然后,任何声明为类型集合的变量都可以被分配到一个类的对象,该对象实现了集合接口,例如ArrayList, Stack,…有关更多示例,请参见链接的doc。

#1


51  

Let's declare two interfaces and a class that implements them both:

让我们声明两个接口和一个实现它们的类:

interface I1 { }

interface I2 { }

class C implements I1, I2 { }

objects can have multiple types

对象可以有多种类型

In the following code, it can be seen that a C instance has the type of C as well as I1 and I2:

在下面的代码中,可以看到一个C实例具有C的类型以及I1和I2的类型:

C c = new C();

boolean isC = (c instanceof C);   //true
boolean isI1 = (c instanceof I1); //true
boolean isI2 = (c instanceof I2); //true

Now let's declare a class B which implements I1 as well:

现在我们声明一个实现I1的类B:

class B implements I1 { }

if a variable is declared to be the type of an interface, its value can reference any object that is instantiated from any class that implements the interface.

如果一个变量被声明为接口的类型,那么它的值可以引用任何实现接口的类实例化的对象。

If we declare a variable of type I1, we can set it to an instance of C, and then reassign it to an instance of B:

如果我们声明类型为I1的变量,我们可以将它设置为C的实例,然后将它重新分配给B的实例:

I1 i1 = new C();
i1 = new B();

We can also reassign it to an instance of D, where D extends C:

我们也可以将它重新分配给D的一个实例,其中D扩展了C:

i1 = new D();

...

class D extends C { }

#2


10  

Consider the following example:

考虑下面的例子:

Serializable s = new ArrayList();

In Java, this is valid code, even though Serializable is an interface, because ArrayList implements Serializable. So in this case, we're treating s as a variable of type Serializable.

在Java中,这是有效的代码,尽管Serializable是一个接口,因为ArrayList实现了Serializable。在本例中,我们将s作为可串行化的变量。

Now suppose we follow up the above code with the following:

现在假设我们按照上面的代码进行如下操作:

s = "String object";

This is also valid becauseString also implements Serializable. Since we declared s as type Serializable, it can point to any object that implements that interface.

这也是有效的,因为string也实现了serizable。由于我们将s声明为Serializable类型,所以它可以指向实现该接口的任何对象。

#3


5  

objects can have multiple types

对象可以有多种类型

Consider the following snippet:

考虑下面的代码片段:

public class MyClass extends ParentClass implements Interface1, Interface2 {    
    //some code    
}

This class can be used in different places as follows:

本课程可在以下不同地方使用:

MyClass m1 = new MyClass();
ParentClass p = new MyClass();
Interface1 i1 = new MyClass();
Interface2 i2 = new MyClass();

variable is declared to be the type of an interface, its value can reference any object that is instantiated from any class that implements the interface.

变量被声明为接口的类型,它的值可以引用任何实现接口的类实例化的对象。

Consider the last two lines in the previous snippet, a variable of type Interface1 can reference any object that implements this interface, so if we have another class implements Interface1, say MyClass2, then

考虑前面代码片段中的最后两行,类型为Interface1的变量可以引用实现这个接口的任何对象,因此,如果我们有另一个实现了Interface1的类,比如MyClass2

Interface1 i1 = new MyClass();
Interface1 i2 = new MyClasss2();
i1 = i2;
i1 = new MyClass2();

All the previous assignments are valid because MyClass and MyClass2 implements Interface1

所有之前的作业都是有效的,因为MyClass和classmy2实现了Interface1

#4


3  

class Ball extends Rubber implements Jumping, Rolling, Squeezing {
   public void jump(){}
   public void roll(){}
   public void squeeze(){}
}

Ball b = new Ball();
Jumping j = new Ball();
j.jump();

#5


2  

The statements you quote (from where?) are true but misleading -- objects already have multiple types without interfaces.

您所引用的语句(来自何处?)是真实的,但具有误导性——对象已经有多种类型,没有接口。

For example, "bimmelim" has the type String, but it also has the type Object. Interfaces do not change that, except that "bimmelim" also has the type Serializable, CharSequence and others.

例如,“bimmelim”具有类型字符串,但它也有类型对象。接口不会改变这一点,除了“bimmelim”还具有可序列化、CharSequence和其他类型的类型。

Actually, it is possibly debatable whether we should say that "bimmelim" "has" the type Object, but certainly a reference to it will fit into an Object variables.

实际上,我们是否应该说“bimmelim”有“类型对象”是有争议的,但是对它的引用肯定会适合对象变量。

If a variable is declared to be type of an interface ... for example

如果一个变量被声明为接口的类型…例如

CharSequence x ;

... then its value can reference a String object such as "bimmelim", or it could possibly be a StringBuffer, which is another type that implements CharSequence.

…然后,它的值可以引用一个字符串对象,如“bimmelim”,或者可能是一个StringBuffer,这是实现CharSequence的另一种类型。

#6


2  

String implements multiple interfaces, so it has multiple types:

字符串实现多个接口,因此它有多种类型:

String s = "A String";
Comparable<String> comp = s;
CharSequece cs = s;
Serializable ser = s;

The interface CharSequence is implemented by multiple classes, so a CharSequence reference can hold all kinds of objects:

接口CharSequence是由多个类实现的,所以一个CharSequence reference可以容纳所有类型的对象:

CharSequence cs = "A String";
cs = new StringBuilder();
cs = new Segment();

#7


2  

objects can have multiple types

对象可以有多种类型

Example:

例子:

public class Foo implements Runnable, Callable<Integer> {
  public void run() {}
  public Integer call() {return 1;}
}

Foo foo = new Foo();
Runnable r = foo;
Callable<Integer> c = foo;

Example:

例子:

if a variable is declared to be the type of an interface, its value can reference any object that is instantiated from any class that implements the interface

如果一个变量被声明为接口的类型,那么它的值可以引用任何实现接口的类实例化的对象

Runnable r = new Foo();
r = Thread.currentThread();   //Thread implements Runnable

#8


2  

Very basic example-

非常基本的例子,

List<String> list1=new ArrayList<String>();

列表 <字符串> list1 = new ArrayList <字符串> ();

Since, ArrayList implements List therefore we can use List interface variable i.e list1 to refer to the object created by Arraylist.

因为ArrayList实现了List,所以我们可以使用List接口变量i。引用Arraylist创建的对象。

#9


1  

That the following is a correct assignment:

以下是正确的作业:

class AClass implements AInterface {
}


AInterface var = new AClass();

#10


1  

Consider the following class and interface definitions:

考虑以下类和接口定义:

public class A { }

public class B extends A implements I { }

public interface I { }

The following statements are all legal:

下列声明均属合法:

A first = new A();
B second = new B();
A third = new B();
I fourth = new B();

Because B implements I and extends A, it can be used as a value anywhere an "I" or an "A" is expected.

因为B实现了I并扩展了A,所以它可以在任何需要“I”或“A”的地方作为值使用。

#11


1  

Take the Collection interface from the standard Java libraries as an example. Any variable declared as of type Collection can then be assigned an object of a class which implements the Collection interface, e.g. ArrayList, Stack, ... see the linked doc for more examples.

以标准Java库的集合接口为例。然后,任何声明为类型集合的变量都可以被分配到一个类的对象,该对象实现了集合接口,例如ArrayList, Stack,…有关更多示例,请参见链接的doc。