`
leonzhx
  • 浏览: 791812 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Item 41: Use overloading judiciously

阅读更多

1.  The choice of which overloading to invoke is made at compile time. Selection among overloaded methods is static, while selection among overridden methods is dynamic.

 

2.  A safe, conservative policy is never to export two overloadings with the same number of parameters. If a method uses varargs, a conservative policy is not to overload it at all.

 

3.  ObjectOutputStream has a variant of its write method for every primitive type and for several reference types. Rather than overloading the write method, these variants have signatures like writeBoolean(boolean), writeInt(int), and writeLong(long). An added benefit of this naming pattern, when compared to overloading, is that it is possible to provide read methods with corresponding names.

 

4.  Multiple constructors for a class are always overloaded. You do, in many cases, have the option of exporting static factories instead of constructors.

 

5.  Exporting multiple overloadings with the same number of parameters is unlikely to confuse programmers if it is always clear which overloading will apply to any given set of actual parameters. This is the case when at least one corresponding formal parameter in each pair of overloadings has a “radically different” type in the two overloadings. Two types are radically different if it is clearly impossible to cast an instance of either type to the other. 

 

6.  Array types and classes other than Object are radically different. Also, array types and interfaces other than Serializable and Cloneable are radically different.

 

7.  You should at least avoid situations where the same set of parameters can be passed to different overloadings by the addition of casts.

 

8.  It causes no harm as long as both overloaded methods always do exactly the same thing when they are invoked on the same object reference. (i.e. String.contentEquals can take either StringBuffer and CharSequence as parameter.) The standard way to ensure this behavior is to have the more specific overloading forward to the more general.

 

分享到:
评论

相关推荐

    Effective Java 3rd edition(Effective Java第三版英文原版)附第二版

    Item 41: Use marker interfaces to define types 7 Lambdas and Streams Item 42: Prefer lambdas to anonymous classes Item 43: Prefer method references to lambdas Item 44: Favor the use of standard ...

    Think in C++ 英文版(含卷一、卷二)

    7: Function Overloading & Default Arguments 8: Constants 9: Inline Functions 10: Name Control 11: References & the Copy-Constructor 12: Operator Overloading 13: Dynamic Object Creation 14: Inheritance...

    Effective C#

    **Item 1: Use Properties Instead of Accessible Data Members** - **Benefits:** Using properties instead of public data members enhances encapsulation and allows for validation logic, lazy loading, and...

    FunctionOverloading:FunctionOverloading是一个概念,其中在类内部,我们在列表中定义了具有相同名称和不同原型的函数的两个定义

    函数重载是C++编程语言中的一个重要特性,它允许在一个作用域内定义多个同名函数,但这些函数的参数列表必须有所不同。这可以是通过不同的参数数量、参数类型或者参数的顺序来实现的。函数重载的主要目的是提高代码...

    Google C++ Style Guide_英文版.pdf

    - **Function Overloading:** Use function overloading judiciously to provide a clear API. - **Default Arguments:** Default arguments can make functions more flexible but can also introduce ambiguity. -...

    软件设计课件:Lecture 07 C Operator Overloading.ppt

    在“软件设计课件:Lecture 07 C++ Operator Overloading.ppt”中,该主题深入探讨了这个概念及其在实际编程中的应用。 首先,运算符重载的基本思想是赋予已存在的运算符新的含义,使得当这些运算符用于不同的数据...

    Overloading&Overriding

    "Overloading&Overriding" 在面向对象编程中,Overloading和Overriding是两个重要的概念,它们都是多态性的体现。下面我们将对这两个概念进行详细的解释。 1. 方法重载(Overloading) 方法重载是让类以统一的...

    C++primer sales_item

    4. **操作符重载(Operator Overloading)**: - `operator+=`是成员操作符,它通过隐式传递的`this`指针与右侧的`Sales_item`对象相加。这允许我们直接在`Sales_item`对象上进行累加操作,如`item += anotherItem`...

    软件设计课件:Lecture 07 C++ Operator Overloading.ppt

    C++中的运算符重载是面向对象编程的一个关键特性,它允许我们为用户自定义类型定义运算符的行为,使得这些类型能够像内置类型(如int、float等)一样使用标准运算符。运算符重载并不创建新的运算符,而是对已有...

    Addison.Wesley.C++.by.Dissection.2002.pdf

    - **Software Engineering: When to Use Overloading:** Provides guidelines on when to use overloading. - **C++ Compared with Java:** Continues the comparison. ### Chapter 6: Templates and Generic ...

    Cplusplus.in.24.Hours.Sams.Teach.Yourself.6th.Edition

    Use operator overloading Extend classes with inheritance Use polymorphism and derived classes Employ object-oriented analysis and design Table of Contents Part I: Beginning C++ HOUR 1: Writing Your ...

    operator-overloading-js, JS简单运算符重载库.zip

    operator-overloading-js, JS简单运算符重载库 目录Operator-Overloading-JS安装工具node MODULE ( NPM )浏览器( Bower )Sneak示例重载运算符设计考虑/非常重要/必须阅读。Definig使用运算符重载的上下文

    Functional Programming with Overloading and Higher-Order Polymorphism PDF 英文

    另一个重要的概念是过载(overloading)。在函数式编程中,过载通常指的是同一个函数名可以用于不同的数据类型。这是通过类型类(type classes)机制实现的,类型类定义了一组函数签名,任何实现了这些签名的类型都...

    Learn.Cplusplus.In.A.DAY.1519318588.epub

    Why Use C++? What You Need to Code in C++ Program Structure and Comments Functions and Return Values Variables and Data Types Characters and Strings Boolean Values Constants Operators Basic Arithmetic...

    oop-java-overloading-KovacsAliz:oop-java-overloading-KovacsAliz由GitHub Classroom创建

    在这个项目"oop-java-overloading-KovacsAliz"中,我们将深入探讨Java中的一个关键概念——方法重载(Method Overloading)以及构造函数的应用。 方法重载是Java中实现多态性的一种方式,允许在一个类中定义多个...

    Function_Overloading_Illustration.rar_C Builder

    在C++编程语言中,函数重载(Function Overloading)是一项重要的特性,它允许我们在同一个作用域内定义多个同名但具有不同参数列表的函数。这样做的好处是能够为相似的操作提供简洁且易于理解的接口,同时提高了...

    oop-java-overloading-mogyorosibalin:oop-java-overloading-mogyorosibalin由GitHub Classroom创建

    Java是一种广泛使用的面向对象编程(OOP)语言,它的核心特性之一就是方法的重载(Overloading)。在Java中,方法重载是指在一个类中可以有多个同名的方法,但这些方法的参数列表必须有所不同,这可以是参数的数量、...

Global site tag (gtag.js) - Google Analytics