第二周学习任务

20189230杨静怡 2018-2019-2 《移动平台开发实践》第2周学习总结

学习《Java和Android开发学习指南(第二版)》第4、7、10、11章——

第4章 对象和类
4.2 Java类
1.类决定了对象。
2.代码清单4.1 Employee类

class Employee{
int age;
double salary;
}

3.一个公有类的定义必须保存在一个文件中,这个文件名和类名相同,文件名必须以java为扩展名。
4.代码清单4.2 带有构造方法的Employee类

public class Employee{
  public int age;
  public double salary;
  public Employee(){
  }
    public Employee(int ageValue, double salaryValue){
      age=ageValue;
      salary=salaryValue;
    }
  }

4.7 封装和访问控制
4.7.1 类访问控制修饰符
1.一个Java源文件只能包含一个public类
2.代码清单4.5 Chapter类,具有默认的访问级别

package app04;
class Chapter{
  String title;
  int numberOfPages;

  public void review(){
    Page page =new Page();
    int sentenceCount=page.numberOfSentences;
    int pageNumber=page.getPageNumber();
  }
}

3.类成员的访问级别

4.8 this关键字
1.如果有一个类级别的字段,它和一个局部变量具有相同的名字:用this.field来应用,通常用来接收用于初始化字段的值。
2.代码清单4.7 Box类

package app04;
public class Box{
  int length;
  int width;
  int height;
  public Box(int length,int width,int height){
    this.length=length;
    this.width=width;
    this.height=height;
  }
}

4.9 使用其他的类
1.Java提供了关键字import,表示想要用一个包或者来自包中的一个类。比如,要在自己的代码中使用java.util.ArrayList,必须使用如下的import语句:

import java.util.ArrayList;

4.11 静态成员
1.从一个静态方法中不能调用非静态成员。
4.16 静态工厂方法
1.代码清单4.11 Discount类

package app04;
import java.time.LocalDate;
public class Discount{
  private int value;
  private Discount(int value){
    this.value=value;
  }
public int getValue(){
  return this.value;
}
public static Discount createSmallCustomerDiscount(){
  return new Discount(10);
}
public static Discount createBigCustomerDiscount(){
  return new Discount(12);
}
}

4.17 传值或传引用
1.代码清单4.12 ReferencePassingTest

package app04;
class Point{
  public int x;
  public int y;
}
public class ReferencePassingTest{
  public static void increment(int x){
    x++;
  }
  public static void reset(Point point){
    point.x=0;
    point.y=0;
  }
  public static void main(String[] args){
    int a=9;
    increment(a);
    System.out.println(a);
    Point p=new Point();
    p.x=400;
    p.y=600;
    reset(p);
    System.out.println(p.x);
  }
}

4.18 加载、连接和初始化
1.代码清单4.13 StaticCodeTest

package app04;
public class StaticInitializationTest{
  public static int a=5;
  public static int b=a*2;
  static{
    System.out.println("static");
    System.out.println(b);
  }
  public static void main (String[] args){
    System.out.println("main method");
  }
}

4.19 对象创建初始化
1.代码清单4.14 InitTest1类(实例初始化可以访问实例变量)

package app04;
public class InitTest1{
  int x=3;
  int y;

  {
    y=x*2;
    System.out.println(y);
  }
  static{
    System.out.println("Static initialization");
  }
  public static void main(String[] args){
    InitTest1 test=new InitTest1();
    InitTest1 moreTest=new InitTest1();
  }
}

2.代码清单4.16 InitTest3类(将初始化代码包装到一个方法中)

package app04;
public class InitTest3{
  int x=3;
  int y;

  public InitTest3(){
    init();
  }
  public InitTest3(int x){
    this.x=x;
    init();
  }
  private void init(){
    y=x*2;
    System.out.println(y);
  }
  static{
    System.out.println("Static initialization");
  }
  public static void main(String[] args){
    InitTest3 test=new InitTest3();
    InitTest3 moreTest=new InitTest3();
  }
}

