CS61B-lecture10,11
Comparison
General Maximization Function Through Inheritance
OurComparable接口
1 | public interface OurComparable { |
1 | Dog[] dogs = new Dog[]{d1, d2, d3}; |
Benefits of this approach
- No need for array maximization code in every custom type (i.e. no Dog.maxDog(Dog[]) function required).
- Code that operates on multiple types (mostly) gracefully, e.g.
1
2OurComparable[] objs = getItems(“somefile.txt”);
return Maximizer.max(objs);
two issues
- Awkward casting to/from Objects.
- We made it up.
- No existing classes implement OurComparable (e.g. String, etc).
- No existing classes use OurComparable (e.g. no built-in max function that uses OurComparable)
Comparable接口
1 | public interface Comparable<T> { |
Ourcomparable 代码
1 | public interface OurComparable { |
Comparable Advantages
- Lots of built in classes implement Comparable (e.g. String).
- Lots of libraries use the Comparable interface (e.g. Arrays.sort)
- Avoids need for casts.
Comparators
Natural Order
- 数字大小排列
- 按名称排列
Compartor接口
1 | public interface Comparator<T> { |
接口实现与使用
1 | public class Dog implements Comparable<Dog> { |
以上接口的总结
- Sometimes a function needs the help of another function that might not have been written yet.
- Example: max needs compareTo
- The helping function is sometimes called a “callback”.
- Some languages handle this using explicit function passing.
- In Java, we do this by wrapping up the needed function in an interface (e.g. Arrays.sort needs compare which lives inside the comparator interface)
- Arrays.sort “calls back” whenever it needs a comparison.
- Similar to giving your number to someone if they need information.
- See Project 1B to explore how to write code that uses comparators.
关于静态类的想法
猜想: 静态类能被用来调用其中的静态方法(对parameter进行计算)
Java libraries
接口的另外两个好处:
- 提供一个供许多类共有的方法
- 提供一个通用方法 例:
compartor
Abstract Data Types(抽象数据类型)
我的理解:我是一个有需求的ADT,需要实现我的方法,而这些方法有多种方式可以实现
Collection
collection interface以及继承它的interface是java.util library 中最重要的几个接口
继承了collection接口的三个接口:
- Lists of things.
- Sets of things.
- Mappings between items, e.g. jhug’s grade is 88.4.
Maps also known as associative arrays, associative lists (in Lisp), symbol tables, dictionaries (in Python).
java.util package
在这个包中有接口和我们可以用的具体类
3 tasks, given the text of a book
Count the number of unique words.
Example: Using a Set to Count Unique Words
1 | public static int countUniqueWords(List<String> words) { |
1 | public static int countUniqueWords(List<String> words) { |
Example: Using a Map to Create Counts of Specific Words
1 | public static Map<String, Integer> |
接口的实现方式有多种选择
Allows power user to explicitly handle engineering tradeoffs.
Interfaces
Inheritance Summary (So Far)
- Interface inheritance: What (the class can do).
- Implementation inheritance: How (the class does it).
Interfaces may combine a mix of abstract and default methods.
- Abstract methods are what. And must be overridden by subclass.
- Default methods are how.
其他要注意的
- Unless you use the keyword default, a method will be abstract.
- Unless you specify an access modifier, a method will be public.
- Can provide variables, but they are public static final.
- final means the value can never change. Use for constants: G=6.67e-11
- A class can implement multiple interfaces.
Interface Summary
- 不能被实例化
- Can provide either abstract or concrete methods.
- Use no keyword for abstract methods.
- Use default keyword for concrete methods.
- Can provide only public static final variables. 变量无法更改
Abstract Classes
Abstract classes
- 不能被实例化
- 与接口相反,抽象方法需要使用
abstract
来标注 - 可以有任何类型的变量
- Can provide protected and package private methods [after mt1].(我对mt1的理解是在第一个方法之后)
通常用法
为接口的实现提供基础
1 | public abstract class AbstractList61B<T> implements List61B<T> { |
好处和坏处
Good
Avoids the need to write a size() method or declare a size variable in AList and SLList.
Bad
混淆结构 (e.g. maybe you don’t want a size variable).
Summary: Abstract Classes vs. Interfaces
Interfaces
- Primarily for interface inheritance. Limited implementation inheritance.
- Classes can implement multiple interfaces.
Abstract classes
- Can do anything an interface can do, and more.
- Subclasses only extend one abstract class.
An example for abstract class
- AbstractList provides default implementations for methods.
- Why not just put them in List itself? No default methods in Java interfaces until 2014, and the AbstractList was public so can’t just throw it away.
1 | public abstract class AbstractList<E> |
Packages
To address the fact that classes might share names:
- A package is a namespace that organizes classes and interfaces.
- Naming convention: Package name starts with website address (backwards).
注意: 我们不用遵循第二条因为我们不会distribution代码
使用
包外
加上包名
1 | \\Dog.java |
1 | \\e.g. 1 |
包内
If used from another class in same package (e.g. ug.joshh.animal.DogLauncher), can just use simple name.
import class
1 | import ug.joshh.animal.Dog; |
import 多个class
1 | import ug.joshh.animal.*; |
import static
1 | import static org.junit.Assert.assertEquals; |
Dangerous! Will cause compilation error if another *
imported class contains Dog.
命令行使用
跳过,有空看