zoukankan      html  css  js  c++  java
  • 20145110《Java程序设计》第三周学习总结

    20145110 《Java程序设计》第三周学习总结

    教材学习内容总结

    chapter4 认识对象

    定义类

    类定义时要使用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);
        }
    }
    

    在这个程序中,一共定义了两个类,一个是公开的Main类,所以在文档中,主文档的命名就是Main,而另外一个就是非公开的clothes类。在程序中,又分别建立了两个clothes的实例,并分别声明了sun与spring两个名称来参考。
    这里要注意:在Field.java中,存在公开的Field类(即存在public class Field语句),所以文档名称必须是Field。
    所以当我将class的名重命名为Clothes时,会出现如下的编译错误

    构造函数中,由于参数与数据类型重名,必须用this将参数值指定给参数。

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

    使用标准类

    两个标准类:java.util.Scanner和java.math.BigDecimal

    1.使用java.util.Scanner

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

    2.使用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);
        }
    }
    


    创建BigDecimal的方法之一是使用字符串,BigDecimal在创建时会剖析传入的字符串,以默认精度进行接下来的运算。

    4.2 基本类型打包器
    打包器的基本类型为: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(wrapper2));
        }
    
    }
    

    4.2.2 自动装箱、拆箱

    Integer wrapper = 10;    //自动装箱
    int foo = wrapper;       //自动拆箱
    

    wrapper会参考至Integer,若被指定给int型的变量foo,则会自动取得打包的int类型再指定给foo。

    4.3 数组对象

    声明二维数组来储存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();
            }
        }
    }
    

    或者用更简洁的for循环来改写

    for(int[] row:cords) {
        for(int value :row) {
            System.out.printf("%2d",value);
        }
        System.out.println();
    }
    

    4.3.2 操作数组对象
    当实现并不知道元素值,只知道元素个数,可以用new关键词指定长度来建立数组。
    例如

    int[] scores=new int[10];
    

    可以用java.utill.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);
        }
    }
    } 
    

    4.3.3 数组复制
    数组复制的基本做法是另行建立新数组。System.arraycopy()的五个参数分别是来源数组、来源起始索引、目的数组、目的起始索引、复制长度。

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

    4.4 字符串对象
    字符串本质是打包数组的对象

    String name="justin"; //建立String实例
    System.out.println(name); //显示justin
    System.out.println(name.length()); //显示长度为6
    System.out.println(name.charAt(0)); //显示第一个字符j
    System.out.println(name.toUpperCase()); //显示JUSTIN
    
    import java.util.Scanner;
    
     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);
        }
    }
    

    第五章 对象封装
    5.1 何谓封装
    对象(或者说是类)可以封装变量, 函数及其它对象. 所谓"封装"即调用对象的用户不必了解实现特定功能的详细代码和过程, 只需懂得设置对象属性(即定义时被封装的变量), 调用其方法(即定义时被封装的函数)就可以了。

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

    5.2 类语法细节
    public权限修饰
    public是个公开类,可以在其他包的类中使用。可以在构造函数上声明public,这表示其他包中的类可以直接调用这个构造函数。可以在方法上声明public,这表示其他包中的方法可以直接调用这个方法。

    关于构造函数

    public class Some {
        private int a=10;
        private String text;
        public Some(int a, String text) {
            this.a=a;
            this.text=text;
        }
    }
    

    重载构造函数
    (1)重载的概念:在同一个类中,允许存在一个以上的同名函数,只要他们的参数个数或者参数类型不同即可。
    (2)重载的特点:与返回值类型无关,只看参数列表。
    (3)重载的好处:方便于阅读,优化了程序设计。

    使用this

    /**
     * Created by TYC on 2016/3/20.
     */
    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类成员
    被声明为static的成员,不会让个别对象拥有,而是属于类。
    由于static成员是属于类,而非个别对象,所以在static成员中使用this,会是一种语意上的错误。

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

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

    这一周的学习较前两周来说,学习量特别大。有很多晦涩难懂的知识点,需要很长时间消化理解。需要多尝试一些代码来加强理解。

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

    一个 *.java 文件中,只能有一个public 的类,而且这个public修饰的这类必需要和这个文件名相同
    问题出在文件命名上,应该命名为Sum.java

    另外,我尝试了书中P121的操作题1,是一个关于费氏数列的问题。其中用到了数组和java.util.Scanner.
    代码如下:

    import java.util.Scanner;
    
    public class Fei1 {
        public static void main(String[] args) {
            Scanner s = new Scanner(System.in);
            System.out.printf("求几个费氏数?");
            int n=s.nextInt();
            int arr[] = new int[n];
            arr[0] =0;
            arr[1] = 1;
            for (int i = 2; i < arr.length; i++) {
                arr[i] = arr[i - 1] + arr[i - 2]; }
            System.out.printf("斐波那契数列的前%d项如下所示:",n);
            for (int i = 0; i < arr.length; i++) {
                System.out.println(arr[i]); }
        }
    }
    

    截图如下所示:

    不过所显示的结果是一行一个,看上去并不美观,不知道如何修改。希望老师能够指导。

    学习进度条

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

    参考资料

  • 相关阅读:
    window.open() 使用详解
    20151117
    20151116
    打开一个网页并弹窗提示,点击确定后2秒后关闭
    网页制作中的一点问题及解决方案
    Android WebView 开发详解(二)
    Android WebView 开发详解(一)
    Android:控件WebView显示网页
    Dagger 2: Step To Step
    Introducing RecyclerView(二)
  • 原文地址:https://www.cnblogs.com/20145110tyc/p/5299467.html
Copyright © 2011-2022 走看看