20145109 《Java程序设计》第四周学习总结
20145109 《Java程序设计》第四周学习总结
教材学习内容总结
Chapter 6 Inheritance & Polymorphism
What is Inheritance?
Basically, ingeritance aims to avoid common activity among several classes. This contributes to maintaining codes more easily.
Example:
public class Role {
private String name;
private int level;
private int blood;
public int getBlood() {
return blood;
}
public void setBlood(int blood) {
this.blood = blood;
}
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
}
public String getName() {
this.name = name;
}
public void setName(String name) {
this.name = name;
}
}
public class SwordsMan extends Role {
public void fight() {
System.out.println("挥剑攻击");
}
}
public class Magician extends Role {
public void fight() {
System.out.println("魔法攻击");
}
public void cure() {
System.out.println("魔法治疗");
}
}
notice :
private members can be inherited, however, sub class can't get directly, but can get from public methods provided by parent class.
Polymorphic & is-a
SwordsMan inherits Role, so SwordsMan is a Role.
The following passes in compile:
Role role1 = new SwordsMan();
Role role2 = new Magician();
The following fails in compile:
SwordsMan swordsMan = new Role();
Magician magician = new Role();
You can also do this:
Role role1 = new SwordsMan();
SwordsMan swordsMan = (SwrdsMan) role1;
It is called 'Cast'.
Polymorphic, to explain abstractly, is to operate different types of variables by single interface(operating method).
Override
public class Role {
...
public void fight() {
}
}
We define fight() in Role with nothing. When SwordsMan inherits Role, fight() is overrided.
public class SwordsMan extends Role {
public void fight() {
System.out.println("挥剑攻击");
}
}
Now we can write like this:
public class RPG {
public static void main(String[] args) {
drawFight(swordsMan);
drawFight(magician);
}
static void drawFight(Role role) {
System.out.println(role.getName());
role.fight();
}
}
When overriding some method in parent class, sub class must have the exactly same name.
After JDK5, annotation is supported. If marking '@Override' before some method in sub class, compile program will check it out.
Abstract Method & Class
If there's no operation in some method block, we can use 'abstract' to mark, replacing '{}' with ';'
public abstract class Role {
...
public abstract void fight();
}
The class containing abstract method must be an abstract class.
An abstract class can't generate instance.
If sub class inherits an abstract class, there're two choices of abstract method. One, continuing to mark the subclass as 'abstract'. Two, operating abstract method.
Details of Polymorphic
protected
'protected' can be accessed from this class and its sub class.
public abstract class Role {
protected String name;
protected int level;
protected int blood;
}
public class SwordsMan extends Role {
public String toString() {
return String.format("骑士(%s, %d, %d)", this.name, this.level, this.blood)
}
}
public class Magician extends Role {
public String toString() {
return String.format("魔法师 (%s, %d, %d)", this.name, this.level, this.blood);
}
}
Summary on jurisdiction key words
| key | in class | same package | other package |
|---|---|---|---|
| public | Ac | Ac | Ac |
| protected | Ac | Ac | Ac in sub class |
| / | Ac | Ac | Fail |
| private | Ac | fail | fail |
If having no idea about the key set, use private. If needed, open the jurisdiction afterwards.
When we want to use methods in parent class, 'super' is useful.
public abstract class Role {
protected String name;
protected int level;
protected int blood;
public String toString() {
return String.format("(%s, %d, %d)", this.name, this.level, this.blood);
}
}
public class Magician extends Role {
@Override
public String toString() {
return "魔法师" + super.toString();
}
}
public class SwordsMan extends Role {
@Override
public String toString() {
return "剑士" + super.toString();
}
}
notice
When 'super' parent class method, the jurisdiction can't be minimized. (maximize is permitted)
constructor
When 'new' an instance of sub class, first it will do parent constructor in the first line like 'super(parameter...)'. If hasn't written, default adding 'super()'.
final
'final' object: can't be alterred. Constructor needs notice.
'final' class: forbid sub class.
'final' method: the last time to define method, sub class can't override final method.
java.lang.Object
The superest class is java.lang.Object
import java.util.Arrays;
import java.util.Objects;
public class ArrayList {
private Objects[] list;
private int next;
public ArrayList(int capacity) {
list = new Objects[capacity];
}
public ArrayList() {
this(16); //初始容量默认为16
}
public void add(Objects o) {
if (next == list.length) {
list = Arrays.copyOf(list, list.length * 2);
}
list[next++] = o;
}
public Objects get(int index) {
return list[index];
}
public int size() {
return next;
}
}
override toString()
toString() is defined in Object. We override it in SwordsMan. Many methods referred to object will execute toString() by default, such as "System.out.print"
System.out.println(swordsMan.toString())
equals
System.out.println(swordsMan);
override equals()
instanceof : judge if the instance is created by some class.
if (!(other instanceof Cat)) {...}
Chapter 7 Interface & Polymorphism
What is Interface
To create a project, all objects can swim. Take care, it is an action not belonging to some class, but to all. In Java, we can define with 'interface'.
public interface Swimmer {
public abstract void swim();
}
Interface is used to define action not operation. Here, the action swim() has no operation, and is marked with 'abstract'.
Details of interface
default
**1. ** If there's nothing to do in the interface method, it must be "public abstract".
public interface Swimmer {
void swim();
}
When compiling, "public abstract" will be added.
public interface Swimmer {
public abstract void swim();
}
**2. ** To define constant in interface, it must be "public static final".
public interface Action {
public static final int STOP = 0;
public static final int RIGHT = 0;
public static final int LEFT = 0;
}
If enumerating constant in interface, you have to use "=" to appoint value, or compile fails. The following is right.
public interfae Action {
int STOP = 0;
}
教材学习中的问题和解决过程
代码调试中的问题和解决过程

