20172317 2017-2018-2 《程序设计与数据结构》第七周学习总结
Brief conclusion of this chapter
- Understood the concept of "Inheritance",for example:
- creating subclasses
- using the reversed word
extended
- REMEMBER!An instance of a chiild class dose not rely on an instance of the parent class
- Understood the usage of the super reference(maybe)
- by utilizing
super
,a subclass can grant access to a parent's members
- by utilizing
- In Java, a derived class can have only one parent's class
- this trait is known as single inheritence
- A child class can override the parent's defination of an inherited method
- however,a final method can't be overriden
- Understood the concept of object class(maybe)
Problems while studying the textbook and the solutions
- Problem 1:super?
Solutions of Problem 1:The textbook mentioned the
super
reference,stated that"one use of of thesuper
reference is to invoke a parent's constructor"For I can not quite understand what exactly the reserved wordsuper
can bring about, here I do a little test:
The tested object is LISTING 9.6 on the text book, which utilizedsuper(numPages)
.Here I first remove the codesuper(numPages)
,then I tried addpages = numPages
after I removedsuper(numPages)
.Under both circumstances I got this error
Familiar,isn't it?This is the error I often encountered when I was studying chapter 4 & 7.This error means there is supposed to be an integer in the brackets of the constructor
Book2(int numPages)
in Book2.java.Howerver there is no value in it now.
As for whysuper(numPages)
won't lead to such problems, I don't know either,so I just recourse to the internet.After some investigation I found two different ways ofsuper
to reference parent.According to the internet,super()
will invoke the constructor of a parent,whilesuper(parameter)
will invoke the constructor with the same formal parameters in a parent.To distinguishsuper()
fromsuper(parameter)
, I removedsuper(numPages)
withsuper()
in Dictionary2.java, trying to get some useful output.Turns out the result is the same error message like before[pic]Although some question still bother me, fortunately an example explaned everything,the following is the example:public class Person { public static void prt(String s) { System.out.println(s); } Person() { prt("父类·无参数构造方法: "+"A Person."); }//构造方法(1) Person(String name) { prt("父类·含一个参数的构造方法: "+"A person's name is " + name); }//构造方法(2) }
public class Chinese extends Person { Chinese() { super(); // 调用父类构造方法(1) prt("子类·调用父类”无参数构造方法“: "+"A chinese coder."); } Chinese(String name) { super(name);// 调用父类具有相同形参的构造方法(2) prt("子类·调用父类”含一个参数的构造方法“: "+"his name is " + name); } public static void main(String[] args) { Chinese cn = new Chinese(); cn = new Chinese("codersai"); } }
The first one is the code of parent class,the secound is the code of subclass.Actually I can't quite understand why there are statements such as
So with the informations above,here is my speculation:public static void prt(String s)
,or why the subclass include the instantiation statement. Anyway, the following picture is the output of these code.
super()
will invoke the constructor which never used any parameters in the parent class,whilesuper(parameter)
will invoke the constructor which has the same type of parameter in the parent class.
super
is also mentioned in overriding methods, for this I found an explanation on the internet:super
can be understood as a pointer that points towards it's parent's class.Therefore you can invoke members in the parent's class bysuper.xxx
- Problem 2:protected?
Solutions of problem 2:The reserved word
protected
was first encountered when I was searching data ofprivate
in the learning of chapter 4.But I....er.....didn't take it seriously........
So I come back to search those data.For the access modifierprotected
:"It's an access modifier betweenpublic
andprivate
.Any class that is modified withprotected
,can only be accessed by the method of it's own or the methods of it's subclass,even if the subclass is in a different package."Which meansprotected
modifier's encapsulation is not as good asprivate
but better thanpublic
.After replaceprotected
withpriavte
inprotected int pages
in the LISTING 9.5(Book2.java),an error message states that "variable pages has private access in Book2.java",which proved the theory,with the futher explanation of the usage of modifierprivate
,which I concluded while studying chapter 4.
Well....I found this on the internet.It explains the visibility of each modifier.It seems thatdefault
is the modifier yet to be learned.......
Problems while coding and the solutions
- 问题1:Hmm?Not finished the blog yet
- 问题1解决方案:XXXXXX
- 问题2:XXXXXX
- 问题2解决方案:XXXXXX
- ...
代码托管
(statistics.sh脚本的运行结果截图)
Conclusions of mistakes in the last week's test
3.The "off-by-one" error associated with arrays arises because
The correct answer is A,I chose B
A.the first array index is 0 and programmers may start at index 1, or may use a loop that goes one index too far
B.the last array index is at length + 1 and loops may only iterate to length, missing one
C.the last array element ends at length - 1 and loops may go one too far
D.programmers write a loop that goes from 0 to length - 1 whereas the array actually goes from 1 to length
E.none of the above, the "off-by-one" error has nothing to do with arrays
The explanation goes:"The array is initialized as = new type[x] where x is the size of the array. However, the array has legal indices of 0 to x - 1 and so, programmers are often off-by-one because programmers will write code to try to access indices 1 to x."
Well......this is the first time I encounter the concept "off-by-one",and explanation is still ambiguous as ever.So here is an example for better understanding:
Sometimes you intended to perform a loop n times and write something like this:for (int i =0; i < n; ++i) { ... }
.However,you may write something like this.....for (int i = 1; i < n; ++i) { ... }
or this......for (int i = 0; i <= n; ++i) { ... }
.The last two statement is so-called "off-by-one"8.Assume that BankAccount is a predefined class and that the declaration BankAccount[ ] firstEmpireBank; has already been performed. Then the following instruction reserves memory space for
The correct answer is B,I chose A
firstEmpireBank = new BankAccount[1000];
A.a reference variable to the memory that stores all 1000 BankAccount entries
B.1000 reference variables, each of which point to a single BankAccount entry
C.a single BankAccount entry
D.1000 BankAccount entries
E.1000 reference variables and 1000 BankAccount entries
The declaration BankAccount[ ] firstEmpireBank; reserves memory space for firstEmpireBank, which itself is a reference variable that points to the BankAccount[ ] object. The statement firstEmpireBank = new BankAccount[1000]; instantiates the BankAccount[ ] object to be 1000 BankAccount objects. This means that firstEmpireBank[0] and firstEmpireBank[1] and firstEmpireBank[999] are all now legal references, each of which is a reference variable since each references a BankAccount object. So, the statement reserves memory space for 1000 reference variables. Note that none of the 1000 BankAccount objects are yet instantiated, so no memory has been set aside yet for any of the actual BankAccount objects.9.If x is a char, and values is an int array, then values[x]
A.causes a syntax error
B.causes an Exception to be thrown
C.casts x as an int based on x's position in the alphabet (for instance, if x is 'a' then it uses 0 and if x is 'z' then it uses 25)
D.casts x as an int based on x's ASCII value (for instance, if x is 'a' then it uses 97 and if x is 'z' then it uses 122)
E.casts x as an int based on the digit that is stored in x (for instance, if x is '3' it uses 3) but throws an exception if x does not store a digitThe correct answer is D,and due to some unknown reasons, I chose E,even if I know that the correct answer is D.......
结对及互评
评分标准
- 正确使用Markdown语法(加1分):
- 不使用Markdown不加分
- 有语法错误的不加分(链接打不开,表格不对,列表不正确...)
- 排版混乱的不加分
- 模板中的要素齐全(加1分)
- 缺少“教材学习中的问题和解决过程”的不加分
- 缺少“代码调试中的问题和解决过程”的不加分
- 代码托管不能打开的不加分
- 缺少“结对及互评”的不能打开的不加分
- 缺少“上周考试错题总结”的不能加分
- 缺少“进度条”的不能加分
- 缺少“参考资料”的不能加分
教材学习中的问题和解决过程, 一个问题加1分
代码调试中的问题和解决过程, 一个问题加1分
- 本周有效代码超过300分行的(加2分)
- 一周提交次数少于20次的不加分
- 其他加分:
- 周五前发博客的加1分
- 感想,体会不假大空的加1分
- 排版精美的加一分
- 进度条中记录学习时间与改进情况的加1分
- 有动手写新代码的加1分
- 课后选择题有验证的加1分
- 代码Commit Message规范的加1分
- 错题学习深入的加1分
- 点评认真,能指出博客和代码中的问题的加1分
- 结对学习情况真实可信的加1分
- 扣分:
- 有抄袭的扣至0分
- 代码作弊的扣至0分
- 迟交作业的扣至0分
点评模板:
- 博客中值得学习的或问题:
- xxx
- xxx
- ...
- 代码中值得学习的或问题:
- xxx
- xxx
- ...
基于评分标准,我给本博客打分:XX分。得分情况如下:xxx
点评过的同学博客和代码
其他(感悟、思考等,可选)
xxx
xxx
学习进度条
代码行数(新增/累积) | 博客量(新增/累积) | 学习时间(新增/累积) | 重要成长 | |
---|---|---|---|---|
目标 | 5000行 | 30篇 | 400小时 | |
第一周 | 200/200 | 2/2 | 20/20 | |
第二周 | 300/500 | 2/4 | 18/38 | |
第三周 | 500/1000 | 3/7 | 22/60 | |
第四周 | 300/1300 | 2/9 | 30/90 |
尝试一下记录「计划学习时间」和「实际学习时间」,到期末看看能不能改进自己的计划能力。这个工作学习中很重要,也很有用。
耗时估计的公式:Y=X+X/N ,Y=X-X/N,训练次数多了,X、Y就接近了。
计划学习时间:XX小时
实际学习时间:XX小时
改进情况:
(有空多看看现代软件工程 课件
软件工程师能力自我评价表)