20155334 2016-2017-2 《Java程序设计》第三周学习总结

20155334 2016-2017-2 《Java程序设计》第三周学习总结

教材学习内容总结

第四章:

讲的是类类型,使用java撰写程序几乎都在使用对象(Object),要产生对象必须先定义类(Class),类是对象的设计图,对象是类的实例(Instance)。

定义类

class Clothes {//定义Clothes类
    String color;
    char size;
}

public class Field {
    public static void main(String[] args) {
        Clothes sun = new Clothes();//建立Clothes实例
        Clothes spring = new Clothes();//建立Clothes实例

        sun.color = "red";
        sun.size = 'S'; 
        spring.color = "green";//为个别对象的数据成员指定值
        spring.size = 'M';
        
        System.out.printf("sun (%s, %c)%n", sun.color, sun.size);//显示个别对象的数据成员值
        System.out.printf("spring (%s, %c)%n", spring.color, spring.size);
    }
}

在这个程序中定义了两个类,一个是公开publicField类,所以文档主文档必须是Field;另一个是非公开的Clothes

Tips:一个原始码中可以有多个类定义,但是只能有一个公开类,且文档的主文档名必须与公开类名称相同

要求JVM将sun绑定对象上的colorsize分别指定为"red"'S',同样的道理对于spring。最后分别显示sunspring的数据成员值。

class Clothes2 {
    String color;
    char size;
    Clothes2(String color, char size) {//定义构造函数
        this.color = color;//
        this.size = size;
    }
}
public class Field2 {
    public static void main(String[] args) {
        Clothes2 sun = new Clothes2("red", 'S');//使用指定构造函数建立对象
        Clothes2 spring = new Clothes2("green", 'M');
        
        System.out.printf("sun (%s, %c)%n", sun.color, sun.size);
        System.out.printf("spring (%s, %c)%n", spring.color, spring.size);
    }
}

构造函数
  • 在构造函数中,由于color参数与数据成员color同名,不可以直接写color=color,而是要使用this表示,将color参数的值指定给这个对象(this)color成员。
  • 构造函数特点:首先,函数名与类名相同,其次,不用定义返回值类型,另外不可以写return语句。
  • 构造函数作用是给对象进行初始化,多个构造函数是以重载的形式存在的。

标准类

Java SE提供了标准API,这些API是由许多类所组成的,可以直接取用这些标准类,省去撰写程序时重新打造基础的需求。主要参考书上就两个基本的标准类java.util.Scannerjava.math.BigDecimal进行了讲解:

  • java.util.Scanner:就以下面的程序为例
import java.util.Scanner;//告诉编译程序接下来想要偷懒
public class Guess {
    public static void main(String[] args) {
        Scanner console = new Scanner(System.in);//建立Scanner实例
        int number = (int) (Math.random() * 10);
        int guess;
        
        do {
            System.out.print("猜數字(0 ~ 9):");
            guess = console.nextInt();//取得下一个整数
        } while(guess != number);
        
        System.out.println("猜中了...XD");
    }
}


首先一开始使用import告诉编译程序,以后只要输入Scanner就可以了。在建立Scanner实例,必须传入java.io.InputStream的实例。
习惯上,包名称为Java开头的类,表示标准API提供的类。

  • java.math.BigDecimal就以下面的程序为例:
import java.math.BigDecimal;
public class DecimalDemo {
    public static void main(String[] args) {
        BigDecimal operand1 = new BigDecimal("1.0");
        BigDecimal operand2 = new BigDecimal("0.8");
        BigDecimal result = operand1.subtract(operand2);
        System.out.println(result);
    }
}

  • 简单来说,Java遵循IEEE 754浮点数运算规范,使用分数与指数表示浮点数。比如 0.75 会用 1/2+1/4来表示,这样的话0.1会用1/16+1/32+1/256+1/512+1/4096+···无限循环下去,无法精确,因而造成运算上的误差。
  • Tips:如果要求精确度,就得小心使用浮点数,并且别用==直接比较浮点数运算结果。
  • 创建BigDecimal的方法之一是使用字符串,BigDecimal在创建时会剖析传入字符串,以默认精度进行接下来的运算。
  • BigDecimal提供有plus()(加)、substract()(减)、multiply()(乘)、divide()(除),都会返回代表运算结果的BigDecimal

对象指定与相等性

  • 在基本类型中,=是将值复制给变量,而==是比较两个变量储存的值是否相同。但是,如果是在操作对象时,=是用在指定参考名称参考某个对象,而==是用在比较两个参考名称是否参考同一对象,换句话说也就是=是用在将某个名牌绑到某个对象,而==是用在比较两个名牌是否绑到同一对象。

  • equals是逻辑上相等,比如a.equals(b)意思就是判断a名牌绑定的对象与b名牌绑定的对象实际上内含值是否相同。

  • 因此equals()==是不一样的。

基本类型包装

  • 将基本类型打包在对象之中,将基本类型当作对象来操作
  • 打包器(Wrapper):LongIntegerDoubleFloatBooleanByte

例子:

