201521123088 the third week of Java Program Design Study summary

Source: Internet
Author: User
Tags set time

1. Summary of this week's study

This week, we learned about Java encapsulation, which is to privatize properties and provide public access to private properties.

2. Written work
  1. Code Reading

     Public classTest1 {Private inti = 1;//This line cannot be modified    Private Static intj = 2;  Public Static voidMain (string[] args) {Geti ();    TEST1GETJ (); }     Public Static voidGeti () {System.out.println (i); }     Public Static voidGetj () {System.out.println (j); }}

    Can the above code be compiled and passed? Where does it go wrong? Trying to correct?
    If you create 3 Test1 objects, how many I are in memory, and how many J? Please analyze the reason?

    • A: cannot compile through. System.out.println (i); there will be an error. Because I it is not static type. If you create 3 test1 objects, there will be 3 I, a J in memory, because I is non-static and J is static. Static variables can be shared by all instance objects of a class, and there is only one copy of the storage space, and non-static variables allocate storage space for each object.

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?

    • Answer: Initialize the class and simplify the code. Write the contents of the public class name () {function}. If you do not write a constructor, the system defaults to a non-parametric construction method.

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

    • Answer: Math is the final class, not the static class, and using new will prompt the constructor Math() is not visible. Because the member variables of the math class and their methods are static , they do not need a new

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?

    • A: overloading refers to a situation where a function or method has the same name, but the argument list is not the same. Example:
      Static double abs (double A)

      For distinguishing two overloaded functions, the key is to differentiate by the difference in the parameter list

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;
    • A: Because the final modifier refers to a type variable, the value (reference) of the variable cannot be modified, but the value of the object that the variable points to can be modified.

6. Read the code Employeetest.java and answer:

① Why are the attributes private? What are the benefits of this design?

    • A: Because the property is to be kept secret, so to protect the property, and only set to private, it will not be arbitrarily called or changed, with private encapsulation of the class data, do not want to be accessed by external classes. Enhanced confidentiality and security.

Employee adds an parameterless constructor to the class, invokes its argument constructor, sets the name to salary, 雇员1 3000 and Hireday for the month and day 2017年3月5日 . (Paste code)

Public This ("Employee 1", 3000.0, 3, 5);} 

Employee Adds an argument constructor to the class, with only name and Salary,hideday as the current date. (Paste code)

 Public Employee (String name,double  salary) {getName (); Getsalary ();}

7. Writing functionspublic 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

    • For:

 Public Static Boolean Ismondaytofriday () {    = localdatetime.now ();     = Now.getdayofweek ();    System.out.println (DayOfWeek);     if (Dayofweek!=dayofweek.saturday && dayofweek!=dayofweek.sunday)    {        System.out.println ("office Hours");         return true ;    }    System.out.println ("Rest Time");     return false ;}

3. Code Cloud Codes Submission record
    • In the Code cloud Project, select statistics-commits history-set time period, and then search for and
4. PTA Experiment

Experiment Summary:

      • Learn how to create classes, familiarize yourself with commonly used classes to write parameters, parameterless constructors, and familiarize yourself with classes, and note that toString andprintln are familiar with classes and objects in experiments to differentiate between overloaded two objects

201521123088 the third week of Java Program Design Study summary

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.