首页 > 编程语言 >Java官方笔记11包

Java官方笔记11包

时间:2023-06-17 16:22:34浏览次数:55  
标签:11 java package import 笔记 graphics Java example Rectangle

Packages

Definition: A package is a grouping of related types providing access protection and name space management. Note that types refers to classes, interfaces, enumerations, and annotation types. Enumerations and annotation types are special kinds of classes and interfaces, respectively, so types are often referred to in this section simply as classes and interfaces.

创建

//in the Draggable.java file
package graphics;
public interface Draggable {
    . . .
}

//in the Graphic.java file
package graphics;
public abstract class Graphic {
    . . .
}

//in the Circle.java file
package graphics;
public class Circle extends Graphic
    implements Draggable {
    . . .
}

//in the Rectangle.java file
package graphics;
public class Rectangle extends Graphic
    implements Draggable {
    . . .
}

//in the Point.java file
package graphics;
public class Point extends Graphic
    implements Draggable {
    . . .
}

//in the Line.java file
package graphics;
public class Line extends Graphic
    implements Draggable {
    . . .
}

文件顶部第一行,每个文件只能有1个package关键字。

命名

Companies use their reversed Internet domain name to begin their package names—for example, com.example.mypackage for a package named mypackage created by a programmer at example.com.

Name collisions that occur within a single company need to be handled by convention within that company, perhaps by including the region or the project name after the company name (for example, com.example.region.mypackage).

Packages in the Java language itself begin with java. or javax.

如果按此规则命名出现不合法,使用下划线:

  • hyphenated-name.example.org,org.example.hyphenated_name

  • example.int,int_.example

  • 123name.example.com,com.example._123name

导包

To use a public package member from outside its package, you must do one of the following:

  • Refer to the member by its fully qualified name

    graphics.Rectangle myRect = new graphics.Rectangle();
    
  • Import the package member

    import graphics.Rectangle;
    
    Rectangle myRectangle = new Rectangle();
    
  • Import the member's entire package

    import graphics.*;
    
    Circle myCircle = new Circle();
    Rectangle myRectangle = new Rectangle();
    

Java会默认导2个包

  1. the java.lang package and
  2. the current package (the package for the current file).

Java导父不导子

Importing java.awt.* imports all of the types in the java.awt package, but it does not import java.awt.colorjava.awt.font, or any other java.awt.xxxx packages. If you plan to use the classes and other types in java.awt.color as well as those in java.awt, you must import both packages with all their files:

import java.awt.*;
import java.awt.color.*;

命名冲突:如果导的包,类名相同,那么必须加上完整包路径进行区分

graphics.Rectangle rect;

static import:import the static members

import static java.lang.Math.PI;

参考资料:

Packages https://dev.java/learn/packages/

标签:11,java,package,import,笔记,graphics,Java,example,Rectangle
From: https://www.cnblogs.com/df888/p/17487619.html

相关文章

  • java中 如何在文本中筛选出汉字
    在Java中,使用正则表达式来筛选出文本中的汉字。下面是一种方法:importjava.util.regex.Matcher;importjava.util.regex.Pattern;publicclassMain{publicstaticvoidmain(String[]args){Stringtext="Hello你好!Thisisatest文本。";/......
  • java中 如何在文本中筛选出汉字
    在Java中,使用正则表达式来筛选出文本中的汉字。下面是一种方法:importjava.util.regex.Matcher;importjava.util.regex.Pattern;publicclassMain{publicstaticvoidmain(String[]args){Stringtext="Hello你好!Thisisatest文本。";/......
  • SpringBoot学习笔记
    新建SpringBoot项目阿里云地址:https://start.aliyun.com异常消息处理//1.自定义异常类,继承RuntimeExceptionpublicclassMyExceptionextendsRuntimeException{publicMyException(){}}//2.定义全局异常类@RestControllerAdvicepublicclassGloabExcept......
  • Python3网络爬虫开发实战阅读笔记
    基本库的使用网络请求库urllib(HTTP/1.1)Python自带请求库,繁琐基础使用:略requests(HTTP/1.1)Python常用第三方请求库,便捷基础使用:略httpx(HTTP/2.0)Python第三方库,支持HTTP/2.0,支持异步请求,支持Python的async请求模式pipinstall'httpx[http2]'基础使用:与requests相似,默认......
  • Django学习笔记
    1.常用命令创建项目:django-adminstartproject项目名创建APP(进入工程目录):pythonmanage.pystartapp网站名创建库表(进入工程目录):pythonmanage.pymakemigrations执行库表建立(进入工程目录):pythonmanage.pymigrate启动运动:pythonmanage.pyrunserver模板渲染嵌入......
  • JavaScript之Object.defineProperty()
    1.对象的定义与赋值经常使用的定义与赋值方法obj.prop=value或者obj['prop']=valueletPerson={};Person.name="Jack";Person["gender"]="female";console.log(Person.name);//Jackconsole.log(Person.gender);//femaleconsole.log(Pers......
  • 社会学笔记与思维导图
    学科意识社会学的历史社会学的想象力社会学的历史古典社会学滕尼斯李约瑟之谜马克思·韦伯中国的入世当代社会学中国的社会学研究方法文化个人主义规范社会结构人的社会化......
  • Java官方笔记10注解
    注解注解的作用:Informationforthecompiler—Annotationscanbeusedbythecompilertodetecterrorsorsuppresswarnings.Compile-timeanddeployment-timeprocessing—Softwaretoolscanprocessannotationinformationtogeneratecode,XMLfiles,ands......
  • 计算机底层的秘密读书笔记之二
    计算机底层的秘密读书笔记之二内存部分内存部分之前自己看过很多资料了:主要是虚拟内存以及TLB相关的内容本书的角度与之前角度都不太一样,更加新颖一些.这次总结想倒着来写.1.SSD的带宽和延迟都比较好了,但是是无法代替内存的内存的寻址可以到字节,然后都是按照内......
  • Java流程控制05:Switch选择结构
    多选择结构还有一个实现方式就是switchcase语句。switchcase语句判断一个变量与一系列值中某个值是否相等,每个值称为一个分支。switch(exception){casevalue://语句break;//可选casevalue:......