这是我学习CS61B Spring 2014第一,二课整理的笔记,先记录下来课堂内容以便后期复习,希望大家学习愉快!
课堂内容版权为伯克利CS61B所有
课堂链接:https://www.bilibili.com/video/BV1vt411W7yu?p=1
Objected-Oriented Programming
-Object: A repository of data
-Class: Type of object
-Method: Procedure or function that operates on an object
-Inheritance: A class may inherit properties from a more general class
例如: ShoppingList inherits from List the property of stroing a sequence of items
-Polymorphism: one method works on several classes, even if the classes need different implementations
例如:”addItem“ method on every kind of List, though adding item to a ShoppingList is different from a ShoppingCart
-Objected-Oriented: Each object knows its own class & methods
例如:Each ShoppingList & ShoppingCart knows which implementation applies to it
Java
-Variables: you must declare them and their types
int x;
x = 1;
Does 2 things (int x;) :
- Allocates memory to store an integer, type “int”.
- Names variable “x”.
-Variables also used to reference objects (point to an object)
2 ways to get classes
- Use one defined by somebody else. Java has tons of predefined classes that are built-in (Java standard library)
- Define your own
这节课主要讲第一种办法 (defined by somebody else)
String myString; //(String: class built into Java)
Declare了myString之后,并没有创造出一个string object,而是store一个reference variable that can point to a string object
myString □ <-- variable (not object)
myString = new String();
String(): constructor signature
new String(): constructor call
2 steps:
- “new String()” is a constructor: construct a brand new string object; in this case, it’s an empty string with no characters in it
- assignment “=” causes myString to reference the object
myString □–> □ (a String object)
也可以直接写 new String(); 但是java会扔掉,因为没有variable指向它
Java programs must be compiled before you can run them
Java program (.java) --javac–> .class files --java (JVM)–> answer
Objects & Constructors
String s; // step 1: declare a String variable
s = new String(); // step 2,3: construct empty Sring; assign it to s
s □–>□
String s = new String(); // steps 1,2,3 combined
s = "Yow!";
String s2 = s; // now s & s2 reference (point to) same object
s2 = new String(s);
// now referencing 2 different identical objects
// 1. look where s points
// 2. Follows reference to String object
// 3. Reads String
// 4. Constructs new String with copy of characters
// 5. Makes s2 reference new String
All null pointers do not point anything
3 String constructors:
- new String(); constructs an empty string
- “Yow!” --> Java considers this as a special string constructor
- new String(s) --> takes a parameter s, makes copy of object that s references
Constructors always have same name as their class, except “stuff in quotes”
Methods
s2 = s.toUppercase();
String s3 = s2.concat("!!");
这里的”!!“也是一个constructor,给concat使用,但是没有variable point to”!!", 最后会变成garbage collected
-The object “Yow!” did not change.
-Strings are immutable (不可以被改变的). Their contents never change
-Most objects 可以被change internal fields,但是String是特例
I/O classes & objects
-Objects in System class (在Java standard libraries里) for interacting with a user;
-System.out is a PrintStream object that outputs to the screen
System.out□–> □ (PrintStream object)
-System.in is an InputStream object that reads from the keyboard
(out是一个在System class里的variable, 如果要在其他的class里表示out variable, 要写成System.out
-readLine (a method) is defined on BufferedReader objects
- How do we construct a BufferedReader? With an InputStreamReader
- How do we construct an InputStreamReader? With an InputStream
- How do we construct an InputStream? System.in is one
Figure this out via online Java Libraries API – java.io
InputStream object: reads raw data
InputStreamReader: compose raw data into characters (2 bytes long)
BufferedReader: compose into entire lines of text
Object orientation encourages you to write your code into decomposable pieces, 所以可以take one piece 然后改成另外一个different piece
import java.io.*;
class SimpleIO {
public static void main(String[] arg)throws Exception {
BufferedReader keybd = new BufferedReader(new InputStreamReader(System.in));
System.out.println(keybd.readLine());
}
}
To use Java libraries, other than java.lang, you “import” them.
Java program always begins at a method called “main”