学习总结——
第七章继承
7.1 概览
1.在Java中,一个类只能够扩展一个类。
2.代码清单7.3 Animal类及其子类

package app07;
class Animal{
  public float weight;
  public void eat(){
  }
}
class Bird extends Animal{
  public int numberOfWings=2;
  public void fly(){
  }
}
class Fish extends Animal{
  public int numberOfFins=2;
  public void swim(){
  }
}
class Dog extends Animal{
  public int numberOflegs=4;
  public void walk(){
    
  }
}

7.3 方法覆盖
1.代码清单7.5 Box类(@Override通常用来表示被覆盖的方法)

package app07;
public class Box{
  public int length;
  public int width;
  public int height;

  public Box(int length,int width,int height){
    this.length=length;
    this.width=width;
    this.height=height;
  }
  @Override
  public String toString(){
    return "I am a Box."
  }
  public Object clone(){
    return new Box(1,1,1);
  }
}

7.4 调用超类的构造方法
1.代码清单7.6 调用超类的无参构造方法

package app07;
class Base{
  public Base(){
    System.out.println(“Base”);
  }
  public Base(String s){
    System.out.println("Base."+s);
  }
}
public class Sub extends Base{
  public Sub(String s){
    System.out.println(s);
  }
  public static void main(String[] args){
    Sub sub=new Sub(“Start”);
  }
}

类名对应子类的名称。
7.5 调用超类的隐藏方法
1.代码清单7.8 使用super访问一个隐藏的成员

package app07;
class Tool{
  @Override
  public String toString(){
    return "Generic tool";
  }
}
public class Pencil extends Tool{
  @Override
  public String toString(){
    return"I am a Pencil";
  }
  public void write(){
    System.out.println(super.toString());
    System.out.println(toString());
  }
  public static void main(String[] args){
    Pencil pencil=new Pencil();
    pencil.write();
  }
}

第10章接口和抽象类
10.5 基类
1.TediousServlet类

package test;
import java.io.IOException;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class TediousServlet implements Servlet{
  @Override
  public void init (ServletConfig config)
         throws ServletException{
         }
  @Override
  public void service(ServletRequest request,
  ServletResponse response)
  throws ServletException,IOException{
    response.getWriter().print("Welcome");
  }
  @Override
  public void destroy(){
    
  }
  @Override
  public String getServletInfo(){
    return null;
  }
  @Override
  public ServletConfig getServletConfig(){
    return null;
  }
}

10.6 抽象类
1.代码清单10.6 DefaultPrinter的一个实现

package app10;
public abstract class DefaultPrinter{
  @Override
  public String toString(){
    return "Use this to print documents.";
  }
  public abstract void print(Object document);
}
public class MyPrinter extends DefaultPrinter{
  @Override
  public void print(Object document){
    System.out.println("Printing document");
  }
}

第11章多态
1.多态是一种OOP特性,它允许一个对象根据接收到的一个方法调用来确定要调用哪一个方法实现。
2.代码清单11.1 多态的一个示例

class Employee{
  public void work(){
    System.out.println("I am an employee.");
  }
}
class Manager extends Employee{
  public void work(){
    System.out.println("I am a manager.")
  }
  public void manage(){
    System.out.println"Managing ...");
  }
}
public class PolymorphismDemo1{
  public static void main(String[] args) {
    Employee employee;
    employee=new Manager();
    System.out.println(employee.getClass().getName());
    employee.work();
    Manager manager=(Manager) employee;
    manager.manage();
  }
}

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

  • 问题:Java三大特性,封装、继承、多态。对于稍微复杂的程序,类与类之间的调用关系就搞不清,尤其是父类与子类之间。
  • 问题解决方案:在网上查找相关资料,学习测试类的源代码
package com.test;