public class IntegerDemo {
    public static void main(String[] args) {
        int data1 = 10;
        int data2 = 20;       
        Integer wrapper1 = new Integer(data1);//打包基本类型
        Integer wrapper2 = new Integer(data2);      
        System.out.println(data1 / 3);//基本类型运算
        System.out.println(wrapper1.doubleValue() / 3);//操作打包器方法
        System.out.println(wrapper1.compareTo(wrapper2));
    }
}

  • 自动装箱(Autoboxing):Integer wrapper = 10;编译程序自动判断是否能进行自动装箱int data1 = 10;Integer wrapper1 = new Integer(data1);可利用自动装箱改写成Integer data1 = 10;
  • 自动拆箱(Auto Unboxing)自动取出打包器中的基本形态信息Integer i = 10;//自动装箱 int foo = wrapper;//自动拆箱
  • 自动拆装箱是编译程序蜜糖,不能让变量参考至空对象又对之进行操作,也不能超过打包器的值域范围

数组对象

数组基础

数组基本上是用来收集数据,是具有索引的数据结构;要想一次取出数组中的每个值,方法之一是使用for循环:

public class Scores {
    public static void main(String[] args) {
        int[] scores = {88, 81, 74, 68, 78, 76, 77, 85, 95, 93};
        for(int i = 0; i < scores.length; i++) {
            System.out.printf("學生分數:%d %n", scores[i]);
        }
    }
}


一维数组使用一个索引存取数组元素,二维数组使用两个索引存取数组元素;要声明二维数组,就是在类型关键字旁加上[][]:

public class XY {
    public static void main(String[] args) {
        int[][] cords = {
                {1, 2, 3},
                {4, 5, 6}
        };//声明二维数组并赋予初始值
        for(int x = 0; x < cords.length; x++) {//按列扫描
            for(int y = 0; y < cords[x].length; y++) {//按行扫描
                System.out.printf("%2d", cords[x][y]);//指定行列索引得数组元素
            }
            System.out.println();
        }
    }
}

操作数组对象

int[] scores=new int[10];在Java中看到new,一定就是建立对象,每个索引会有默认值,如果默认值不符合需求,则可以使用java.util.Arraysfill()方法来设定新建数组元素值。例如:

import java.util.Arrays;

public class Scores2 {
    public static void main(String[] args) {
        int[] scores = new int[10];
        for(int score : scores) {
            System.out.printf("%2d", score);
        }
        System.out.println();
        Arrays.fill(scores, 60);
        for(int score : scores) {
            System.out.printf("%3d", score);
        }
    }
}


对于二维数组,没人规定二维数组一定得是矩阵,可以建立不规则数组。例如:

public class IrregularArray {
    public static void main(String[] args) {
        int[][] arr = new int[2][];
        arr[0] = new int[] {1, 2, 3, 4, 5};
        arr[1] = new int[] {1, 2, 3};
        for(int[] row : arr) {
            for(int value : row) {
                System.out.printf("%2d", value);
            }
            System.out.println();
        }
    }
}

第五章

讲的是对象封装

何谓封装
  • 封装实际上使用方法将类的数据隐藏起来,隐藏对象细节,将对象当作黑箱操作,用户不用再需要重复撰写对象初始化流程。
类语法细节
  • 三个权限修饰:publicprivateprotected
  • public权限修饰
  • 如果想在其他包的类程序中存取某包的类或对象成员,则该类或对象成员必须是公开成员,用public加以声明
  • 声明类为public,表示它是个公共类,可以在其他包的类中使用
  • 声明构造函数为public,表示其他包中的类可以直接调用这个构造函数
  • 声明方法为public,表示其他包的方法中可以直接调用这个方法
  • 定义类时,可以使用构造函数定义对象建立的初始流程
  • 在没有编写任何构造函数时,也可以Some some = new some();这样以无自变量方式调用函数
  • 重载构造函数:定义多个构造函数,只要参数类型或个数不同
  • 调用方法valueOf()根据传递的自变量类型不同,会调用对应的方法
class Some{
    void someMethod(int i){
        System.out.println("int 版本被调用");
    }
    void someMethod(Integer integer){
        System.out.println("Integer 版本被调用");
    }
}
public class OverloadBoxing {
    public static void main(String[] args){
        Some s = new Some();
        s.someMethod(1);
    }
}
  • import static解析顺序:1.局部变量覆盖2.成员覆盖3.重载方法比较
  • 内部类:在类中再定义类
  • 被声明为static的内部类,将外部类当作名称空间,Some.Other o = new Some.Other()
  • 被声明为static的内部类,虽然将外部类当作名称空间,但是算是独立类,只可以存取外部类static成员

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

遇到的问题都通过以下的资料进行解决:

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

代码照搬课本,没有遇到大问题。

代码托管

托管截图

代码量截图

上周考试错题总结

由于粗心大意导致出错了不少问题,同时老师在前面的博客中提到的问题进行了认真的反思。

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

学习进度条

代码行数(新增/累积) 博客量(新增/累积) 学习时间(新增/累积) 重要成长
目标 5000行 30篇 400小时
第一周 5/5 1/1 10/10
第二周 90/95 1/2 15/25
第三周 131/234 1/3 20/45

参考:软件工程软件的估计为什么这么难软件工程 估计方法

参考资料

原文地址:https://www.cnblogs.com/bestixx/p/6539010.html