学号20145220《Java程序设计》第3周学习总结

教材学习内容总结

使用jave撰写程序几乎都在使用对象(Object),要产生对象必须先定义类(Class),类是对象的设计图,对象是类的实例。类定义时使用class关键词,名称使用clothes。在jave的术语中,这叫声明参考名称定义类。产生对象必须先定义类,对象是类的实例,对象和实例在Java中几乎是等义的名词。- class定义类时使用的关键词- String变量的字符串表示- char类型声明变量- new新建一个对象- =参考至新建对象
eg:在Clothes类中,定义color与size两个变量。(定义两个值域成员/对象数据成员)
class Clothes
{
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);
}

}
运行结果:

构造函数
函数是定义在类中的而具有特定功能的一段独立程序,函数即方法。函数中不能定义函数,只能调用函数。
优势:函数提高了代码的复用性。
重载: 在同一个类中,允许存在一个以上的同名函数,只要他们的参数个数或参数类型不同即可。(重载只和参数列表有关)

格式:{
执行语句;
return返回值;(函数运行后的结果的数据类型)
}

  • 参数类型:形式参数的数据类型
  • 形式参数:一个变量,用于存储调用函数时传递给函数的实际参数
  • 实际参数:传递给形式参数的具体数值
  • return:用于结束函数
  • 返回值:该值会返回给调用者(没有具体返回值时用void)

如何定义一个函数?
1.明确该函数功能的运算结果(明确函数的返回值类型)
2.明确在定义过程中是否需要未知的内容参与运算(明确函数的参数列表)
eg:定义构造函数
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);
    
}

}
运行结果

使用标准类
1.使用java.util.Scanner
eg:猜数字

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.print("猜数字(0~9):");
        guess = scanner.nextInt();
    }
    while (guess != number);
    
    System .out.println("bingo!!");
}

}
运行结果

2.使用java.math.BigDecimal
eg:得到更好的精确度
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);

}

}
运行结果

import java.math.BigDecimal;
eg:比较相等

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");
}
}
}
运行结果

基本类型打包器
1.打包器的基本类型:Long、Integer、Double、Float、Boolean。将基本类型打包在对象之中,这样就可以操作这个对象,就是将基本类型当做对象操作。
eg:
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));
}

}
运行结果

2.自动装箱、拆箱
eg:简洁版
public class IntegerDemo2
{
public static void main (String[] args)
{
Integer data1 = 10;
Integer data2 = 20;
System.out.println(data1.doubleValue() /3);
System.out.println(data1.compareTo(data2));
}

}
运行结果

数组对象
定义:数组是同一类型的数据的集合,相当于一个容器。
优势:将数组中的元素从0开始编号,便于操作。
(数组名称.length):直接获取数组中的元素个数
eg:依次取出数组中的每个值(使用for循环)
public class Score
{
public static void main (String[] args)
{
int[] scores ={88,81,74,78,76,77,85,95,93};
for(int i = 0;i < scores.length;i++)
{
System.out.printf("学生分数:%d %n",scores[i]);
}
}
}
运行结果

eg:增强式for循环,声明数组来储存XY坐标位置要放的值(二维数组使用两个索引存取数组元素)
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();
}

}

}
运行结果

操作数组对象
如果事先只知道元素个数,那么可以使用new关键词指定长度来建立数组。
1.栈:数据使用完毕,会自动释放。
2.堆:- 堆中的每一个实体都有一个内存地址值
- 队中的实体是用于封装数据,都有默认值 int:0
double:0.0
float:0.0f
boolean:false
- 垃圾回收机制
eg:使用java.util.Arrays的fill()方法来设定袭击案数组的元素值。将每个学生的成绩默认60分起。
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);
            }
    }

}
运行结果

数组复制
数组复制的基本做法是另行建立新数组
System.arraycopy()方法会使用原生方式复制每个索引元素,System.arraycopy()的五个参数分别是来源数组、来源起始索引、目的数组、目的起始索引、复制长度。
eg:使用更方便的Arrays.copyOf()方法,建立新的数组
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);
        }
}   

}
运行结果

