CS106A 编程方法学(五)

第五课

看到 Mehran Sahami 把变量 variable 比做盒子 box,真是让人开心,因为我也是这么想的。

一个变量有三个要素:名字 name,类型 type 和 值 value。

编程语言里面的关键字也不是 key word 而是 reserve word。

Java 中的原类型 primitive type

整型 int (integer),取值范围 minus 2 billion to 2 billion,整型一般用来说明:how many kind value counting

双精度浮点型 double,real-value number,用来说明:how much there is,no next value

这个关于整型和浮点型的解释挺有意思的,比如问,你有多少个孩子?回答应该是整型的;你的体重有多少?回答可以是带小数点的

give variables an initial value when it make sense it is also good software engineering.

在写 C# 程序的时候,遇到整型变量,我还是比较习惯使用默认值“0”;字符型的变量,有时候我会赋初始值 string.Empty。

type name = value;

variable = expression;

You can say, give me a variable that is a box, and the type that the box holds is an object that’s the type of some class, so all classes can actually be used as type means.

the object is receiver who’s receiving or being told to do something.

the name of the variable is the receiver and the method that’s being called is the message.

取余运算符 remainder operator  % : 7 % 2 = 1

posted on 2012-03-11 20:16  zhaorui  阅读(283)  评论(0编辑  收藏  举报

导航