20145238 《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循环一次取出数组中的每个值
语法:
for(int score:scores){
System.out.printf(“分数:%d ”,score);
}
·二维数组要在类型关键字旁加上 [] [] 确定行列。
六、数组复制:
·建立长度为x1相同的新数组,再逐一访问其中的每一个索引元素,并指定给x2对应的索引位置。
·简单方法System,arraycopy():使用原生方式复制每个索引元素;以及Arrays.CopyOf()注:这两个方法都是浅层复制,如果要连同对象一起复制,需要自行操作。
七、字符串对象:
·length()长度
·charAt()字符
·toUpperCase()将原本小写的字符串转为大写的内容
·可以用+来连接字符串
字符串对象一旦建立,就无法更改对象中的任何内容,使用+会产生新的String实例。
·使用javac指令没有指定-encoding选项是,会使用操作系统默认编码。
八、对象封装:
·封装的目的是指隐藏对象的属性和实现细节,命名规范中以get开头,之后接上首字母大写的单词。
·只有在公开类程序中(public)才能够在类程序代码中存取类或对象成员。
·private关键字:是一个权限修饰符; 用于修饰成员(成员变量和成员函数);被私有化的成员只在本类中有效。
·this关键:在构造函数参数与对象数据成员同名时加以区分时用的。
·static关键字:用于修饰成员(成员变量和成员函数),静态方法只能访问静态成员;静态方法中不可以写this,super关键字;主函数是静态的。
教材学习中的问题和解决过程
·定义两个对象数组成员运行代码如下
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);
}
}
·运行结果截图
·用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);
}
}
·代码截图
·用BigDecimal比较相等的例子
import java.math.BigDecimal;
public class DecimalDemo2 {
public static void main(String[] args) {
BigDecimal op1 = new BigDecimal("0.1");
BigDecimal op2 = new BigDecimal("0.1");
BigDecimal op3 = new BigDecimal("0.1");
BigDecimal result = new BigDecimal("0.3");
if(op1.add(op2).add(op3).equals(result)){
System.out.println("等于0.3");
}
else {
System.out.println("不等于0.3");
}
}
}
·代码结果截图
·类型打包对象例子代码如下:
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));
}
}
·代码结果截图
·依次取出数组中的值(采用for循环)
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]);
}
}
}
·代码结果截图
·声明二位数组来存储xy的坐标的例子代码如下:
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();
}
}
}
·代码结果截图:
·使用java.util.Arrays的fill()方法来设定新建数组元素值的例子:
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);
}
}
}
·代码结果截图:
·每个索引参考至Integer实例代码如下:
public class IntegerArray {
public static void main(String[] args) {
Integer[] scores = new Integer[3];
for (Integer score : scores){
System.out.println(score);
}
scores[0] = new Integer(99);
scores[1] = new Integer(87);
scores[2] = new Integer(66);
for(Integer score : scores){
System.out.println(score);
}
}
}
·代码结果截图:
·数组复制例子代码如下:
import java.util.Arrays;
public class CopyArray {
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);
}
}
}
·代码结果截图:
·深层复制(参考对象也被复制)代码如下:
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", 'L'), new Clothes2("blue", '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);
}
}
·代码结果截图:
·用户输入整数,输入0后计算所有整数总和代码如下:
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);
}
}
·代码结果截图:
·不可变动字符串代码如下:
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());
}
}
·代码结果截图
·构造函数存储数组成员代码如下:
class CashCard{
String number;
int balance;
int bonus;
CashCard(String number ,int balance,int bonus){
this.number = number;
this.balance = balance;
this.bonus = bonus;
}
}
public class CardApp {
public static void main(String[] args) {
CashCard[] cards = {
new CashCard("A001",500,0),
new CashCard("A002",300,0),
new CashCard("A003",1000,1),
new CashCard("A004",2000,2),
new CashCard("A005",3000,3)
};
for(CashCard card : cards){
System.out.printf("(%s,%d,%d)%n",card.number,card.balance,card.bonus);
}
}
}
·代码运行结果截图
·构造函数与方法重载例子代码如下:
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);
}
}
·代码结果截图
调用参数为Integer版本的方法:只需把最后一句(1)改为Integer(1)
·使用this例子代码如下:
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);
}
}
·代码结果截图
·static语法
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());
}
}
·代码结果截图
代码调试中的问题和解决过程
·有问题的代码如下
public class Averagr {
public static void main(String[] args) {
long sum=0;
for(String arg : args){
sum += Long.parseLong(arg);
}
System.out.println("平均:" +(float)sum/args.length);
}
}
出错结果
另一个出错代码
import java.util.Scanner;
public class CashCard {
public static void main(String[] args) {
CashCard[] cards = {
new CashCard("A001",500,0),
new CashCard("A002",300,0),
new CashCard("A003",1000,1),
};
Scanner scanner = new Scanner(System.in);
for(CashCard card : cards){
System.out.printf("为(%s,%d,%d)储值:",card.number,card.balance,card.bonus);
card.store(scanner.nextInt());
System.out.printf("明细(%s,%d,%d)%n",card.number,card.balance,card.bonus);
}
}
}
出错结果
不太清楚第一个问题所在
对于第二个代码,出错问题在于public类的名称与书上的文件名不一致,照着书敲代码也不能不经过大脑思考,对照之前自己应该先思考应该写的什么然后在通过书检查正误。
其他(感悟、思考等,可选)
在Idea中同一个src里面运行代码如果有一个出错,下一个代码就无法运行,运行之后自动跳转到前一个出错的代码片度,,,(我还以为是电脑出了错)
这周的学习量和难度感觉比前面都上了一个等级和梯度,我认为学习java敲代码是一方面,最主要的是理解代码的含义,如果只是单纯的打代码不经过自己的思考那也是无益,希望以后自己能够对运行以后的每一个代码理解能更深入。
使用开源中国托管代码截图:
学习进度条
代码行数(新增/累积) | 博客量(新增/累积) | 学习时间(新增/累积) | 重要成长 | |
---|---|---|---|---|
目标 | 5000行 | 30篇 | 400小时 | |
第一周 | 200/200 | 2/2 | 20/20 | |
第二周 | 300/500 | 2/4 | 18/38 | |
第三周 | 500/1000 | 3/7 | 22/60 |