2017年7月28日 星期五

CS106A - Programming Methodology - Lecture 9

One of the most important lectures!!

"Strings"
String is also a type like int, double...

String str = "hello there";

String name = "Bob";
int age = 20;
String s = "Name: " + name + "Age: " + age;




And the readLine method do like readInt:

readLine("?");

you can get the input from the users

"Writing our own class"
you can have other classes besides the main class and you can have multiple class

public class name [extends Superclass] {
    methods
    variables
}

Actually you can skip the [] part because there are some sort of a default setting for your java class and it'll automatically extends it. If you don't specify it and it will be OBJECTS.

Note that we have already known the methods could be private or public; and here if the methods are private, they are only able to be seen by other methods inside the same class, but if the method is public, it will able to be access by other methods in OTHER classes.
Also, most of the things in a class should be PRIVATE unless there's a good reason to be public.

start a new class

right click on the assignment or whatever folder and find new, find class
on the window pops out you only need to name it
note the message on the top: The use of the default package is discouraged.
the package is like packing some related classes together (yeah thanks for explaining)
but for now we don' really have to distinguish them, so the package will be default
of course to use a default package is just like saving files on your desktop it's discouraged, but again, for now it is just fine.

Sometimes I think maybe I could collect all of the jokes Mehran told and you know, sort of publish them here something.

"You will get a new class and guess what, eclipse has already created a file the name is exactly the same as your class name, and it wrote two lines of codes for you. just one of them is a closing brace."

counter program: automatically create sequential new numbers and give the numbers out

constructor method: a constructor does not return a value
you can have more than one constructor, and which one is going to be used is define by the parameters.

//這裡我完全搞不清楚 constructor 是什麼,不過感覺上是一個空的東西,但是可以用來定義這個 object 被使用的時候的 parameter,以及根據 parameter 去決定要用哪一個 constructor。寫法是像 method 一樣 public/private 開頭,但 so far 都是用public。在寫其他 method 的時候會加入 type,但 as mentioned 我們不會回傳一個值,所以 constructor 就不用定義 type 了。(應該吧

public class Incrementor {

    /* Set start value for Incrementor counter */
    public Incrementor (int startValue) {
        counter = startValue;
    }
    ...
}

Here are some tips (maybe?
Name of class is used as constructor name
Constructor does not specify return type
Responsible for initializing object
It is called when an object is created

//Hey here we go,所以 constructor 是用來 initialize 的!

you cannot assign a variable with the same name like:

public Incrementor (int counter) {
    counter = counter;
}

here the parameter counter is going to be assigned to ITSELF,  because in java world the program will check local or private variables first and if the name is not specified it will start to check the instance variables. 

BUT, you can actually force the program to know the difference:

public Incrementor (int counter) {
    this.counter = counter;
}

//真是淺顯易懂啊

this always refers to receiver object
Automatically available to methods of a class

BUT again this is an ugly syntax!! just use different names.

I should start to write comments now...

how to use our counter?

just like other objects (GRext, GOval...)

Incrementor count1 = new Incrementor();
Incrementor count2 = new Incrementor(1000);

according to the parameter to decide which constructor to use

"Objects are Called by Reference not Called by Value"

when you passing the object, you are really passing that object not like int or double that is just a copy

//應該是說,假設 methods 是一台一台號碼機,會運算顯示出結果;而 int or double 是像號碼機算完了印出一張紙條在 method 之間丟來丟去,塗塗改改,但是實際的值會像號碼機上面顯示一個號碼不會變動。當你算完了,離開這個 method 之後紙條就丟掉了 (scope),機器也會 reset 直到你下次給它另一個 parameter。而 calling object 的時候發生的事情,是你走到旁邊的另外一台機器去問問題,得到答案之後拿回來做事情,可是當你下一次再 call object 的時候你是走回去機器問,而不是去找剛剛那張紙條。

class variable: all the objects in the class shares the same box

//跟剛剛說的一樣,你去找 object 的時候是去找另一台獨立的機器,所以跟 assign 一個 local or instance variable 不同,你不是拿到紙條是去看機器上的數字。

"static" class variable

like when we assign the constants in class

//如果我們用了 static 也就是 class variable,會發生一件事情是他不是給紙條,而是每次都跑去看那台機器,輸入或輸出值。所以課堂上的 use counter 跑出來就會變成從 1000 開始數,因為你第二次去 call object 的時候不是拿紙條而是又跑去 reset 機器。



"Javadoc"

Javadoc starts with: /**
end with */

NOTE: you can have special tags (@param, @result, etc.)
and these tags allow the computer to automatically generate a documentation for human reading

student class example

All of your class should have a thing called "toString"
this return the information in this class in a texture way





"Instance variables and class variables"
(from handouts)

As with instance variables, class variables are defined as part of a class. However, in contrast to instance variables, with a class variable there is only one copy of the variable which is shared by all instances of the class.

Instance variables 是 class 的一部份,可以被 class 裡的所有 methods access;但是每個 method 都會有自己的一份 copy,所以當這些 methods 對 instance variables 做了什麼的時候(就像之前說的紙條,現在只有在紙條上改數字),並不會影響到其他的 methods。

class variables 因為是所有 methods 共同 share 的,所以並沒有哪個 method 可以對它做動作,也就是之前在定義常數的時候寫的 "private static int... ",static 表示它是 class variables。

另外關於 variables到底是 private 或是 public,這個和是哪一種 variables 沒有關係。如果是可以被其他人 access 的就是 public,反之則是 private。所謂的其他人比方說另一個 class,另一個 program 等等。