20145110 《Java程序设计》第四周学习总结

20145110 《Java程序设计》第四周学习总结

教材学习内容总结

6.1 何谓继承

6.1.1 继承共同行为

继承基本上就是避免多个类间重复定义共同行为。
继承的三个好处:减少代码冗余;维护变得简单;扩展变得容易。
构造方法不能被继承。

public class RPG1
{
    public static void main (String[] args)
    {
        demoSwordsMan();
        demoMagician();
    }
    static void demoSwordsMan()
    {
        SwordsMan swordsMan = new SwordsMan();
        swordsMan.setName("Justin");
        swordsMan.setLevel(1);
        swordsMan.setBlood(200);
        System.out.printf("剑士:(%s.%d,%d)%n",swordsMan.getName(),
                swordsMan.getLevel(),swordsMan.getBlood());
    }
    static void demoMagician()
    {
        Magician magician = new Magician();
        magician.setName("Moinca");
        magician.setLevel(1);
        magician.setBlood(100);
        System.out.printf("魔法师:(%s,%d,%d)%n",magician.getName(),
                magician.getLevel(),magician.getBlood());

    }
}
class SwordsMan extends Role
{
    public void fight()
    {
        System.out.println("挥剑攻击");
    }
}

class Magician extends Role
{
    public void fight()
    {
        System.out.println("魔法攻击");
    }

    public void cure()
    {
        System.out.println("魔法治疗");
    }
}

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()
    {
        return name;
    }
    public void setName(String name)
    {
        this.name = name;
    }

}

截图:

6.1.2 多态与is-a

is-a指的是类的父子继承关系,中文称为“是一种”关系。

多态:用抽象讲法解释,就是使用单一接口操作多种类型的对象。

public class RPG2
{
    public static void main(String[] args)
    {
        SwordsMan swordsMan = new SwordsMan();
        swordsMan.setName("Justin");
        swordsMan.setLevel(1);
        swordsMan.setBlood(200);

        Magician magician = new Magician();
        magician.setName("Monica");
        magician.setLevel(1);
        magician.setBlood(100);

        showBlood(swordsMan);
        showBlood(magician);

    }
    static void showBlood(Role role)
    {
        System.out.printf("%s 血量 %d%n",role.getName(),role.getBlood());
    }
}

截图:

用以上代码为例,在showBlood()中,既可以通过Role类型操作SwordsMan对象,也可以通过Role类型操作Magician对象。

6.1.3 重新定义行为

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()
    {
        return name;
    }
    public void setName(String name)
    {
        this.name = name;
    }



    public void fight()
    {

    }
}

6.2 继承语法细节

6.2.1 protected成员

被声明为protected的成员,相同包中的类可以直接存取,不同包中的类可以在继承后的子类直接存取。与我们熟知的关键字public之间来说,它的权限较小。

public abstract class Role5
{
    protected String name;
    protected int level;
    protected 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()
    {
        return name;
    }
    public void setName(String name)
    {
        this.name = name;
    }
}
public class Magician5 extends Role5
{
    public void fight()
    {
        System.out.println("魔法攻击");
    }
    public void cure()
    {
        System.out.println("魔法治疗");
    }
    public String toString()
    {
        return String.format("魔法师 (%s, %d, %d)", this.name, this.level, this.blood);
    }

}
public class SwordsMan5 extends Role5
{
    public void fight()
    {
        System.out.println("挥剑");
    }
    public String toString()
    {
        return String.format("剑士 (%s, %d %d)", this.name, this.level, this.blood);
    }

}

6.2.3 再看构造函数

如果类有继承关系,在创建子类实例后,会先进行父类定义的初始流程,再进行子类中的初始流程,也就是创建子类实例后,会先执行父类构造函数定义的流程,再执行子类构造函数定义的流程。

6.2.5 java.lang.Object

Java中所有对象一定是一种Object.在Java中,自来只能继承一个父类,如果在定义类的时候没有指定要继承哪一类,那么这个类就一定是继承。书中给了我们一个数组收集对象的程序,以下定义的ArrayList类可以不限长度的收集对象,程序如下:

import java.util.Scanner;
import static java.lang.System.out;

public class Guest {
    public static void main(String[] args) {
        ArrayList names = new ArrayList();
        collectNameTo(names);
        out.println("访客名单:");
        printUpperCase(names);
    }

    static void collectNameTo(ArrayList names) {
        Scanner console = new Scanner(System.in);
        while (true) {
            out.print("访客名称:");
            String name = console.nextLine();
            if (name.equals("quit")) {
                break;
            }
            names.add(name);
        }
    }

    static void printUpperCase(ArrayList names) {
        for (int i = 0; i < names.size(); i++) {
            String name = (String) names.get(i);
            out.println(name.toUpperCase());
        }
    }
}
import java.util.Arrays;

public class ArrayList {
    private Object[] list;
    private int next;

    public ArrayList(int capacity) {
        list=new Object[capacity];
    }

    public ArrayList() {
        this(16);
    }

    public void add(Object o) {
        if(next==list.length) {
            list=Arrays.copyOf(list, list.length*2);
        }
        list[next++]=o;
    }
    public Object get(int index) {
        return list[index];
    }

    public int size() {
        return next;
    }
}

6.2.7

public abstract class GuessGame {
    public void go() {
        int number=(int)(Math.random()*10);
        int guess;
        do {
            print("输入数字:");
            guess=nextInt();
        }while(guess!=number);
        println("猜中了");
    }

