mixin和泛型类:完美组合。
一个静态的泛型和非泛型类工厂方法?
A static factory method on a generic or a non-generic class?
您可以使用非泛型基接口间接访问泛型类。
You can access a generic class indirectly using a nongeneric base interface.
编辑任何泛型类声明。
有时我们想限制可能出现的泛型类的类型实例化。
Sometimes we want to restrict the potential type instantiations of a generic class.
这个泛型类的每个应用都会继承一个不同的父类。
Each application of this generic class would extend a distinct parent class.
对程序可以产生的泛型类的实例化数目设置上限。
Place an upper limit on the number of instantiations of a generic class that a program can make.
实际的类型参数可以由泛型类定义的类型变量匹配。
The actual type arguments can be matched to the type variables defined by the generified class.
泛型类可被视为将它们的参数映射成新实例化的函数。
Generic classes can be viewed as functions mapping their arguments to new instantiations.
我们对程序可以产生的泛型类的实例化数目设置上限。
We could put an upper limit on the number of instantiations of a generic class that a program can make.
当泛型类在其自己的主体中实例化其本身时,发生多态递归。
Polymorphic recursion occurs when a generic class instantiates itself in its own body. For example, consider the following devious example.
这个泛型类处理成对的值,类型参数给出了这些对中项目的类型。
This generic class handles pairs of values, with the type parameters giving the types of the items in the pairs.
当mixin被明确表述为泛型类时,意外覆盖的问题特别讨厌。
When mixins are formulated as generic classes, the problem of accidental overrides is particularly nasty.
例如,如果您知道您的泛型类仅用于引用类型,则应用类约束。
For example, if you know that your generic class is intended for use only with reference types, apply the class constraint.
例如,在泛型类List中,您真的想要防止List的构造吗?
For example, in a generic class List, do you really want to prevent the construction of a List?
只要泛型类的运行时实例化没有包括所需的构造函数,就抛出异常。
Throw an exception whenever a run-time instantiation of the generic class does not include a needed constructor.
泛型类最常用于集合,如链接列表、哈希表、堆栈、队列、树等。
The most common use for generic classes is with collections like linked lists, hash tables, stacks, queues, trees, and so on.
原因是:mixin和泛型类都能被认为是将现有类映射到新类的函数。
The reason: both mixins and generic classes can be thought of as functions mapping existing classes to new classes.
所有的具体工厂都由泛型类——ControlsFactory实现。
All of the specific factories are implemented using generics - ControlsFactory class.
在泛型类中定义的委托,可以与类的方法一样地使用泛型类的类型参数。
Specialized generic types are created once for each unique value type used as a parameter.
例如,泛型类JSplitPane可以创建Scrollable的实例。
For example, a generic class JSplitPane may create an instance of Scrollable.
为了这个目的,我要使用在前两篇文章中开始的代表文件目录的同一套泛型类。
For that, I'm going to use the same set of generic classes for representing file directories that I started with two articles back.
再提一次,那是因为一个泛型类可以用其自己的类型参数来实例化另一个泛型类。
Again, that's because one generic class may instantiate another with its own type parameters.
因而,在泛型类定义内部,唯一可调用的构造函数是那些在界限中声明的构造函数。
Then, inside the generic class definition, the only constructors that could be called are those declared in the bound.
运行时缺乏类型信息,这给泛型容器类和希望创建保护性副本的泛型类提出了难题。
This lack of type information at runtime poses a problem for generic container classes and for generic classes that want to make defensive copies.
同样,实例化泛型类的客户机类必须使用满足对构造函数存在所声明的约束的类来这样做。
Also, client classes that instantiate our generic class must do so with classes that meet the declared constraint on what constructors exist.
在定义泛型类时,可以对客户端代码能够在实例化类时用于类型参数的类型种类施加限制。
When you define a generic class, you can apply restrictions to the kinds of types that client code can use for type arguments when it instantiates your class.
将mixin明确表述为泛型类,我们就始终有父类的句柄,它是该类继承的类型参数。
With mixins formulated as generic classes, we always have a handle on the parent class, the type parameter that the class extends.
通过允许指定泛型类或方法操作的类型,泛型功能将类型安全的任务从您转移给了编译器。
By allowing you to specify types that are acted on by a generic class or method, the generics feature shifts the burden of type safety from you to the compiler.
处理该问题的另一种方法是:只要泛型类的运行时实例化没有包括所需构造函数,就抛出异常。
Another way of handling this problem would be to throw an exception whenever a run-time instantiation of the generic class does not include a needed constructor.
应用推荐