这里的“+”是中文输入法的“+”,还是疏忽了。转换为英文的就解决了。


原因是Object都打成了Objects(自动补全惹的祸),晕啊。


其他(感悟、思考等,可选)
学习进度条
| 代码行数(新增/累积) | 博客量(新增/累积) | 学习时间(新增/累积) | 重要成长 | |
|---|---|---|---|---|
| 目标 | 5000行 | 30篇 | 400小时 | |
| 第一周 | 50/50 | 2/2 | 8/8 | |
| 第二周 | 100/150 | 2/4 | 8/16 | |
| 第三周 | 250/400 | 2/6 | 10/26 | 用git上传代码 |
| 第四周 | 300/700 | 2/8 | 12/38 | 用wc查看代码行数 |
参考资料
20145109 《Java程序设计》第四周学习总结的更多相关文章
- Java程序设计第四周学习总结
1. 本周学习总结 1.1 尝试使用思维导图总结有关继承的知识点. 1.2 使用常规方法总结其他上课内容. 答:①String类是final类,不能定义String的子类. ② instanceof ...
- 杨其菊201771010134《面向对象程序设计(java)》第四周学习总结
<面向对象程序设计(java)> 第四周学习总结 第一部分:理论知识 1.类与对象 a.类(class)是构造对象的模板或蓝图.由类构造对象的过程称为创建类的实例: java中类声明的格式 ...
- 对于“2017面向对象程序设计(JAVA)第四周学习总结”存在问题的反馈
对于“2017面向对象程序设计(JAVA)第四周学习总结”存在问题的反馈 “这部分同学博文总结没有写,实验作业没有提交.”——1.关于博文作业.实验作业教学功能的正解:学习知识.暴露问题.衔接课上.2 ...
- 201871010106-丁宣元 《面向对象程序设计(java)》第四周学习总结
201871010106-丁宣元 <面向对象程序设计(java)>第四周学习总结 正文开头 项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-da ...
- 20155318 2016-2017-2 《Java程序设计》第九学习总结
20155318 2016-2017-2 <Java程序设计>第九学习总结 教材学习内容总结 学习目标 了解JDBC架构 掌握JDBC架构 掌握反射与ClassLoader 了解自定义泛型 ...
- 《Java程序设计》第二学习总结
<Java程序设计>第二学习总结 教材学习内容总结 类型 byte(字节) shot(短整型) int(整型) long(长整型) float(浮点型) double(双精度) char( ...
- 20145304 刘钦令 Java程序设计第二周学习总结
20145304 <Java程序设计>第2周学习总结 教材学习内容总结 java可区分基本类型和类类型(即参考类型)两大类型系统. 基本类型主要可区分为整数.字节.浮点数.字符与布尔. 整 ...
- 20145304 刘钦令 Java程序设计第一周学习总结
20145304<Java程序设计>第1周学习总结 教材学习内容总结 1995年5月23日,是公认的Java的诞生日,Java正式由Oak改名为Java. Java的三大平台是:Java ...
- 20145120黄玄曦 《java程序设计》 寒假学习总结
1和2.我对未来规划不多,我认为好好学习积累知识能帮助我应对未来的挑战,这是我的学习动力之一,此外,了解新知识满足好奇心也是我的主要的学习动力. 3.我认为专业课学习比公务员考试重要,我认为专业知识是 ...
- 201621123007 Java程序设计第一周 学习总结
第一周-Java基本概念 201621123007 <Java程序设计> 第一周学习总结 1. 本周学习总结 java是面向对象的一类语言,三大特征:封装性,继承性,多态性. jdk jr ...
随机推荐
- Android版微信小代码(转)
以下代码仅适用于Android版微信: //switchtabpos:让微信tab更贴合Android Design 如果你并不喜欢微信Android版和iOS端同用一套UI,现在有一个小方法可以实现 ...
- 使用JMAP dump及分析dump文件
查看整个JVM内存状态 jmap -heap [pid]要注意的是在使用CMS GC 情况下,jmap -heap的执行有可能会导致JAVA 进程挂起 查看JVM堆中对象详细占用情况jmap -his ...
- Eclipse UML插件
Green UML http://green.sourceforge.net/ AmaterasUML http://amateras.sourceforge.jp/cgi-bin/fswiki_en ...
- HDU3533(Escape)
不愧是kuangbin的搜索进阶,这题没灵感写起来好心酸 思路是预处理所有炮台射出的子弹,以此构造一个三维图(其中一维是时间) 预处理过程就相当于在图中增加了很多不可到达的墙,然后就是一个简单的bfs ...
- 160622、详解JavaScript变量提升
变量在程序中随处可见.它们是一些始终在相互影响,相互作用的的数据和逻辑.正是这些互动使应用程序活了起来. 在JavaScript中使用变量很重要的一方面就是变量的提升 —— 它决定了一个变量何时可以被 ...
- 巨蟒python全栈开发flask3
首先,我们新建一个项目: 这个时候,我们调用ab函数,可以在所有的模板中使用. 上边是一个特殊装饰器, 1.flask特殊装饰器 下面说几个特殊的装饰器 再请求之前的装饰器 运行: 这个时候,服务端打 ...
- vbs 修改Administrator帐号密码
Dim WshShell, oExec Set wshShell = CreateObject("WScript.Shell") Set objFSO = CreateObject ...
- :nohlsearch
vim 编辑器 ——黄色阴影的消除问题 - leikun153的博客 - CSDN博客 https://blog.csdn.net/leikun153/article/details/78903597 ...
- Android系统移植与调试之------->如何添加一个adb wifi无线调试的功能【开发者选项】-【Wifi调试】
首先弄懂怎么设置adb wifi无线调试的功能,如下所示. 1. 手机端开启adb tcp连接端口 :/$setprop service.adb.tcp.port :/$stop adbd :/$st ...
- IOS研究之IOS7四种后台机制
IOS 7中,实际上APP拥有四种后台模式.不管是哪一种后台机制,均须要利用苹果给予的对应后台接口实现.IOS7系统中,开发人员能够灵活利用多种后台接口(API)实现更加智能的应用操作. 对获取 ...