类与对象
定义类
类是对象的“设计图”,对象是类的实际类型。另外,定义时用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版)》学习指导