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

keyin classsame packageother package
publicAcAcAc
protectedAcAcAc in sub class
/AcAcFail
privateAcfailfail

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;
}

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

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

829945-20160328092318910-323100838.png

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

829945-20160328100308426-497224914.png

829945-20160328100324598-1323500366.png

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

829945-20160328100436644-1822055978.png

829945-20160328100508660-363197971.png

其他(感悟、思考等,可选)

学习进度条

代码行数(新增/累积)博客量(新增/累积)学习时间(新增/累积)重要成长
目标5000行30篇400小时
第一周50/502/28/8
第二周100/1502/48/16
第三周250/4002/610/26用git上传代码
第四周300/7002/812/38用wc查看代码行数

参考资料

转载于:https://www.cnblogs.com/Christen/p/5326928.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值