zoukankan      html  css  js  c++  java
  • Thinking in java——Generics

    ​Ordinary classes and methods work with specific types: either primitives or class types. If you are writing code that might be used across more types, this rigidity can be overconstraining.

    One way that object-oriented languages allow generalization is through polymorphism. You can write (for example) a method that takes a base class object as an argument, and then use that method with any class derived from that base class. Now your method is a little more general and can be used in more places. The same is true within classes—anyplace you use a specific type, a base type provides more flexibility. Of course, anything but a final(Or a class with all private constructors) class can be extended, so this flexibility is automatic much of the time.

    Sometimes, being constrained to a single hierarchy is too limiting. If a method argument is an interface instead of a class, the limitations are loosened to include anything that implements the interface—including classes that haven't been created yet. This gives the client programmer the option of implementing an interface in order to conform to your class or method. So interfaces allow you to cut across class hierarchies, as long as you have the option to create a new class in order to do so.

    Sometimes even an interface is too restrictive. An interface still requires that your code work with that particular interface. You could write even more general code if you could say that your code works with "some unspecified type," rather than a specific interface or class.

    This is the concept of generics, one of the more significant changes in Java SE5. Generics implement the concept of parameterized types, which allow multiple types. The term "generic" means "pertaining or appropriate to large groups of classes." The original intent of generics in programming languages was to allow the programmer the greatest amount of expressiveness possible when writing classes or methods, by loosening the constraints on the types that those classes or methods work with. As you will see in this chapter, the Java implementation of generics is not that broad reaching—indeed, you may question whether the term "generic" is even appropriate for this feature.

    If you've never seen any kind of parameterized type mechanism before, Java generics will probably seem like a convenient addition to the language. When you create an instance of a parameterized type, casts will be taken care of for you and the type correctness will be ensured at compile time. This seems like an improvement.

    However, if you've had experience with a parameterized type mechanism, in C++, for example, you will find that you can't do everything that you might expect when using Java generics. While using someone else's generic type is fairly easy, when creating your own you will encounter a number of surprises.One of the things I shall try to explain is how the feature came to be like it is.

  • 相关阅读:
    aud$定位错误用户密码登陆数据库的具体信息
    Linux 磁盘分区、格式化、目录挂载
    Linux RHCS 基础维护命令
    Vertica 6.1不完全恢复启动到LGE方法
    Oracle Recovery 02
    如何删除回滚段状态为NEEDS RECOVERY的undo表空间
    Oracle Recovery 01
    DRA(Data Recovery Advisor)的使用
    Oracle启动报错ORA-27102解决
    jquery remove()不兼容问题解决方案
  • 原文地址:https://www.cnblogs.com/ghgyj/p/3650886.html
Copyright © 2011-2022 走看看