public class BaseClass {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Father f = new Father();
        f.sayHi();
        Son s = new Son();
        s.sayHi();
        s.sayHello();
        Father fs = new Son();
        fs.sayHi();
        //Son sf = (Son) new Father();
        //sf.aa();
        //sf.sayHi();
        System.out.println("---------------------------");
        System.out.println(f.getHeight());
        System.out.println(s.getHeight());
        System.out.println(fs.getHeight());
        System.out.println(fs.getClass());
    }

}

class Father {
    
    public int height;
    Father(){
        height = 5;
    }
    Father(int height){
        this.height= height;
    }
    
    public void sayHi(){
        System.out.println("Hi,World!I'm Father.");
    }
    
    public void aa(){
        System.out.println("Hi,aa!I'm Father.");
    }
    
    public int getHeight(){
        return height;
    }
}

class Son extends Father {

    public void sayHello(){
        System.out.println("Hello,World!I'm Son.");
    }
    
    @Override
    public void sayHi(){
        System.out.println("Hi,World!I'm Son.");
    }
}

输出为:

Hi,World!I'm Father.
Hi,World!I'm Son.
Hello,World!I'm Son.
Hi,World!I'm Son.
---------------------------
5
5
5
class com.test.Son

总结如下:
1.父类引用指向父类对象,子类引用指向子类对象,就是正常的类生成。
2.父类引用指向子类对象时,父类引用可以调用父类里定义的方法,比如sayHi();但是不能调用父类没用,子类有的方法,比如sayHello();会报The method sayHello() is undefined for the type Father错误。但是,父类引用指向子类对象时,调用的方法是子类的,也就是控制台输出的“Hi,World!I'm Son.”调用.getClass(),会打印"class com.test.Son"。
3.由于Son继承Father,所以所有的.getHeight();都是输出5.
4.子类对象指向父类引用的,需要强制转换成子类,见代码的注释地方,既可以调用父类方法,也可以调用子类方法,但是会报com.test.Father cannot be cast to com.test.Son异常。

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

  • 问题1:一开始不会使用Git
  • 问题1解决方案:(1)通过Ubuntu命令行上传项目到码云




    操作实例——
    创建docs目录和空文件“java_homework_week1.md”,并推送到码云上

    (2)通过Windows上的IDEA上传项目到码云
    先在IDEA中写好项目调试完毕没有问题之后,找到项目所在文件夹,右键Git Bash Here打开Git;依次输入:git init-git add src-git commit -m "commit message"-git remote add origin https://gitee.com/EvelynYang/20189230yangjingyi.git-git pull origin master --allow-unrelated-histories-git push -u origin master即可将项目上传至码云。
  • 问题2:不会提交脚本
  • 问题2解决方案:在IdeaProjects下打开终端-touch statistics.sh(创建脚本)-gedit statistics.sh(编辑脚本)- 输入如下内容:

    -赋予脚本可执行权限chmod +x statistics.sh -执行脚本 ./statistics.sh即可。

[代码托管]https://gitee.com/EvelynYang/20189230yangjingyi/commit/c5db6721a3c17bfb976cad2378225ae77661f652



上周考试错题总结

  • 5.如果只是要运行 Java 程序,下载程序哪个安装即可?
    A JDK
    B JRE
    C JavaDoc
    D Glassfish
    答案:B
  • 16.如果有个Console类别的原始码开头定义如下:
    package cc.openhome; public class Console {
    ...
    }
    如何在另一个类别中撰写import?
    A import cc.openhome.Console;
    B import cc.openhome;
    C import cc.openhome.*;
    D import Console;
    答案:AC,我只选了A

学习进度条

代码行数(新增/累积) 博客量(新增/累积) 学习时间(新增/累积) 重要成长
目标 5000行 30篇 400小时
第一周 200/200 2/2 20/20
第二周 300/500 2/3 18/38

参考资料

猜你喜欢

转载自www.cnblogs.com/EvelynYang/p/10498982.html