    public void println(String text) {
        print(text+"\n");
    }

    public abstract void print(String text);
    public abstract int nextInt();
}
import java.util.Scanner;

public class ConsoleGame extends GuessGame {
    private Scanner scanner=new Scanner(System.in);

    @Override
    public void print(String text) {
        System.out.print(text);
    }

    @Override
    public void println(String text) {
        System.out.println(text);
    }


    @Override
    public int nextInt() {
        return scanner.nextInt();
    }
}
public class Guess {
    public static void main(String[] args){
        GuessGame game=new ConsoleGame();
        game.go();

    }
}

7.1 何谓接口

7.1.1

类要操作接口,必须使用implements关键字。操作某接口时,对接口中定义的方法有两种处理方式,一是操作接口中定义的方法,二是再度将该方法标示为abstract。
继承会有“是一种”关系,接口操作则表示“拥有行为”,但不会有“是一种”关系。

public abstract class Fish implements Swimmer {
    protected String name;
    public Fish(String name){
        this.name = name;
    }
    public String getName()
    {
        return name;
    }
    @Override
    public abstract void swim();
}
public class Human implements Swimmer {
    private String name;
    public Human(String name){
        this.name=name;
    }
    public String getName()
    {
        return name;
    }


    @Override
    public void swim()
    {
        System.out.printf("人类 %s 游泳 %n",name);
    }
}

7.1.2 行为的多态

public class Ocean{
        public static void main(String[] args)
        {
            doSwim(new Human("贾斯汀"));
            doSwim(new Submarine("黄色一号"));
        }
    
        static void doSwim(Swimmer swimmer){
            swimmer.swim();
        }
    }

7.2 接口语法细节

public interface Action{
    public static final int STOP=0;
    public static final int RIGHT=1;
    public static final int LEFT=2;
    public static final int UP=3;
    public static final int DOWN=4;
}
import static java.lang.System.out;
public class Game{
    public static void main(String[] args){
        play(Action.RIGHT);
        play(Action.UP);
    }
    public static void play(int action){
        switch(action){
            case Action.STOP:
                out.println("bofangtingzhidonghua");
                break;
            case Action.RIGHT:
                out.println("bofangxiangyoudonghua");
                break;
            case Action.LEFT:
                out.println("bofangxiangzuodonghua");
                break;
            case Action.UP:
                out.println("bofangxiangshangdonghua");
                break;
            case Action.DOWN:
                out.println("bofangxiangxiadonghua");
                break;
            default:
                out.println("buzhichicidongzuo");
        }
    }
}

import static java.lang.System.out;
public class Game{
    public static void main(String[] args){
        play(Action.RIGHT);
        play(Action.UP);
    }
    public static void play(int action){
        switch(action){
            case Action.STOP:
                out.println("播放停止动画");
                break;
            case Action.RIGHT:
                out.println("播放向右动画");
                break;
            case Action.LEFT:
                out.println("播放向左动画");
                break;
            case Action.UP:
                out.println("播放向上动画");
                break;
            case Action.DOWN:
                out.println("播放向下动画");
                break;
            default:
                out.println("不支持此动作");
        }
    }
}

7.2.3 使用enum枚举常数

public enum Action2{
    STOP,RIGHT,LEFT,UP,DOWN
}
import static java.lang.System.out;
public class Game2{
    public static void main(String[] args){
        play(Action.RIGHT);
        play(Action.UP);
    }
    public static void play(int action){
        switch(action){
            case Action.STOP:
                out.println("播放停止动画");
                break;
            case Action.RIGHT:
                out.println("播放向右动画");
                break;
            case Action.LEFT:
                out.println("播放向左动画");
                break;
            case Action.UP:
                out.println("播放向上动画");
                break;
            case Action.DOWN:
                out.println("播放向下动画");
                break;
            default:
                out.println("不支持此动作");
        }
    }
}

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

这周的学习难度相当大。在测试代码的过程中也暴露出自己之前学习的不足,对class、public之类定义没有深刻的认识。在前几周,书上的代码跑出来了,自己也就截图让它过去了,没有去体会每一段代码的作用。当需要综合使用之前的知识时,就捉襟见肘了,连书上的代码都无法跑出来,耗费了大量的时间重新回头去温习前几周的内容。这周几组代码组合使用能够极大的提升我们对java代码构造的认识。

本周代码托管截图

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

这一周的学习让我认识到了很多。敲完书上的代码跑出来,自己就是真的会了吗?如果重新去看这段代码,自己能理解每一段的含义吗?能说出每一段的功能吗?能脱离书本自己再重新还原一遍这段代码吗?每一个问号我都不敢给出肯定的答案,自己前几周的学习浮于表面,看似很累,但其实收获也有限。如何在消耗大量时间的同时能够获得等同的学习效果是我在接下去的学习中需要思考的问题,java这门语言真的需要投入大量时间才能入门。选择这门课的初心就是能够应用,自己离这个目标还有多远,分配下来每周还需要付出哪些努力,都是要反省的事情。

学习进度条

代码行数(新增/累积) 博客量(新增/累积) 学习时间(新增/累积) 重要成长
目标 5000行 30篇 400小时
第一周 200/200 2/2 20/20
第二周 300/500 1/3 18/38
第三周 400/1000 1/4 25/60
第四周 538/1300 1/5 30/90

参考资料

posted on 2016-03-27 19:52  20145110  阅读(151)  评论(5编辑  收藏  举报