linkedin-skill-assessments Java 检定 static import

前言
在更新Linkedkin 个人档案的时候
偶然发现他有技术检定测验
如果总成绩在前30%,会发给你技术认证徽章
如果第一次没考过可以重考
如果第二次也没考过就要"等半年!!!" 才能考第二次
本文章为实际考题
个人感觉题目有点像OCA的考题,蛮适合正在学习Java观念
或工作一段时间但没深入研究Java的
如果有需要的人还请自行服用。

Quesion:
Normally , to acess a static member of a class such as Math.PI,
you would need to specify the class "Math". What would be the best way to allow you yo use simple "PI" in your code ?

A. Add a static import .
B. Declare local copies of the constants in your code.
C. Put the static members in an interface and inherit from that interface.
D. This cannot bedone . You must always qualify references to static memberss with the class frome which they came.

answer: A
解析:
这题目大家第一次看可能看不太懂,他的意思是如果我想直接使用Math中的PI常数,要怎么做呢?

我们可以点进去Math这个class,可以找到PI前面宣告了static
 (话说小编只能背到3.1415926...国小老师不知道为什么要我们背,跟弟子规一样未解之谜)

    /**     * The {@code double} value that is closer than any other to     * <i>pi</i>, the ratio of the circumference of a circle to its     * diameter.     */    public static final double PI = 3.14159265358979323846;

而一般ide在引入static参数时,会这样建议,习惯上也会这样用:

    public static void main(String[] args) {        System.out.println(Math.PI); //class.variable        //印出 3.141592653589793}

但其实也可以这样import,但比较少见

import static java.lang.Math.*;class Main {    public static void main(String[] args) {        System.out.println(PI); //不须加上Math,因为在上面已经import        //印出 3.141592653589793    }}

关于作者: 网站小编

码农网专注IT技术教程资源分享平台,学习资源下载网站,58码农网包含计算机技术、网站程序源码下载、编程技术论坛、互联网资源下载等产品服务,提供原创、优质、完整内容的专业码农交流分享平台。

热门文章