博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
20145235李涛 《Java程序设计》第3周学习总结
阅读量:4340 次
发布时间:2019-06-07

本文共 4089 字,大约阅读时间需要 13 分钟。

 

类与对象

定义类

类是对象的“设计图”,对象是类的实际类型。另外,定义时用class,建实例用new。 通过书上的代码才有所理解:

 

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);}}

 

spring和sun就是一个类类型变量,类类型变量指向的是对象。 运行结果:

 

  • 标准类

java.util.Scanner、java.math.BigDecimal是API中两个基本的标准类。

java.util.Scanner的nextInt()方法会看标准输入中,录入下一个字符串,并会尝试将之剖析为int类。一旦直接取得上一个字符串,则使用next()。nextLine()这是一个更加方便的功能,直接取得用户输入的整行文字,类似于C中的gets和getc之间的关系。 另外,还有nextByte()、nextShort()之类的功能也是以此类推的。

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!");}}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);} }

 

运行结果:

基本类型打包器

Long、Integer、Double、Float、Boolean等,就是所谓的打包器。其主要目的是提供对象实例作为“壳”,将基本类型打包在对象之中,就可以将基本类型当作对象操作。书上的简单练习例子如下:

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));}}

 

运行结果:

1、数组基础
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();}}}

 

2、操作数组对象
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);}}}

 

3、数组复制

首先,再建立一个数组,再对这个数组进行转移,就是一个简单的方法。另外,还可以使用arrays.copyOf()。

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);}}   }

 

字符串对象

字符串使用java.lang.String实例,用来打包字符数组。 可以使用length()取得字符串长度。 另外还可以使用charAt()指定取得字符串中某个字符,使用toUppercase()将原本小写的字符串内容转为大写。

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 One{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());}}

 

对象封装

封装

封装是指隐藏对象的属性和实现细节,仅对外提供公共访问方式,其目的主要就是隐藏对象细节,将对象当作黑箱进行操作。

构造函数

构造函数特点:首先,函数名与类名相同,其次,不用定义返回值类型,另外不可以写return语句。 构造函数作用是给对象进行初始化,多个构造函数是以重载的形式存在的。

this关键字

this代表其所在函数所属对象的引用,简而言之就是this代本类对象的引用,当在函数内需要用到调用该函数的对象时,我们就可以使用this。 this()代表了调用另一个构造函数,至于调用哪个构造函数,则视调用this()时给的自变量类型与个数而定。

static关键字

static用于修饰成员(成员变量和成员函数),被声明为static的成员,不会让个别对象拥有,而是属于类。 在static方法中不能出现this关键字,static方法中不能用非static数据或方法成员。 import static语法是为了偷懒,但要注意名称冲突的问题。

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

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

通过对四,五章的学习,对类类型有了一定的了解,诚然基本类型容易掌握,而类类型掌握起来就不那么容易了,API中有很多很多的标准类等着我们去学习,然而学习Java的过程任重而道远,就两张的知识学起来,并不是那么容易,这周空余时间基本都在看Java,而这两章知识并不好理解,还要动手敲书上的代码练习来巩固自己的所领悟到的。

学习进度条

代码行数(新增/累积) 博客量(新增/累积) 学习时间(新增/累积) 重要成长 目标 5000行 30篇 400小时 第一周 50/50 1/2 20/20 第二周 600/600 2/4 38/38 第三周 276/600 1/7 60/60 第四周 /1300 2/9 /90 参考资料

Java学习笔记(第8版) 《Java学习笔记(第8版)》学习指导

转载于:https://www.cnblogs.com/20145235litao/p/5299219.html

你可能感兴趣的文章
阶段3 2.Spring_03.Spring的 IOC 和 DI_6 spring中bean的细节之三种创建Bean对象的方式
查看>>
阶段3 2.Spring_04.Spring的常用注解_2 常用IOC注解按照作用分类
查看>>
阶段3 2.Spring_09.JdbcTemplate的基本使用_5 JdbcTemplate在spring的ioc中使用
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_02.ssm整合之搭建环境
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第1节零基础快速入门SpringBoot2.0_3、快速创建SpringBoot应用之手工创建web应用...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第1节零基础快速入门SpringBoot2.0_5、SpringBoot2.x的依赖默认Maven版本...
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_08.ssm整合之Spring整合MyBatis框架
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_9、SpringBoot基础HTTP其他提交方法请求实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_12、SpringBoot2.x文件上传实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_19、SpringBoot个性化启动banner设置debug日志...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_20、SpringBoot2.x配置全局异常实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第5节 SpringBoot部署war项目到tomcat9和启动原理讲解_23、SpringBoot2.x启动原理概述...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_21、SpringBoot2.x配置全局异常返回自定义页面...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_32..SpringBoot2.x持久化数据方式介绍...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_34、SpringBoot整合Mybatis实操和打印SQL语句...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_35、事务介绍和常见的隔离级别,传播行为...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_40、Redis工具类封装讲解和实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_37、分布式缓存Redis介绍...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第10节 SpringBoot整合定时任务和异步任务处理_42、SpringBoot常用定时任务配置实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_39、SpringBoot2.x整合redis实战讲解...
查看>>