20145316许心远《Java程序设计》第3周学习总结
教材学习内容总结
一、定义类:
- 类定义时使用class关键字
- 如果要将x绑定到新建的对象上,可以使用“=”制定,称为x名称参考至新建对象。
- 一个原始码中可以有多个类定义,但只能有一个是公开类,且文档中的主文档名必须与公开类名称相同。
二、使用标准类:
- java.util.Scanner
为了免去每次输入时都要写java.util.Scanner,可以一开始就是用import,在建立Scanner实例时必须传入java.io.InputStream的实例。 - java.math.BigDeciaml
有的时候浮点数与浮点数相加并使用==比较运算结果时会出错。
原因:java遵守IEEE754浮点数运算规范,使用分数与指数来表示浮点数,有的小数要用分数表示就无限循环下去,没法精确表达造成误差。
解决方案:创建BigDecimal,他会剖析传入字符串,以默认精度进行接下来的运算。提供plus()、substract()、multiply()、divide()等进行加减乘除的运算。
三、对象制定与相等性
- = 是用在参考名称参考某个对象,而==是用在比较两个参考名称是都参考同一对象。
四、基本类型打包器
- 要使基本类型如同对象一样操作,可以使用Long、Integer、Double、Float、Boolean、Byte等类型打包。
五、数组对象:
在java中数组是对象,数组的长度属性可回去的数组长度,可以用for循环一次取出数组中的每个值
二维数组要在类型关键字旁加上 [] [] 确定行列。
六、数组复制:
- 建立长度为x1相同的新数组,再逐一访问其中的每一个索引元素,并指定给x2对应的索引位置。
- 简单方法System,arraycopy():使用原生方式复制每个索引元素;以及Arrays.CopyOf()注:这两个方法都是浅层复制,如果要连同对象一起复制,需要自行操作。
七、字符串对象:
length()长度
charAt()字符
toUpperCase()将原本小写的字符串转为大写的内容
可以用+来连接字符串
字符串对象一旦建立,就无法更改对象中的任何内容,使用+会产生新的String实例。使用javac指令没有指定-encoding选项是,会使用操作系统默认编码。
八、对象封装:
- 封装的目的是指隐藏对象的属性和实现细节,命名规范中以get开头,之后接上首字母大写的单词。
- 只有在公开类程序中(public)才能够在类程序代码中存取类或对象成员。
- private关键字:是一个权限修饰符; 用于修饰成员(成员变量和成员函数);被私有化的成员只在本类中有效。
- this关键:在构造函数参数与对象数据成员同名时加以区分时用的。
- static关键字:用于修饰成员(成员变量和成员函数),静态方法只能访问静态成员;静态方法中不可以写this,super关键字;主函数是静态的。
教材学习中的问题和解决过程
1.Field.java p84
{
String color;
char size;
}
public class Field
{
public static void main(String[] args)
{
Clothes sun = new Clothes();
Clothes spring = new 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);
}
}```
###2.Field2.java p85
```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);
}
} ```

###3.Guess.java p86
```import java.util.Scanner;
public class Guess
{
public static void main(String[] args)
{
Scanner scanner = new Scanner (System.in);
int number = (int) (Math.random() * 10);
int guess;
do
{
System.out.printf("GUESS A NUMBER:");
guess = scanner.nextInt();
}
while(guess != number);
System.out.println("YOU ARE RIGHT!");
}
} ```

###4.DecimalDemo.java p88
```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);
}
} ```

###5.IntegerDemo.java p91
```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(w2));
}
} ```

###6.Score.java p96
```public class Socre {
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]);
}
}
}```

###7.XY.java p97
```public class XY
{
public static void main(String[] args)
{
int[][] cords={
{1,2,3},
{4,5,6}
};
for(int[] row : cords)
{
for(int value : row)
{
System.out.printf("%2d",value);
}
System.out.println();
}
}
} ```

###8.Score2.java p98
```import java.util.Arrays;
public class Score2
{
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);
}
}
} ```

###9.Copy.java p104
```import java.util.Arrays;
public class Copy
{
public static void main(String[] args)
{
int[] scores1 = {88,81,74,68,78,76,77,85,95,93};
int[] scores2 = Arrays.copyOf(scores1,scores1.length);
for(int score : scores2)
{
System.out.printf("%3d",score);
}
System.out.println();
scores2[0] = 99;
for(int score : scores1)
{
System.out.printf("%3d",score);
}
}
}```

###10.DeepCopy.java p106
```class Clothes2
{
String color;
char size;
Clothes2(String color, char size)
{
this.color=color;
this.size=size;
}
}
public class DeepCopy
{
public static void main(String[] args)
{
Clothes2[] c1 = {new Clothes2("red",'S'),new Clothes2("green",'M')};
Clothes2[] c2 = new Clothes2[c1.length];
for(int i = 0; i < c1.length; i++)
{
Clothes2 c = new Clothes2(c1[i].color, c1[i].size);
c2[i] = c;
}
c1[0].color = "yellow";
System.out.println(c2[0].color);```

##11.Sum.java p108
```import java.util.Scanner;
public class Sum
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
long sum = 0;
long number = 0;
do
{
System.out.print("输入数字:");
number = Long.parseLong(scanner.nextLine());
sum += number;
}
while(number != 0);
System.out.println("总和为:"+sum);
}
}```

###12.OneTo100.java p112
```public class OneTo100
{
public static void main(String[] args)
{
StringBuilder builder = new StringBuilder();
for(int i = 1; i < 100; i++)
{
builder.append(i).append('+');
}
System.out.println(builder.append(100).toString());
}
}```

###13.OverloadBoxing.java p135
```class Some
{
void someMethod(int i)
{
System.out.println("int 版本被调用");
}
void someMethod(Integer integer)
{
System.out.println("Integer 版本被调用");
}
}
public class Overload
{
public static void main(String[] args)
{
Some s = new Some();
s.someMethod(1);
}
}```

###14.ObjectlnitialBlock.java p137
```class Other{
{
System.out.println("对象初始区块");
}
Other()
{
System.out.println("Other() 构造函数");
}
Other(int o )
{
this();
System.out.println("Other(int o ) 构造函数");
}
}
public class ObjectInitialBlock
{
public static void main(String[] args)
{
new Other(1);
}
}```

###15.ImportStatic.java p144
```import java.util.Scanner;
import static java.lang.System.in;
import static java.lang.System.out;
public class ImportStatic
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(in);
out.print("请输入姓名:");
out.printf("%s 你好!%n",scanner.nextLine());
}
}```

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

因为粗心将{}和()弄混
导致程序一直出错。
## 其他(感悟、思考等,可选)
- 克服拖延症,周日晚上太痛苦,知识掌握也不扎实
- 学习了一波Markdownpad2的使用方法,可是代码块的渲染还是出了问题 难道不是收尾```?
- 代码托管仍然存在问题,就差最后一步了!第一次托管命令时手抖把用户名写错了,在git上用百度出来的方法amend重新改用户名,建立联系后仍然是“hello 老用户名”(奇怪的是用git log命令查已经变成了新用户名,这难道不是证明更改成功?),最后一步敲的域名是新用户名,可是显示说找不到。打算这两天再自己捣鼓下,实在不行就去请教同学和老师~目前git@osc上的代码是手动码的。



代码上传成功了!
## 学习进度条
| | 代码行数(新增/累积)| 博客量(新增/累积)|学习时间(新增/累积)|重要成长|
| -------- | :----------------:|:----------------:|:---------------: |:-----:|
| 目标 | 5000行 | 30篇 | 400小时 | | |
| 第三周 | 500/1000 | 3/7 | 22/60 | |
## 参考资料
- [Java学习笔记(第8版)](http://book.douban.com/subject/26371167/)
- [《Java学习笔记(第8版)》学习指导](http://www.cnblogs.com/rocedu/p/5182332.html)
- ...