纯java程序员读effective c++体会

一,对象的传递
c++在传递对象的时候,很关注到底是对象(即复制对象),还是引用(即传原对象),以及是否const,除了效率之外,还牵涉到是否可修改,也就是语言的严谨性
但是到了java中,因为语言没有强制要求,很容易出现本该复制的对象,本体却到处乱传,特别是愚蠢的自动生成getter,setter,就是万恶之源,
二,封装
先上例子

public class A{
private int i = 0;
public void a(){
i++;
}
public void b(){
i--;
}
public void c(){
a();
b();
}
}

这代码OK吗?NO!封装要做的是什么?封装就是控制对象field的可见性,所以public field是糟糕的设计.
在例子里,a,b是应该见到i的,所以没错,c没用到i,所以他不应该见到i,应该移出class A
这里有一个面向对象的典型误区,认为一个对象的方法就应该放在对象中,抱着这种想法的话,必然导致对象臃肿
我再次强烈批判愚蠢的自动生成getter,setter,他使得对象毫无封装可言,罪恶直逼public field,而如果getter,setter里做了私活,就违反了最小惊愕原则,所以最好是确实地把该做的活做好,然后换个可读的方法名
三,效率
即使在c++,for循环里选择new对象还是赋值,到底哪个效率高,也是不一定的,要视情况而定.
这本书的内容,与其说是教你怎么写的有效率,不如说其实是教你怎么写的正确,所谓最求效率的写法,实在少有必要,真正重要的是正确的编码和正确的设计
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Book Description Written for the moderately experienced Java programmer, this book builds on readers¿ existing knowledge of object-oriented programming and covers all important aspects of Standard C++—emphasizing more lower-level C-style details later in the presentation. Chapter topics include philosophy of C++, simplest C++, pointers and reference variables, object-based programming: classes, operator overloading, object-oriented programming: inheritance, templates, abnormal control flow, input and output, collections: the standard template library, primitive arrays and strings, C-style C++, and using Java and C++: the JNI. For new C++ programmers converted from Java. For experienced Java programmers and students who require the skills of C++ programming, best-selling author Mark Allen Weiss bridges the gap. He efficiently presents the complex C++ language in this well-designed tutorial/reference that both students and seasoned programmers will appreciate. The book is ideal as a primary text for intermediate C++ courses, as a supplemental no-nonsense reference for other courses, or for independent learning by professionals. C++ for Java Programmers is a concise, well-written text that provides authoritative and up-to-date coverage of key features and details of C++, with a special focus on how C++ compares to Java. The book's approach shows knowledgeable students or professionals how to grasp the complexities of C++ and harness its power by mutually addressing the benefits and the pitfalls of the two languages. By highlighting the features and comparative elements of each language, and building on the reader's existing knowledge of object-oriented programming, C++ for Java Programmers enables users to master the essentials of C++ quickly and thoroughly. Key Features Includes insightful comparisons of the two programming languages throughout the text and points out the subtleties of C++ Succinctly covers the pertinent highlights of STL (Standard Template Library) and the most effective use of templates Explains the use of the powerful JNI (Java Native Interface) for combining Java and C++ Includes a summary of key C++ features and issues with each chapter Provides extensive treatment of C details the programmer is likely to encounter in C++ Companion Website for complete online source code at: http://www.prenhall.com/weiss Available Instructors Resource CD-ROM Product Details Paperback: 304 pages Publisher: Prentice Hall; 1 edition (November 7, 2003) Language: English ISBN-10: 013919424X ISBN-13: 978-0139194245 Product Dimensions: 9.5 x 6.8 x 0.6 inches
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值