Skip to main content
Advanced Search
Search Terms
Content Type

Exact Matches
Tag Searches
Date Options
Updated after
Updated before
Created after
Created before

Search Results

127 total results found

Guide to CS 61A

Guide to CS 61A Introduction

Welcome to one of the most widely known introductory Computer Science courses, UC Berkeley's CS 61A. If you're anything like... well, probably around half of the people that take this class, you know that you're interested in Computer Science but have little t...

4. 使用私有构造器执行非实例化

Effective Java 第二章 创建和销毁对象

  偶尔你会想写一个只包含静态方法和静态字段的类。 这些类的名声非常不好,因为有些人滥用这些类从而避免以面向对象方式思考从而编写过程化的程序,但是它们确实有着特殊的用途。 它们可以用来按照 java.lang.Math 或 java.util.Arrays 的方式,把基本类型的值或数组类型上的相关方法组织起来。我们也可以通过 java.util.Collections 的方式,把实现特定接口上面的静态方法进行分组,也包括工厂方法(详见第 1 条)。 (从 Java 8 开始,你也可以将这些方法放在接口中,假定该接口...

11. 重写 equals 方法时同时也要重写 hashcode 方法

Effective Java 第三章 对象的通用方法

  在每个类中,在重写 equals 方法的时侯,一定要重写 hashcode 方法。 如果不这样做,你的类违反了 hashCode 的通用约定,这会阻止它在 HashMap 和 HashSet 这样的集合中正常工作。根据 Object 规范,以下时具体约定。 如果没有修改 equals 方法中用以比较的信息,在应用程序的一次执行过程中对一个对象重复调用 hashCode 方法时,它必须始终返回相同的值。在应用程序的多次执行过程中,每个执行过程在该对象上获取的结果值可以不相同。 如果两个对象根据 equals(Ob...

10. 重写 equals 方法时遵守通用约定

Effective Java 第三章 对象的通用方法

  虽然 Object 是一个具体的类,但它主要是为继承而设计的。它的所有非 final 方法(equals、hashCode、toString、clone 和 finalize)都有清晰的通用约定( general contracts),因为它们被设计为被子类重写。任何类要重写这些方法时,都有义务去遵从它们的通用约定;如果不这样做,将会阻止其他依赖于约定的类 (例如 HashMap 和 HashSet) 与此类一起正常工作。   本章论述何时以及如何重写 Object 类的非 final 的方法。这一章省略了 f...

9. 使用 try-with-resources 语句替代 try-finally 语句

Effective Java 第二章 创建和销毁对象

  Java 类库中包含许多必须通过调用 close 方法手动关闭的资源。 比如 InputStream,OutputStream 和 java.sql.Connection。 客户经常忽视关闭资源,其性能结果可想而知。 尽管这些资源中有很多使用 finalizer 机制作为安全网,但 finalizer 机制却不能很好地工作(详见第 8 条)。   从以往来看,try-finally 语句是保证资源正确关闭的最佳方式,即使是在程序抛出异常或返回的情况下: // try-finally - No longer th...

8. 避免使用 Finalizer 和 Cleaner 机制

Effective Java 第二章 创建和销毁对象

  Finalizer 机制是不可预知的,往往是危险的,而且通常是不必要的。 它们的使用会导致不稳定的行为,糟糕的性能和移植性问题。 Finalizer 机制有一些特殊的用途,我们稍后会在这个条目中介绍,但是通常应该避免它们。 从 Java 9 开始,Finalizer 机制已被弃用,但仍被 Java 类库所使用。 Java 9 中 Cleaner 机制代替了 Finalizer 机制。 Cleaner 机制不如 Finalizer 机制那样危险,但仍然是不可预测,运行缓慢并且通常是不必要的。   提醒 C++程序...

7. 消除过期的对象引用

Effective Java 第二章 创建和销毁对象

  如果你从使用手动内存管理的语言(如 C 或 C++)切换到像 Java 这样的带有垃圾收集机制的语言,那么作为程序员的工作就会变得容易多了,因为你的对象在使用完毕以后就自动回收了。当你第一次体验它的时候,它就像魔法一样。这很容易让人觉得你不需要考虑内存管理,但这并不完全正确。   考虑以下简单的栈实现: // Can you spot the "memory leak"? public class Stack { private Object[] elements; private int si...

6. 避免创建不必要的对象

Effective Java 第二章 创建和销毁对象

  在每次需要时重用一个对象而不是创建一个新的相同功能对象通常是恰当的。重用可以更快更流行。如果对象是不可变的(详见第 17 条),它总是可以被重用。   作为一个不应该这样做的极端例子,请考虑以下语句: String s = new String("bikini"); // DON'T DO THIS!   语句每次执行时都会创建一个新的 String 实例,而这些对象的创建都不是必需的。String 构造方法 ("bikini") 的参数本身就是一个 bikini 实例,它与构造方法创建的所有对象的功能相同...

5. 依赖注入优于硬连接资源(hardwiring resources)

Effective Java 第二章 创建和销毁对象

  许多类依赖于一个或多个底层资源。例如,拼写检查器依赖于字典。将此类类实现为静态工具类并不少见 (详见第 4 条): // Inappropriate use of static utility - inflexible & untestable! public class SpellChecker { private static final Lexicon dictionary = ...; private SpellChecker() {} // Noninstantiable ...

3. 使用私有构造方法或枚类实现 Singleton 属性

Effective Java 第二章 创建和销毁对象

  单例是一个仅实例化一次的类[Gamma95]。单例对象通常表示无状态对象,如函数 (详见第 24 条) 或一个本质上唯一的系统组件。让一个类成为单例会使测试它的客户变得困难,因为除非实现一个作为它类型的接口,否则不可能用一个模拟实现替代单例。   有两种常见的方法来实现单例。两者都基于保持构造方法私有和导出公共静态成员以提供对唯一实例的访问。在第一种方法中,成员是 final 修饰的属性: // Singleton with public final field public class Elvis { ...

About This Guide

Guide to CS 61A Welcome to 61A!

Thanks for stopping by. I hope this guide is of good use to you, but before we get into it, I just want to talk a little bit about how to best use this guide, as well as some other administrivia. How to Use This Guide This guide is not a textbook, so please do...

2. 当构造方法参数过多时使用 builder 模式

Effective Java 第二章 创建和销毁对象

  静态工厂和构造方法都有一个限制:它们在可选参数很多的情景下,无法很好得扩展。请考虑一个代表包装食品上的营养成分标签的例子。这些标签有几个必需的属性——每次建议的摄入量,每罐的份量和每份卡路里 ,以及超过 20 个可选的属性——总脂肪、饱和脂肪、反式脂肪、胆固醇、钠等等。大多数产品只包含这些可选字段中的少数,且具有非零值(大部分字段为空)。   应该为这样的类编写什么样的构造方法或静态工厂?传统上,程序员使用了可伸缩(telescoping constructor)构造方法模式。在这种模式中,首先提供一个只有必需...

1. 考虑使用静态工厂方法替代构造方法

Effective Java 第二章 创建和销毁对象

  一个类允许客户端获取其实例的传统方式,是提供一个公共构造方法。 其实,还有另一种技术应该成为每个程序员工具箱的一部分。 一个类可以提供一个简单的、只返回该类实例的公共静态工厂方法。 下面是一个 Boolean 简单的例子( 基本类型boolean的包装类)。 此方法将基本类型boolean转换为 Boolean 对象引用: public static Boolean valueOf(boolean b) { return b ? Boolean.TRUE : Boolean.FALSE; }   注意...

Setup (Lab 0)

Guide to CS 61A Welcome to 61A!

We've covered the basics, and it's time to get into it. Let's go ahead and install Python to get ourselves going. Lab 0 The course staff has done an incredible job in writing Lab 0, which guides you through the process of installing Python and also introduces ...

Functions

Guide to CS 61A Welcome to 61A!

But what can you really do with all of these different expressions? If you combine them into functions, quite a bit. What are Functions? As with algebra, a function is something that takes one or more inputs, does something with this input, and returns an outp...

Expressions & Names

Guide to CS 61A Welcome to 61A!

We'll start off a little slow and talk about expressions in python. From your math classes, you probably know that an expression is anything that can be evaluated, like 24+3, 5/2, 6.72, etc.. In Python, expressions are pretty much the same -- anything that can...

Environment Diagrams

Guide to CS 61A Welcome to 61A!

Now that we know how to start basic coding, we should also look into how we can visualize our code. On a large scale, this is relatively hard to do as the project gets too big to visualize. However, when first learning how to program, it can be extremely usefu...

Computer Science, Briefly

Guide to CS 61A Welcome to 61A!

Before we dive head-first into Python and the fundamentals of Computer Science, it's important that you understand exactly what "Computer Science" entails. You probably already have your own definition of CS -- and that definition is probably why you're here -...

What is CS 61A?

Guide to CS 61A Welcome to 61A!

Now that we've established some of my incoherent thoughts about computer science, let's take a look at what this class in particular is about. Intro to Programming This class will first and foremost teach you how to program. You will gain a decent understandin...

12. 始终重写 toString 方法

Effective Java 第三章 对象的通用方法

  虽然 Object 类提供了 toString 方法的实现,但它返回的字符串通常不是你的类的用户想要看到的。 它由类名后跟一个「at」符号(@)和哈希码的无符号十六进制表示组成,例如 PhoneNumber@163b91。 toString 的通用约定要求,返回的字符串应该是「一个简洁但内容丰富的表示,对人们来说是很容易阅读的」。虽然可以认为 PhoneNumber@163b91 简洁易读,但相比于 707-867-5309,但并不是很丰富 。 toString 通用约定「建议所有的子类重写这个方法」。好的建议...