20172329 2017-2018-2 《程序设计与数据结构》第八周学习总结

20172329 2017-2018-2 《程序设计与数据结构》第八周学习总结

教材学习内容总结

第十章多态性
一、后绑定
1、引用变量的类型和该引用变量指向的对象必须是兼容的,但不必完全相同;
2、多态性引用能够随时间变化指向不同类型的对象;
3、对于有多态性引用,后绑定要延迟到程序运行时才能执行;

4、后绑定的效率低于编译阶段的绑定效率。

二、由继承实现多态性
1、一个引用变量可以指向由继承关系的任何类的任何对象;
2、实际将调用的方法版本取决于对象的类型而不是引用变量的类型;

三、利用接口实现多态性
1、接口名可以用于声明对象引用变量;
2、一个接口引用变量可以指向实现该接口的任何类的任何对象;

四、排序
1、选择排序法;
2、插入排序法;
3、比较:选择排序法和插入法的效率相同,但选择法所执行的交换操作的次数更少。

五、搜索
1、线性搜索;
2、二分搜索;
3、比较:二分搜索比线性搜索的效率高,但二分搜索要求数据已经做过排序。

教材学习中的问题和解决过程

  • 问题1:在第一节所讲到的绑定中,说:“编译时绑定的效率比动态绑定的效率高”,为什么?
  • 问题1解决方案:
    首先,一开始我的想法就是说,因为编译时的绑定叫做静态绑定也叫做前期绑定,我就查找了相关的资料,其中,讲到静态绑定有这样一个说法:

这里讲解了静态方法在什么地方适用,并且也为我阐明了一个概念,因为静态绑定发生在代码执行之前,因此这种绑定就不会延迟程序的执行时间。

  • 问题2:动态绑定的范畴只有对象吗,可不可以是类的方法之类的?
  • 问题2解决方案:
    我们利用继承实现多态性做一个实验来讲:
public class Father {
    protected String name = "父亲属性";
}
  public class Son extends Father {
    protected String name = "儿子属性";
    public static void main(String[] args) {
        Father sample = new Son();
        System.out.println("调用的属性:" + sample.name);
    }
}

最后的结论为调用的成员为父亲的属性。

现在如果试图调用子类的成员变量name,该怎么做?

public class Father {
    protected String name = "父亲属性";

    public String getName() {
        return name;
    }
}  
public class Son extends Father {
    protected String name = "儿子属性";
    public String getName() {
        return name;
    }

    public static void main(String[] args) {
        Father sample = new Son();
        System.out.println("调用的属性:" + sample.getName());
    }
}

就是将将该成员变量封装成方法getter形式。

  • 问题3:重写和多态有什么关系?
  • 问题3解决方案:因为在看到一个问题的讨论上,看到了这样的问题:为什么要说有方法重写是多态的前提呢?其实一方面,看到这个问题的时候,自己意识到前提就是他们俩的一种关系,同时自己也想问这个问题,就继续往下看了:
    在那篇讨论里,很多大佬用飞机举例子,其中一个人这样讲:

但是这个是理解继承里的多态性,但是从整体来讲,还是没有特别明细的去讲出为什么?
另外一个人从多态的概念和作用之间来讲:

书上是这样说的:

当一个子类重载了其夫类的方法的定义时,实际上该方法的两个版本都存在。如果用一个多态性引用调用该方法,那么被调用方法的版本由执行方法调用的对象的类型确度,而不是由引用变量的类型确定.

代码调试中的问题和解决过程

  • 问题1:在pp10.1的练习当中,起初不理解到底如何去编,我就先编了一个接口,之后,将Staff里的void方法放到了接口里,在main驱动里学习书里的例子,用接口名声明一个对象,将其实例化之后,显示有错误?


  • 问题1解决方案:我ALT加回车想着将Staff声明为接口类,试一试,就可以了,但是好像不太符合题目要求,所以感觉自己还是错的,但是的确可以输出。

代码托管

上周考试错题总结

错题1
Inheritance through an extended (derived) class supports which of the following concepts?
A. interfaces
B. modular
C. information hiding
D. code reuse
E. correctness

    正确答案: D 我的答案: A 

解析:通过扩展一个类并继承它,新类不必重新实现任何这些继承的方法或实例数据,从而节省了程序员的工作量。因此,代码重用是为了您的需要扩展它而重用其他代码的好处。

错题2
Aside from permitting inheritance, the visibility modifier protected is also used to
A. permit access to the protected item by any class defined in the same package
B. permit access to the protected item by any static class
C. permit access to the protected item by any parent class
D. ensure that the class cannot throw a NullPointerException
E. define abstract elements of an interface

    正确答案: A 我的答案: B 

解析:受保护的可见性修饰符用于以受保护的方式控制对项目的访问。保护是访问限于当前类(如私人项目),同一包中的类或此类的扩展类。

错题3
Which of the following is an example of multiple inheritance?
A. A computer can be a mainframe or a PC
B. A PC can be a desktop or a laptop
C. A laptop is both a PC and a portable device
D. A portable device is a lightweight device
E. Macintosh and IBM PC are both types of PCs

    正确答案: C 我的答案: E 

解析:多重继承意味着一个给定的类继承了多个父类。在上面列出的那些中,笔记本电脑继承了来自PC和便携式设备的特性。A,B和E中的答案都是单个继承的例子,其中一个班级至少有两个孩子(在A中,计算机有儿童大型机和PC,B,PC有儿童台式机和笔记本电脑,E,PC有孩子Macintosh和IBM PC)。答案D表示一个类的属性。