System.arraycopy()及Arrays.copyOf()在类类型生命的数组时,都是执行浅层复制。
eg:深层复制,连同对象一同复制。
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);
    }

}
运行结果

字符串
在Java中,字符串是java.lang.String实例,用来打包字符数组。

  • length():取得字符串长度
  • charAt():指定取得字符串中某个字符
  • toUppercase():将原本小写的字符串内容转为大写。

如果有一个char[]数组,也可以使用new来创建String。也可以使用String的toCharArray()方法,以将字符串以char[]数组返回。
eg:让用户输入整数,输入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:"+sum);
}
}
运行结果

字符串特性

  • 字符串常量与字符串池:用“”写下的字符串称为字符串常量,在JVM中只会建立一个String实例。

  • 不可变动字符串:在java中,字符串对象一旦建立,就无法更改对象中任何内容,对象上没有任何方法可以更改字符串内容。
    eg:使用 + 连接字符串,改用 StringBuilder 来改善的代码,1+2+…+100?
    public class Plus
    {
    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());
    }
    }
    运行结果

    查询API文档
    学习Java要学会使用Java API,具有检索功能,使用非常方便。
    封装
    定义:封装是面向对象方法的重要原则,就是把对象的属性和操作(或服务)结合为一个独立的整体,并尽可能隐藏对象的内部实现细节。
    作用:- 对象的数据封装特性彻底消除了传统结构方法中数据与操作分离所带来的种种问题,提高了程序的可复用性和可维护性,降低了程序员保持数据与操作内容的负担。
    - 对象的数据封装特性还可以把对象的私有数据和公共数据分离开,保护了私有数据,减少了可能的模块间干扰,达到降低程序复杂性、提高可控性的目的
    eg:
    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);
          }
      }
    

}
封装对象
eg:直接建立三个CashCard对象。而后进行储值并显示明细。
import java.util.Scanner;

public class CashApp
{
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为公开类,在构造函数上声明public表示其他包中的类可以直接调用这个构造函数,在方法上声明public表示其他包的方法可以直接调用这个方法。
  • 构造函数
    特点:1) 函数名与类名相同 2)不用定义返回值类型 3)不可以写return语句
    作用: 给对象进行初始化
    注意:1) 默认构造函数的特点2) 多个构造函数是以重载的形式存在的
  • 方法重载(只与参数列表有关)
    eg:
    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);
    }
    }
    运行结果

    eg:调���参数为Integer版本的方法
    class Some
    {
    void someMethod(int i)
    {
    System.out.println("int 版本被调用");
    }
    void someMethod(Integer integer)
    {
    System.out.println("Integer 版本被调用");
    }
    }
    public class Overload2
    {
    public static void main(String[] args)
    {
    Some s = new Some();
    s.someMethod(new Integer(1));
    }
    }
    运行结果

    this
    this关键字可以出现在类中任何地方,在构造函数与对象数据成员同名时,可用this加以区别。
    当在函数内需要用到调用该函数的对象时,就用this。
    eg:在创建对象之后、调用构造函数之前,若有想执行的流程,可以用{}定义。
    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类成员
用于修饰成员(成员变量和成员函数)
被修饰后的成员具备以下特点:1)随着类的加载而加载
2)优先于对象存在
3)被所有对象所共享
4)可以直接被类名调用
使用注意:1)静态方法只能访问静态成员
2)静态方法中不可以写this,super关键字
3)主函数是静态的
eg:import 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());
}

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

刚开始定义类时,将Clothes和Field都定义成public类,一个原始码中可以有多个类定义,但公开类只能又一个,因此将Clothes改为非公开,文件名也修改为Field就可以了。

修改后:

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

输出时习惯性的写成了println,因此出现以下错误,println的参数只能是一个,而printf的参数是不定的,适用于格式化输出的,在其后面加%d(自动回车换行)。

修改后:

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

通过近一个月的学习,我对jave这门课程产生了浓厚的兴趣,我希望可以和老师多多互动,多多交流,更好的学习jave。

学习进度条

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

参考资料