java中有很多的关键字,他们的使用让Java语言变得更加灵活、易用,下面将介绍Java中最常用的几个关键字并说明其用法
this理解为:当前对象 或 当前正在创建的对象,可以调用的结构: 属性、方法;构造器
2.1 this调用属性、方法:
2.2 this调用构造器:
① 我们在类的构造器中,可以显式的使用"this(形参列表)"方式,调用本类中指定的其他构造器
② 构造器中不能通过"this(形参列表)"方式调用自己
③ 如果一个类中有n个构造器,则最多有 n - 1构造器中使用了"this(形参列表)"
④ 规定:"this(形参列表)" 必须声明在当前构造器的首行
⑤ 构造器内部,最多只能声明一个"this(形参列表)" ,用来调用其他的构造器
super 关键字可以理解为:父类的,可以用来调用的结构: 属性、方法、构造器
2.1super调用属性、方法:
与this关键字使用方式相同,只不过调用的是父类的属性
2.2 在子类的方法或构造器中使用
通过使用"super.属性"或"super.方法"的方式,显式的调用父类中声明的属性或方法。但是,通常情况下,我们习惯省略"super."
特殊情况
2.3 super调用构造器:

1 为了更好的实现项目中类的管理,提供包的概念 2 使用package声名类或接口所属的包,声名源文件的首航 3. 包,属于标识符,遵循标识符的命名规则、规范(xxxyyyzzz)、“见名知意” 4. 每"."一次,就代表一层文件目录。
[mvc设计模式]
java. lang--包含一些Java语言的核心类,如 String、Math、 integer、 System和Thread,提供常用功能
java. net--包含执行与网络相关的操作的类和接口
java.io--包含能提供多种输入/输出功能的类
java util--包含一些实用工具类,如定义系统特性、接口的集合框架类、使用与日期日历相关的函数。
java. text--包含了一些java格式化相关的类
java.sql--包含了java进行JDBC数据库编程的相关类/接口
java. awt--包含了构成抽象窗口工具集 (abstract window toolkits)的多个类,这些类被用来构建和管理应用程序的图形用户界面GU)。 B/S C/S
在源文件中显式的使用import结构导入指定包下的类、接口
声明在包的声明和类的声明之间
如果需要导入多个结构,则并列写出即可
可以使用"xxx.*"的方式,表示可以导入xxx包下的所结构
如果使用的类或接口是java.lang包下定义的,则可以省略import结构
如果使用的类或接口是本包下定义的,则可以省略import结构
如果在源文件中,使用了不同包下的同名的类,则必须至少一个类需要以全类名的方式显示。
使用"xxx.*"方式表明可以调用xxx包下的所结构。但是如果使用的是xxx子包下的结构,则仍需要显式导入
import static:导入指定类或接口中的静态结构:属性或方法。
主要用来修饰类的内部结构,如:属性、方法、代码块、内部类
2.1static 修饰属性:静态变量(或类属性)
属性是否使用static修饰,可分为:静态属性 vs 非静态属性(实例变量)
[static修饰属性的其他说明]
[静态属性巨款i:system.out:Math.pi] [静态变量内存解析]

2.2 static修饰方法:静态方法、类方法
| 静态方法 | 非静态方法 | |
|---|---|---|
| 类 | yes | no |
| 对象 | yes | yes |
使用的注意点:
2.3. 如何判定属性和方法应该使用static关键字:
关于属性:
关于方法:
class="prettyprint hljs java" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">class Circle{
-
- private double radius;
- private int id;//自动赋值
-
- public Circle(){
- id = init++;
- total++;
- }
-
- public Circle(double radius){
- this();
- // id = init++;
- // total++;
- this.radius = radius;
-
- }
-
- private static int total;//记录创建的圆的个数
- private static int init = 1001;//static声明的属性被所对象所共享
-
- public double findArea(){
- return 3.14 * radius * radius;
- }
-
- public double getRadius() {
- return radius;
- }
-
- public void setRadius(double radius) {
- this.radius = radius;
- }
-
- public int getId() {
- return id;
- }
-
- public static int getTotal() {
- return total;
- }
-
- }
-
[可以用来修饰:类 方法 变量]
2.1 abstract修饰类:抽象类
[注意点:]
class="prettyprint hljs java" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public abstract class Vehicle{
- public abstract double calcFuelEfficiency();//计算燃料效率的抽象方法
- public abstract double calcTripDistance();//计算行驶距离的抽象方法
- }
-
- public class Truck extend Vehicle{
- public double calcFuelEfficiency( ){//写出计算卡车的燃料效率的具体方法} p
- ublic double calcTripDistance( ){//写出计算卡车行驶距离的具体方法}
- }
3模板设计模式
将不确定的部分是由抽象类的方法用子类继承方式实现
class="prettyprint hljs java" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">abstract class Template{
-
- //计算某段代码执行所需要花费的时间
- public void spendTime(){
-
- long start = System.currentTimeMillis();
-
- this.code();//不确定的部分、易变的部分
-
- long end = System.currentTimeMillis();
-
- System.out.println("花费的时间为:" + (end - start));
-
- }
-
- public abstract void code();
-
- }
-
- class SubTemplate extends Template{
-
- @Override
- public void code() {
-
- for(int i = 2;i <= 1000;i++){
- boolean isFlag = true;
- for(int j = 2;j <= Math.sqrt(i);j++){
-
- if(i % j == 0){
- isFlag = false;
- break;
- }
- }
- if(isFlag){
- System.out.println(i);
- }
- }
-
- }
-
- }
-
如何定义接口:定义接口中的成员
接口中不能定义构造器的!意味着接口不可以实例化
Java开发中,接口通过让类去实现(implements)的方式来使用.
Java类可以实现多个接口 --->弥补了Java单继承性的局限性
格式:class AA extends BB implements CC,DD,EE
接口与接口之间可以继承,而且可以多继承
接口的具体使用,体现多态性
接口,实际上可以看做是一种规范