错题4
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 or any number of children (derived or extended) classes

    正确答案: A 我的答案: E 

解析:Java支持继承,但不支持多重继承,所以Java类可以有任意数量的子元素,但只有一个父元素。此外,由于所有的Java类都直接或间接地从Object类继承,所有的Java类都只有一个父类。

错题5
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. closeable
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

    正确答案: D 我的答案: A 

解析:多态意味着变量可以有多种形式。在普通情况下,Java被强烈定义,即一旦声明为某个类型的变量永远不会变为不同的类型。这是一个例外,多态变量可以是任何类型的派生类(尽管不是同时,变量可以从一种类型变为另一种类型)。

错题6
Using the reserved word, super, one can
A. access a parent class ‘constructor(s)
B. access a parent class ‘methods and instance data
C. access a child class ‘constructor(s)
D. access a child class ‘methods and instance data
E. none of the above

    正确答案: E 我的答案: B 

解析:super保留字提供了访问父类的方法和实例数据(不管它们是否隐藏)的机制。另外,可以使用super访问父类的构造器。所以正确的答案是A和B的组合,这不是一个选项,所以正确答案是E.

错题7
Why shouldn't an abstract method be declared final?
A. There's nothing wrong with doing so
B. Abstract methods cannot be overridden and they must be if a concrete class ever is to be instantiated
C. So long as the Abstract method never actually is used in by any other method, there's no problem with doing this
D. As long as the Abstract method is declared in a Class (not an Interface), there's nothing wrong with doing this
E. None of the above

    正确答案: B 我的答案: E 

解析:为了使抽象方法变得具体,它必须被覆盖。声明一个方法是最终的,因此不可能覆盖它。这是一个矛盾,是被禁止的。

错题8
If class AParentClass has a protected instance data x, and AChildClass is a derived class of AParentClass, then AChildClass can access x but cannot redefine x to be a different type.
A. true
B. false

    正确答案: B 我的答案: A 

解析:派生类可以重新定义父类的任何实例数据或方法。父类的版本现在隐藏了,但是可以通过使用super来访问,就像在super.x中一样。

错题9
The reserved word, extends, is used to denote a has-a relationship.
A. true
B. false

    正确答案: B 我的答案: A 

解析:Extends用于继承...继承是一个is-a关系,而不是一个has-a关系。

错题10
Although classes can be inherited from one-another, even abstract classes, interfaces cannot be inherited.
A. true
B. false

    正确答案: B 我的答案: A 

解析:接口具有普通类所具有的所有继承属性。因此,您可以创建一个Interface继承层次结构,就像您可以创建一个Class继承层次结构一样。然而,你不能做的是实例化他们必须实现的接口。

结对及互评

  • 本周结对学习情况
  • 博客中值得学习的或问题:
    • 内容详略得当;
    • 代码调试环节比较详细;
  • 基于评分标准,我给本博客打分:4分。得分情况如下:
  1. 正确使用Markdown语法(加1分):

  2. 模板中的要素齐全(加1分)

  3. 教材学习中的问题和解决过程, 一个问题加1分

  4. 代码调试中的问题和解决过程, 一个问题加1分

  • 博客中值得学习的或问题:
    • 内容详略得当;
    • 代码调试环节比较详细;
  • 基于评分标准,我给本博客打分:4分。得分情况如下:
  1. 正确使用Markdown语法(加1分):
  2. 模板中的要素齐全(加1分)
  3. 教材学习中的问题和解决过程, 一个问题加1分
  4. 代码调试中的问题和解决过程, 一个问题加1分

其他

  • 这一周应该是快乐与痛苦相伴最契合的一周,在五一,总是在学习和休息之间划分好时间,但是有时候太想休息总是投入不到学习中,使得自己变得懒散,最后让自己玩也没玩好,学也没学好,所以我觉得合理支配时间会使我们自己过的更加充分。
  • 时间过得很快,眼看大半学期就要过去了,Java课程也变得有点困难,终究是自己打的基础不牢靠,总是学一点忘一点,再加上啦啦操训练占据了晚上大部分时间,和别人学习的时间差了很多,感觉自己最近都不是因为玩手机熬夜了,每天晚上都是敲到电脑没电,看着别人电脑还有电的无奈,只能看会儿书睡觉,现在班级里的竞争越来越强,希望大家都加油,身体为主,加油!

学习进度条

| | 代码行数(新增/累积)| 博客量(新增/累积)|学习时间(新增/累积)
| -------- | :----------------😐:----------------😐:---------------: |:-----:
| 目标 | 5000行 | 30篇 | 400小时
| 第一周 | 156/156 | 1/1 | 15/15
| 第二周 | 217/371 | 1/2 | 20/35
| 第三周 | 233/604 | 2/4 | 20/55
| 第四周 | 1382/1986 | 1/5 | 35/90
| 第五周 | 146/2196 | 1/6 | 25/115
| 第六周 | 462/2658 | 1/7 | 15/130
| 第七周 |856/3514 | 1/8 | 20/150
|第八周 | 1877/5391 | 3/11 |20/170

参考资料

Java程序设计
蓝墨云
Java静态绑定与动态绑定
方法重写是多态的必须条件么?

posted @ 2018-05-02 18:04  lalalaouye  阅读(354)  评论(2编辑  收藏  举报