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

    1. 本周学习总结

    -本周学习了面向对象,学会了如何用Eclipse自动生成setter/getter/toString以及构造有参函数等

    2. 书面作业

    1、代码阅读

    public class Test1 {
        private int i = 1;//这行不能修改
        private static int j = 2;
    
        public static void main(String[] args) {
            geti();
            Test1getj();
        }
    
        public static void geti() {
            System.out.println(i);
        }
    
        public static void getj() {
            System.out.println(j);
        }
    }
    

    以上代码可否编译通过?哪里会出错?为什么?尝试改正?如果创建3个Test1对象,有内存中有几个i,几个j?请分析原因?

    答:如图所示:

    不能通过,如图所示出现了两处错误,第一个显而易见就是Test1getj();第二个就是函数geti()中的i;
    第一个错误的原因是因为没有Test1getj()函数;第二个的原因是因为static方法不能够访问非static属性以及方法;改正代码:
    public class Test1 {
        private int i = 1;//这行不能修改
        private static int j = 2;
        public static void main(String[] args) {
            geti();
            Test1.getj();
        }
        public static void geti() {
            Test1 test=new Test1();//新建了一个对象;
            System.out.println(test.i);//将i改成test.i;
        }
        public static void getj() {
            System.out.println(j);
        }
    }
    
    如果创建3个Test1对象,会有3个i,一个j;因为static类的对象系统中只会分配一个空间,而非static的每个都会分配一个储存空间。
    

    2、构造函数有什么用?其编写格式是什么?如果一个类不写构造函数,它有构造函数吗?如果一个类中只有带参的构造函数,它有没有不带参数的默认构造函数?

    答: 对类进行初始化,如初始化类的属性;
        其编写格式就如ppt中构造函数的特点:构造函数的名称要和类名一致,没有返回类型(不是void);
        有,如果一个类中没写构造函数,系统会默认一个无参构造函数;
        没有,如果想要有的话,用户可以自己定义一个无参构造函数。
    
    

    3、使用java.lang.Math类的函数时,为什么不需要new?如果new Math()会产生什么错误?分析原因?

    答:对于这一题我们看一下Math的源代码

    就让我翻译一下这句:不要让任何人实例化这个类。
    如果用new的话就是将这个类实例化了,而之前翻译的句子就说到了不能实例化这个类;
    至于为什么会错误呢?看一下图片:

    还是之前的那个Math类不能实例化;所以系统将Math()看成一个未定义的类。

    4、什么是重载?什么是构造函数重载?在java.lang.Math类中举出1个函数重载的例子?怎么才能区分两个重载函数?

    答:重载(overload—方法重名但是参数不同),这么说有点空泛,我从jdk文档中查找了Math类的abs:

    我们可以从图中可以看出Math.abs();的方法有很多但是因为其参数不一样,调用的方法不一样,所以输出结果不一样。

    通过函数的参数来区分,如Math.abs(1.3)就是调用第二个函数。

    5、final修饰的变量不能改变,为什么如下代码可以正常运行?

    final int[] NUMBS= {1,2,3,4,5};
    NUMBS[2] = 1;
    

    答:final修饰引用类型变量,变量的值(引用)不能修改;这一题中变量NUMBS存储的是数组的首地址,因此修改NUMBS[2]的值并不改变NUMBS中的值。

    6、阅读代码EmployeeTest.java,回答:

    6.1、为什么其属性均为private?这样设计有什么好处?

    6.2、为Employee类增加一个无参构造函数,调用其有参构造函数,设置name为雇员1, salary为3000, hireDay的年月日为2017年3月5日。(粘贴代码)

    6.3、为Employee类再增加一个有参构造函数,参数只有name与salary,hideDay为当前日期。(粘贴代码)

    答:6.1:因为如果属性为public的话,数据就容易被修改,信息的安全性就不能够得到保证;用private的话确保了数据的封装性;

    6.2:

    import java.util.*;
    
    /**
     * This program tests the Employee class.
     * @version 1.11 2004-02-19
     * @author Cay Horstmann
     */
    public class EmployeeTest
    {
       public static void main(String[] args)
       {
          // fill the staff array with three Employee objects
          Employee[] staff = new Employee[3];
    
          staff[0] = new Employee("Carl Cracker", 75000, 1987, 12, 15);
          staff[1] = new Employee("Harry Hacker", 50000, 1989, 10, 1);
          staff[2] = new Employee("Tony Tester", 40000, 1990, 3, 15);
    
          // raise everyone's salary by 5%
          for (Employee e : staff)
             e.raiseSalary(5);
    
          // print out information about all Employee objects
          for (Employee e : staff)
             System.out.println("name=" + e.getName() + ",salary=" + e.getSalary() + ",hireDay="
                   + e.getHireDay());
       }
    }
    
    class Employee
    {
       public Employee(String n, double s, int year, int month, int day)
       {
          name = n;
          salary = s;
          GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
          // GregorianCalendar uses 0 for January
          hireDay = calendar.getTime();
       }
       
       public Employee() {
    	   this("雇员1", 3000.0, 2017, 3, 5);
       }
    
       public String getName()
       {
          return name;
       }
    
       public double getSalary()
       {
          return salary;
       }
    
       public Date getHireDay()
       {
          return hireDay;
       }
    
       public void raiseSalary(double byPercent)
       {
          double raise = salary * byPercent / 100;
          salary += raise;
       }
    
       private String name;
       private double salary;
       private Date hireDay;
    }
    
    

    6.3

    import java.util.*;
    
    /**
     * This program tests the Employee class.
     * @version 1.11 2004-02-19
     * @author Cay Horstmann
     */
    public class EmployeeTest
    {
       public static void main(String[] args)
       {
          // fill the staff array with three Employee objects
          Employee[] staff = new Employee[3];
    
          staff[0] = new Employee("Carl Cracker", 75000, 1987, 12, 15);
          staff[1] = new Employee("Harry Hacker", 50000, 1989, 10, 1);
          staff[2] = new Employee("Tony Tester", 40000, 1990, 3, 15);
    
          // raise everyone's salary by 5%
          for (Employee e : staff)
             e.raiseSalary(5);
    
          // print out information about all Employee objects
          for (Employee e : staff)
             System.out.println("name=" + e.getName() + ",salary=" + e.getSalary() + ",hireDay="
                   + e.getHireDay());
       }
    }
    
    class Employee
    {
       public Employee(String n, double s, int year, int month, int day)
       {
          name = n;
          salary = s;
          GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
          // GregorianCalendar uses 0 for January
          hireDay = calendar.getTime();
       }
       
        public Employee(String n, double s) {
            this(n, s, Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(Calendar.MONTH) + 1,Calendar.getInstance().get(Calendar.DAY_OF_MONTH));
        }
    
       public String getName()
       {
          return name;
       }
    
       public double getSalary()
       {
          return salary;
       }
    
       public Date getHireDay()
       {
          return hireDay;
       }
    
       public void raiseSalary(double byPercent)
       {
          double raise = salary * byPercent / 100;
          salary += raise;
       }
    
       private String name;
       private double salary;
       private Date hireDay;
    }
    

    7、编写函数public static boolean isMondayToFriday()

    功能:判断今天如果是周一到周五,直接输出如“上班时间”并返回true,否则输出“休息时间”。

    提示:使用LocalDateTime, DayOfWeek

    参考资料:

    JAVA 8:健壮、易用的时间-日期API - ImportNew.pdf

    Java8DateTimeTest.java

    public static boolean isMondayToFriday() {
        LocalDateTime time = LocalDateTime.now();
        int day = time.getDayOfWeek().getValue();
        if (day >= 1 && day <= 5) {
            System.out.println("上班时间");
            return true;
        }else{
        System.out.println("休息时间");
        return false; 
        }      
    }
    

    3. 码云代码提交记录

    4. PTA实验总结

    1、代码在本机上可以运行但是在pta上显示答案错误;后来问老师,原来只是格式错误,排版错误。

    2、实验2和实验1相类似所以没什么太大的错误。

    3、在编写实验3中Circle的getArea()和getPerimeter()时因为Math.PI是一个double型,最后返回值也会是double,而函数的返回值是int,可以这样return int (2Math.PIthis.radius);将返回值强制更改为int型;

    4、本次实验难度不高,没有遇到什么太大的问题。

  • 相关阅读:
    javascript 函数介绍
    javascript 日期对象(date)详解
    js 计算过去和未来的时间距离现在多少天?
    phpcms 模板常用标签指南
    checkbox radio 样式重写
    datatable-固定行固定列
    表格-固定列 固定行
    axios 请求数据 入门级介绍
    图片上传的问题-偶现base64图片 小黑块问题
    cropper.js图片裁剪——第二弹
  • 原文地址:https://www.cnblogs.com/wjt960310/p/6534665.html
Copyright © 2011-2022 走看看