Study Number: 201521123116 Java Programming Third Week study summary

Source: Internet
Author: User

1. Summary of this Week's study

Learning to object-oriented, will learn a lot of fragmented concepts and knowledge. Try to use mind mapping to organize these fragmented concepts and knowledge. Use a pen or paper or the tools below to draw the points of knowledge you learned this Week.

Written work Q1: code reading
PublicClassTest1 {Privateint i =1;This line cannot be modifiedPrivatestatic int j = 2; public static  void main (string[] Args) {geti (); TEST1.GETJ (); } public static void geti ( Out.println (i); } public static void getj ( 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, There are several I in memory, a few j?

A: Cannot compile Through. System. out.println (i) will be Faulted. Static is a class, non- static belongs to an object, and each object may have a different value. Geti is a static method that cannot directly invoke a variable i without static Definition.

The modified Code:

PublicClassTest1 {Privateint i =1;This line cannot be modifiedPrivateStaticInt J =2;PublicStaticvoidMainstring[] Args) {geti (); TEST1GETJ ();Write Test1getj () static method}PublicStaticvoidTEST1GETJ () {TODO auto-generated Method Stub System.Out.println (j); }PublicStaticvoidGeti () {Test1 t=New Test1 ();Create a new Test1 object System.Out.println (t.i);Call its properties}PublicStaticvoidGETJ () {System.Out.println (j); }}
Q2. What is the use of the constructor function? 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?
答:(1)构造函数可以简单使用函数,得到我们所需的数据,例如求圆的周长或是面积,直接调用函数得出,不需要在代码中重复书写圆的周长和面积的公式,节约成本
(2)编写格式是private/public void/类型(int,String等等) 方法名{}
(3)有,系统默认有一个无参构造函数;若是只有带参函数,需要自己构建,系统只默认有无参构造函数

Q3.使用java.lang.Math类的函数时,为什么不需要new?如果new Math()会产生什么错误?分析原因?
答:Math是final类,不是static类。且构造函数被声明为privata,说明不能被外部调用,所以不能new出来一个新的对象。Math类的所有方法都是静态方法,使用Math.方法名就可以。 强行使用new会提示`The constructor Math() is not visible`此构造函数Math()不可见。

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

function overloading in the Java.lang.Math class:

答:重载即多个方法拥有相同的方法名但参数不同(参数类型不同,参数个数不同,参数顺序不同)。在一个类中定义多个构造函数,只要构造函数的参数不同,称为重载构造函数。根据重载函数中参数的类型,个数,顺序的不同来区分两个重载函数。
   < Span class= "hljs-title" >         

Q5.final modified variables cannot be changed, Why do the following code work correctly?

final int[] NUMBS= {1,2,3,4,5};NUMBS[2] = 1;
   < Span class= "hljs-title" >  

Q6: read the code Employeetest.java and Answer:

? 6.1 Why are the attributes private? what are the benefits of this design?

答:用private封装了类私有数据,不希望被外部的类访问。这样可以让用户无法直接存取,而必须通过管理者提供的操作方法,经过定义的流程才有可能存取私有数据。增强了保密性和安全性。

? 6.2 Employee adds an parameterless constructor to the class, invokes its argument constructor, sets the name to salary, 雇员1 3000 and Hireday is the date of the month 2017年3月5日 .

? 6.3 Add another parameter constructor for the Employee class, with only name and Salary,hideday as the current Date.

   < Span class= "hljs-keyword" >    

 q7. Write function public static Boolean ismondaytofriday ()
  Function: Judging if today is Monday to Friday, direct output such as "work time" and return true, otherwise the output "break time".
  tip: use localdatetime, DayOfWeek

public static boolean isMondayToFriday(){ LocalDateTime now = LocalDateTime.now(); DayOfWeek dayOfWeek = now.getDayOfWeek(); if(dayOfWeek!=DayOfWeek.SATURDAY && dayOfWeek!=DayOfWeek.SUNDAY){ System.out.println("上班时间"); return true; } else{ System.out.println("休息时间"); return false; } }
3. Code cloud codes Submission Record 4. PTA Experiment Summary
    • A: In the ide, you can use the program code auto-generation function to generate a value method, Click source Generate Constructors from Superclass -to Create an empty parameter constructor; click Source –> to Generate Getters and Setters automatically generate getter and setter methods to improve Efficiency.
    • When initializing classes and objects, the order of precedence should be static initialization blocks, initialization blocks, Constructors.
    • When called in main (), like math, call directly, but pay attention to the parameters, you want to call the function according to the corresponding parameters, otherwise it will be called wrong, not to achieve their own results
 

Study Number: 201521123116 Java Programming Third Week 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.