教材学习内容总结
- 第十章 多态性
- 后绑定:
- 多态性引用能够随时间变化指向不同类型的对象。
- 程序执行会产生一些请求事件,要求执行某段代码来完成一个方法调用,这种请求事件称为一个方法调用与一个方法定义的绑定。对于多态性引用,绑定要延迟到程序运行时才执行,这一被延迟的的请求事件称为后绑定
- 后绑定的效率要低于编译阶段的绑定效率,但灵活性更高。
- 由继承实现多态性:
- 一个引用变量可以指向有继承关系的任何类的任何对象。
- 实际调用的方法版本取决于对象的类型而不是引用变量的类型。
- 多态性的实质是,每个类都很清楚自己如何完成一个制定的行为。(SRP原则应该指的就是这个吧)多态机制允许用具有一致性但又独特的方式处理类似的对象。
- 利用接口实现多态性:
- 接口名可以用于声明对象引用变量
- 一个接口引用变量可以指向实现该接口的任何类的任何对象。当使用接口引用变量时,只能调用定义在接口中的方法。
- 方法的参数可以是多态性的,使得方法所接受的参数具有灵活性。
- 排序:
- 选择法排序:扫描整个数组找到最小值,将最小值与数列中第一个位置的值交换位置,循环下去就得到一个从小到大排列的有序列表。
- Compareble接口包含一个返回整型值的方法compareTo:如果i=0, 也表明对象x与y排位上是相等的如果返回数值i>0 则意代表x > y,反之若i<0则代表x < y。
- 插入法排序:不断地将一个新元素插入到已排好序的数列子集中,直到排序完成。
- 搜索:
- 线性搜索:
- 二分搜索:从有序数列的中点开始搜索。
教材学习中的问题和解决过程
问题1:
实际调用的方法版本取决于对象的类型而不是引用变量的类型
这句话是怎么理解的?
问题1解决方案:引用变量可以是多态性的,在不同的时间它可以指向不同的对象。引用变量指向什么对象时,就调用该对象的方法。
就引用老师给的例子,Holiday类有一个celebrate方法,Christmas类覆盖了此方法。
如果定义
Holiday day1 = new Holiday();
Christmas day2 = new Christmas();
day1.celebrate();
day2.celebrate();
那么day1和day2引用的celebrate方法分别是Holiday类和Christmas类的celebrate方法。
- 问题2:发现我连声明对象引用变量都不知道是什么意思。。。
问题2解决方案:根据老师的讲课
对象引用变量可以指向其声明的类的对象,还可以指向任何因继承而发生关系的类的对象

也就是说,如果已经确定了“Creature”属于Animal类,那么它可以指向Animal下的任何子类,既可能指向pig类,也可以指向cat类。
- 问题3:外层循环和内层循环?
问题3解决方案:外层循环相当于是记住数组里的一个数,而内层循环相当于是将这个数与数组里剩下里的数做比较,分出大小。
代码调试中的问题和解决过程

上周考试错题总结
- 错题1:Abstract methods are used when defining
A . interface classes
B . derived classes
C . classes that have no constructor
D . arrays
E . classes that have no
- 解析:在interface中所有的方法都是public abstract的,即使你没有申明它是public abstract的。
- 错题2:Which of the following is true regarding Java classes?
A . All classes must have 1 parent but may have any number of children (derived or extended) classes
B . All classes must have 1 child (derived or extended) class but may have any number of parent classes
C . All classes must have 1 parent class and may have a single child (derived or extended) class
D . All classes can have any number (0 or more) of parent classes and any number of children (derived or extended) classes
E . All classes can have either 0 or 1 parent class and any number of children (derived or extended) classes
- 解析:Java支持继承,但不是多重继承,所以Java类可以有任意数量的子节点,但只有一个父节点object(类的父类也可看成是object类)。 而且,由于所有Java类都直接或间接地从对象类继承,所以所有Java类都有一个双亲类。
- 错题3:A variable declared to be of one class can later reference an extended class of that class. This variable is known as
A . protected
B . derivable
C . cloneable
D . polymorphic (多态)
E . none of the above, a variable declared to be of one class can never reference any other type of class, even an extended class
- 解析:术语“多态性”(polymorphic)可以理解为“有许多形式”。在一般情况下,一个变量,一旦声明为某一类型,就永远不能改变为其他类型。唯一的例外是多态变量可以是任何类型的派生类(尽管不是在同一时间,变量可以从一种类型转换为另一种类型)。
- 错题4:In order to determine the type that a polymorphic variable refers to, the decision is made
A . by the programmer at the time the program is written
B . by the compiler at compile time
C . by the operating system when the program is loaded into memory
D . by the Java run-time environment at run time
E . by the user at run time
- 解析:多态变量可以有许多不同的类型,但是在程序执行之前,它不知道它采用了哪种类型。在这个变量被引用的时候,必须做出决定。这个决定是由运行时环境根据变量的最新赋值决定的。多态性引用能够随时间变化指向不同类型的对象
- 错题5:Using the reserved word, super, one can
A . access a parent class‘constructor
B . access a parent class‘methods and instance data
C . access a child class‘constructor
D . access a child class‘methods and instance data
E . none of the above
- 解析:保留字super提供了一种机制来访问父类的方法和实例数据(不管它们是否被隐藏),此外也可以用来来访问父类的构造器,所以这道题的A、B选项都是正确的。
- 错题6:Interface classes cannot be extended but classes that implement interfaces can be extended.
A . true
B . false
- 解析:任何类都可以被扩展,无论它是一个接口,还是实现接口的类,或者两者都不是但如果该类用“++final++”这个词进行修饰,在这种情况下,它不能被扩展。
- 错题7:If class AParentClass has a protected instance data x, and AChildClass is a derived class of AParentClass, then AChildClass can access x but can not redefine x to be a different type.
A . true
B . false
- 解析:派生类可以重新定义父类的任何实例数据或方法。父类的数据虽然是隐藏的,但是可以通过使用super来访问。
结对及互评
点评过的同学博客和代码
其他
这章的学习主要需要结合课本中提供的例子来学习,其中排序和搜索的概念应该多加复习,尽快掌握。
学习进度条
目标 |
5000行 |
30篇 |
400小时 |
|
第一周 |
117/117 |
1/1 |
19/19 |
|
第二周 |
289/406 |
1/2 |
21/40 |
|
第三周 |
403/809 |
2/4 |
22/62 |
|
第四周 |
1783/2592 |
1/5 |
35/97 |
|
第五周 |
770/3362 |
1/6 |
25/122 |
|
第六周 |
734/4096 |
1/7 |
25/147 |
|
第七周 |
687 / 4783 |
1/8 |
25/172 |
|
第八周 |
824/5607 |
2/10 |
30/202 |
|
参考资料
20172323 2017-2018-2 《程序设计与数据结构》第八周学习总结
原文:https://www.cnblogs.com/Lewandodoski/p/8969831.html