61B: Writing Efficient Programs Programming cost. How long does it take to develop your programs? How easy is it to read, modify, and maintain your code? More important than you might think! Majority of cost is in maintenance, not development! 自顶向下,逐层抽象,分而治之,化整为零
ADT Implementations Designing ADTs 虽然extension简单,但是委托delegation更加灵活
Views 视图 在Java中,“view"通常指的是一种数据结构的视图,它提供了一种访问和操作底层数据的方式,而不需要复制整个数据集。视图的主要优点是它们提供了一种高效的方式来操作数据子集,而不需要复制数据,从而节省内存和提高性能。然而,视图也有一些限制,例如固定大小的视图不能添加或删除元素。
Occasionally, implementation details may allow for views that are too difficult to implement for an abstract type.
Programming in the Real World 对技术要敬畏 midterm review Comparing strings for equality using == vs .equals —> see in autoboxing lecture 在Java中,this 是一个指向当前对象实例的引用。它通常用于引用当前类的实例成员,或者在方法中区分成员变量和局部变量。然而,你不能将 this 重新赋值为另一个对象的引用,因为 this 是一个固定的概念,它代表当前对象本身。
你提供的代码示例中,尝试将 this 赋值为一个新的 Dog 对象,这是不允许的。Java 编译器会报错,因为它违反了 this 的使用规则。
1 2 3 4 5 6 7 8 public class Dog { public void f() { this = new Dog(); // 这行代码会导致编译错误,因为不能重新赋值this } } Dog d = new Dog(); d.f(); // 调用f()方法,但由于上面的错误,这行代码实际上无法执行 如果你想要创建一个新的 Dog 对象并将其引用赋给 this,你需要使用另一个变量,比如 anotherDog。下面是修改后的代码示例:
13-16几乎是java语法讲解😄
Primitives Cannot Be Used as Actual Type Arguments Autoboxing Wrapper Types Are (Mostly) Just Like Any Class
8种基本类型之间转换也存在widening
Immutability 类似于const在cpp
1 2 3 4 5 6 7 8 9 public class Date { public final int month; public final int day; public final int year; private boolean contrived = true; public Date(int m, int d, int y) { month = m; day = d; year = y; } } Warning: Declaring a reference as Final does not make object immutable.