201521123085 Java Programming 3rd week of study summary

Source: Internet
Author: User

1. Summary of this week's study2. Written work1. Code reading
PublicClassTest1 {Privateint i =1;This line cannot be modifiedPrivatestatic int j = 2; public static  void main (string[] args) {Geti (); TEST1GETJ (); } public static void geti ( Out.println (i); } public static void GETJ ( Out.println (j); }} 
1. Can the above code be compiled and passed? Where does it go wrong? Trying to correct? If you create 3 Test1 objects, there are several I in memory, a few J? Please analyze the reason?

答:(1)不能编译成功。 Test1getj();与System.out.println(i);显示编译错误,因为字段i为非静态,不能进行静态引用。 将public static void geti()改为public void geti()。

    (2)有3个i,1个j。

2. What is the function of the constructor? What is the writing format? If a class does not write a constructor, does it have a constructor function? If a class has only a constructor with parameters, does it have a default constructor with no arguments?

答:构造函数的作用:给对象中的属性进行初始化,编写格式为:构造函数名与类名相同即可,如果一个类不写构造函数,它有构造函数,如果一个类中只有带参的构造函数,它没有不带参数的默认构造函数。

3. Why do I need new when I use a function of the Java.lang.Math class? What happens if new Math () generates errors? Analyze the cause?

答:(1)因为Math类所含的方法以及变量都是静态的,此时无需生成对象就可以调用静态方法和属性,所以不需要new;

    (2)如果new Math()则会出现The constructor Math() is not visible;

4. What is an overload? What is a constructor overload? In the Java.lang.Math class cited out 1 examples of function overloading? How can I distinguish between two overloaded functions?

答:重载,简单来说就是函数活着方法有相同的名称,但是参数列表不相同的情况。可使函数、运算符等处理不同类型数据或接受不同个数的参数。

Function names can be the same, but feature labels must be different. The function names are used to determine the difference between functions, and the feature labels are used to determine the difference between overloaded functions. At the same time, the order of the parameters, different types can also determine the difference between overloaded functions.

5.final modified variables cannot be changed, why do the following code work correctly?
final int[] NUMBS= {1,2,3,4,5};NUMBS[2] = 1;

答:因为数组是对象,用final定义数组对象,只是数组这个对象不可变,即该数组名不能指向其他的数组,但是里面的数值还是可以改变的

6. Read the code Employeetest.java and answer:1. Why are the attributes private? What are the benefits of this design?

答:属性均为private可以防止外部直接修改变量,使变量不被外部的类访问,起到保护作用,保证了数据的安全性。

2. Add an parameterless constructor for the employee class, call its parameter constructor, set name to employee 1, salary to Hireday, and the date of the month is March 5, 2017. (Paste code)

For:

Public Employee () {

This ("Employee 1", 3000.0, 2017, 3, 5);

}

3. Add an additional parameter constructor for the employee class, with only name and Salary,hideday as the current date. (Paste code)

For:

Public Employee (String N, double s) {
Name=n;
Salary=s;
GregorianCalendar Calendar = new GregorianCalendar
(Gregoriancalendar.year,gregoriancalendar.month-1,gregoriancalendar.day_of_month);
Hireday = Calendar.gettime ();
}

7. Write function public static Boolean ismondaytofriday ()
Function: Judging today if it is Monday to Friday, direct output such as "work Time" and return true, otherwise output "break time".

Tip: Use LocalDateTime, DayOfWeek

Resources:
JAVA 8: Robust, easy-to-use time-date api-importnew.pdf
Java8datetimetest.java

答:public static Boolean Ismondaytofriday ()

{

LocalDateTime Localdatetime=localdatetime.now ();

int Day=localdatetime.getdayofweek (). GetValue ();

if (day>=1&&day<=5) {

System.out.println ("Working hours");

return true;

}
else {
System.out.println ("Rest Time");
return false;
}

}

201521123085 Java Programming 3rd week of study summary

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.