201711671224 《Java程序设计》第4章学习总结

教材学习内容总结

  • 要产生对象必须先定义类,类是对象的设计图,对象是类的实例
  • 类是从少数实例推广到大量相似实例的抽象化过程

4.1 类与对象

  • 定义类
    • 1.先在程序中定义类class Clothes{String color;Char size;}(定义colorsize两个对象数据成员) 2.新建一个对象new Clothes() 3.声明参考名称(参考变量或参考)Clothes c1; 4.将c1名称参考至新建对象 CLothes c1 = new Clothes();
    • 类:公开类和非公开类,一个原始码中只能有一个公开类,但可以有多个类定义,且文档中的主文档名必须与公开类名称相同(计量不要在一个类中写多个class)
    • thisthis.color = color将参数的值指给这个对象的某个参数成员(this
    • new:新建一个对象
    • 两个基本标准类
      • java.util.Scanner:Scanner对每个基本类型都有相应的nextxxx()方法,将字符串剖析至相应类型,直接取得上一个字符串使用next(),取得整行文字nextLine()
     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("猜中了!");
    }
}
  • java.math.BigDecimal提供plus()subtract()multiply()divide()
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);
    }
}
  • 对象制定与相等性
    • =:用于基本类型,将值复制给变量;用于操作对象,指定参考名称参考某个对象==:用于基本类型,比较两个变量储存的值是否相同;用在操作对象,比较两个参考名称是否参考同一对象equals():用于操作对象,比较两对象的值是否相同

      4.2 基本类型包装

  • 将基本类型打包在对象之中,将基本类型当作对象来操作
  • 打包器(Wrapper):LongIntegerDoubleFloatBooleanByte
package cc.openhome;
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(wrapper2.compareTo(wrapper2));
    }
}
  • 自动装箱(Autoboxing)Integer wrapper = 10;编译程序自动判断是否能进行自动装箱int data1 = 10;Integer wrapper1 = new Integer(data1);可利用自动装箱改写成Integer data1 = 10;
  • 自动拆箱(Auto Unboxing)自动取出打包器中的基本形态信息Integer i = 10;//自动装箱 int foo = wrapper;//自动拆箱
  • 自动拆装箱是编译程序蜜糖,不能让变量参考至空对象又对之进行操作,也不能超过打包器的值域范围
  • 无论两个变量打包的值位于哪个范围,只要变量打包的值相同,equals()比较的结果就会是true

    4.3 数组对象

  • Java中,数组就是对象
  • 数组是具有索引的数据结构,如果存取超出索引范围,就会抛出错误
  • 依次取出数组中每个值的方法
    • for循环(数组的length属性可以取得数组的元素个数)
public class Score {
    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]);
        }
    }
}
  • 增强式for循环
public class EnhanceScore {
    public static void main(String[] args){
    int[] scores = {88,81,74,68,78,76,77,85,95,93};
    for(int score : scores){
        System.out.printf("学生分数:%d %n",score);
    }
}
}
  • 声明二维数组,在类型关键词旁加上[][],cords.length得知有几行,cords[x].length得知每行有几个元素
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]);
            }
        }
    }
} 
  • 利用增强for循环
public class EnhanceXY {
    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("%d",value);
            }
            System.out.println();
        }

    }
}
  • 利用java.util.Arraysfill()方法来设定新建数组的元素值
import java.util.Arrays;

public class ArrayScore {
    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()方法会使用原生方式复制每个索引元素,五个参数:来源数组、来源起始索引、目的数组、目的起始索引、复制长度,浅层复制
  • Array.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;//不影响score2参考的数组对象
        for(int score : scores1){
            System.out.printf("%3d",score);
        }
    }
}
  • 浅层复制:仅将c1每个索引处所参考的对象,也给c2每个索引来参考,用在类类型声明的数组时,都是执行浅层复制
  • 深层复制:实际上c1每个索引参考的对象会被复制,分别指定给c2每个牵引

    4.4 字符串对象

  • 字符串是打包字符数组的对象,是java.lang.String类的实例
  • length()取得字符串的长度,charAt()指定取得字符串中某个字符,toCharArray()将字符串以char[]数组返回

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.printf("输入数字:" );
            number = Long.parseLong(scanner.nextLine());
            sum += number;
        }while(number != 0);
        System.out.printf("总和:"+ sum);
    }
}
  • 以""包括的字符串,只要内容相同,无论在程序代码中出现几次,JVM只会建立一个String实例,并在字符串池中维护
  • 字符串对象一旦建立,就无法更改对象中任何内容,对象中没有任何方法可以更改字符串内容

    4.5 查询Java API文件

  • 查询java各种类的各种方法

其他(感悟、思考等)

补的学习总结……接下来都是按章写的了 o(╥﹏╥)o

参考资料

  • 《Java程序设计》

猜你喜欢

转载自blog.csdn.net/nemeziz/article/details/84543861