201521123100 《Java程序设计》第3周学习总结
1. 本周学习总结
初学面向对象,会学习到很多碎片化的概念与知识。尝试学会使用思维导图将这些碎片化的概念、知识组织起来。请使用纸笔或者下面的工具画出本周学习到的知识点。截图或者拍照上传。
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);
}
}
上代码可否编译通过?哪里会出错?为什么?尝试改正?
A:编译不可通过;图一中第15行出错;因为此时i变量不属于static变量
如果创建3个Test1对象,有内存中有几个i,几个j?请分析原因?
A:i一个,j一个
2. 构造函数有什么用?其编写格式是什么?如果一个类不写构造函数,它有构造函数吗?如果一个类中只有带参的构造函数,它有没有不带参数的默认构造函数?
A:构造函数对类进行初始化操作,编写格式:public xxx(){};如果一个类,你没有定义构造函数,那么系统默认会有一个无参的构造函数
3. 使用java.lang.Math类的函数时,为什么不需要new?如果new Math()会产生什么错误?分析原因?
A: 因为java.lang.Math中所有的函数都是静态方法,所以不需要new;没办法使用new Math()会出现"The constructor Math() is not visible"字样
4. 什么是重载?什么是构造函数重载?在java.lang.Math类中举出1个函数重载的例子?怎么才能区分两个重载函数?
A:重载,就是名称相同,参数不同;构造函数的重载,就是构造函数具有相同的名字,而参数的个数或参数类型不相同
5. final修饰的变量不能改变,为什么如下代码可以正常运行?
final int[] NUMBS= {1,2,3,4,5};
NUMBS[2] = 1;
````````````````````````````````````````````````````````````````````````````````````````````````````````````````
A:final修饰引用类型变量,变量的值(引用)不能修改,但是变量指向的对象的值可以修改
**6. 阅读代码EmployeeTest.java,回答:
为什么其属性均为private?这样设计有什么好处?**
A:因为属性是不希望被人轻易知道的,所以要将属性保护好,只有设置为private,才不会随便被调用或是被改变
**1. 为Employee类增加一个无参构造函数,调用其有参构造函数,设置name为雇员1, salary为3000, hireDay的年月日为2017年3月5日。(粘贴代码)**
A:
```````````````````````````````````````````````````````````````````````
public Employee()
{
Employee("雇员1", 3000.0, 2017, 3, 5);
}
``````````````````````````````````````````````````````````````````````````
**2. 为Employee类再增加一个有参构造函数,参数只有name与salary,hideDay为当前日期。(粘贴代码)**
```````````````````````````````````````````````````````````````````````````````````````````````````
public Employee(String ming, double gongzi, int year, int month, int day)
{
name = ming;
salary = gongzi;
GregorianCalendar c= new GregorianCalendar(year, month - 1, day);
hireDay = c.getTime();
}
public Employee(String ming, double gongzi)
{
Employee(ming, gongzi, Calendar.getInstance().get(Calendar.YEAR),
Calendar.getInstance().get(Calendar.MONTH) + 1,
Calendar.getInstance().get(Calendar.DAY_OF_MONTH));
}
`````````````````````````````````````````````````````````````````````````````````````````````````````
**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.码云代码提交记录
**在码云的项目中,依次选择“统计-Commits历史-设置时间段”, 然后搜索并截图**
![](http://images2015.cnblogs.com/blog/1109984/201703/1109984-20170311103934717-1128177129.png)
4. PTA实验
题目集:jmu-Java-03-面向对象1-基础-封装继承 中的编程(5-1, 5-2, 5-3)
一定要有实验总结
A:本次实验练习了如何创建一个类,如何灵活使用 public & pravite 来使我们的程序更加清晰不易出错,还有eclipse中的快捷技巧创建 getter & setter,使我们更加有效率的完成代码,实验中也遇到了一些问题,例如每一个属性应该用什么类型,是否要用静态等等,都通过请教同学和老师得到了解决