【自用】CS61b的一些笔记
Instance Variables and Object Instantiation
- An
Object
in Java is an instance of any class. - The
Dog
class has its own variables, also known as instance variables or non-static variables. Unlike languages like Python or Matlab, these must be declared inside the class, where new variables can be added at runtime. - The method that we created in the
Dog
class did not have thestatic
keyword. We call such methods instance methods or non-static methods. - To call the
makeNoise
method, we had to first instantiate aDog
using thenew
keyword, and then make a specificDog
bark. In other words, we calledd.makeNoise()
instead ofDog.makeNoise()
. - Once an object has been instantiated, it can be assigned to a declared variable of the appropriate type, e.g.
d = new Dog();
- Variables and methods of a class are also called members of a class.
- Members of a class are accessed using dot notation.
public class Dog {
public int weightInPounds;
public void makeNoise() {
if (weightInPounds < 10) {
System.out.println("yipyipyip!");
} else if (weightInPounds < 30) {
System.out.println("bark. bark.");
} else {
System.out.println("woof!");
}
}
}
Constructors in Java
When the instantiation is parameterized, saving us the time and messiness of manually typing out potentially many instance variable assignments
public class Dog {
public int weightInPounds;
public Dog(int w) {
weightInPounds = w;
}
public void makeNoise() {
if (weightInPounds < 10) {
System.out.println("yipyipyip!");
} else if (weightInPounds < 30) {
System.out.println("bark. bark.");
} else {
System.out.println("woof!");
}
}
}
public class DogLauncher {
public static void main(String[] args) {
Dog d = new Dog(20);
d.makeNoise();
}
}
Class Methods vs. Instance Methods
Java allows us to define two types of methods:
- Class methods, a.k.a. static methods.
- Instance methods, a.k.a. non-static methods.
Instance methods are actions that can be taken only by a specific instance of a class.
Static methods are actions that are taken by the class itself.
Both are useful in different circumstances. As an example of a static method, the Math
class provides an sqrt
method. Because it is static, we can call it as follows:
x = Math.sqrt(100);
If sqrt
had been an instance method, we would have instead of the awkward syntax below. Luckily sqrt
is a static method so we don’t have to do this in real programs.
Math m = new Math();
x = m.sqrt(100);
Static Variables
It is occasionally useful for classes to have static variables. These are properties inherent to the class itself, rather than the instance. For example, we might record that the scientific name (or binomen) for Dogs is “Canis familiaris”:
public class Dog {
public int weightInPounds;
public static String binomen = "Canis familiaris";
...
}
Static variables should be accessed using the name of the class rather than a specific instance, e.g. you should use Dog.binomen
, not d.binomen
.
While Java technology allows you to access a static variable using an instance name, it is bad style, confusing, and in my opinion an error by the Java designers.
primitive types vs reference type
- 8 primitive types in java: byte short int long float double boolean char, everything else, including arrays, is a reference type.
- primitive types的
a = b
给a开辟了新memory,改变a不改变b;而reference type的a = b
是另指针a指向b的内存,改变了a等于改变了b - primitive types传递给函数形参,reference type传递给函数指针
- primitive types的
Terminology Summary
int i=Integer.parseInt("200"); //类型转化
String[] a = {"cat", "dog", "laser horse", "ketchup"}; for (String s : a) {...} //间接版for循环
A.equals(B);//比较reference type 是否相同
-
对于每次通过主循环,
update
在计算所有力并安全存储在xForces
和之前不要调用yForces
。例如,planets[0].update()
在整个xForces
和yForces
数组完成之前不要调用!差异很微妙,但如果您planets[0].update
在计算xForces[1]
和之前调用自动分级器会感到不安yForces[1]
-
p+tap =
System.out.print();
-
类 可以嵌套 类 (设内部的类为B,外部的类为A),如果B不被除了A之外成员访问,可以将B设为
private
,如果B不访问A中的成员,可以将B设为static
。一旦B被设为private
,B中成员的修饰词(public or private)就无关紧要了。 -
例1传递给参数pokemon的指针,可以改变指针所指的内容
poke.level
,但不会改变指针所指poke = new pokemon()
-
例1深刻理解
static variable
;例2Horse same
这个变量只在if里有效,出了if的大括号所有的same
都是类的成员same
,而不是一个名叫same的Horse的实例;例4中虽然数组传递的是指针,但for(int x : A)
x也只是在for中有效的形参,不能改变数组的值,用for(int i = 0; i<A.length; i++)
就能改变数组的值
-
overloaded and have the same signature is overridden
-
[exercise](*examprep04sol.pdf (datastructur.es))
-
wrapper types and primitives can be used almost interchangeably. But Arrays are never autoboxed/unboxed, e.g. and Integer[] can not be used in place of an int[] (or vice versa).
-
interface vs abstract class: Unlike interfaces, abstract classes can provide implementation inheritance for features other than public methods, including instance variables. In interface, methods are abstract(without braces, and followed by a semicolon), unless it has keywords
default
. However, in abstract class, abstract methods should be followed by keywordsabstract
.- think of an interface as defining a “can-do” or an “is-a” relationship, whereas an abstract class should be a stricter “is-a” relationship. The difference can be subtle, and can often use one instead of the other.