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

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

教材学习内容总结

第十章多态性
1、后绑定
一、引用变量的类型和该引用变量指向的对象必须是兼容的,但没必要彻底相同;
二、多态性引用可以随时间变化指向不一样类型的对象;
三、对于有多态性引用,后绑定要延迟到程序运行时才能执行;php

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

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

3、利用接口实现多态性
一、接口名能够用于声明对象引用变量;
二、一个接口引用变量能够指向实现该接口的任何类的任何对象;程序员

4、排序
一、选择排序法;
二、插入排序法;
三、比较:选择排序法和插入法的效率相同,但选择法所执行的交换操做的次数更少。web

5、搜索
一、线性搜索;
二、二分搜索;
三、比较:二分搜索比线性搜索的效率高,但二分搜索要求数据已经作过排序。数据结构

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

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

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

  • 问题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,该怎么作?this

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形式。spa

  • 问题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静态绑定与动态绑定
方法重写是多态的必须条件么?

相关文章
相关标签/搜索