Java Modifiers(java修饰符):

Like other languages, it is possible to modify classes, methods, etc., by using modifiers. There are two categories of modifiers(两类修饰):

  • Access Modifiers(可访问): default, public , protected, private

  • Non-access Modifiers(不可访问): final, abstract, strictfp

We will be looking into more details about modifiers in the next section.

Java Variables(变量):

We would see following type of variables in Java:

  • Local Variables(局部变量)
  • Class Variables (Static Variables) 类变量-静态变量
  • Instance Variables (Non-static variables) 实例变量-非静态变量

Java Arrays(数组):

Arrays are objects that store multiple variables of the same type. However, an array itself is an object on the heap(堆). We will look into how to declare, construct and initialize(初始化) in the upcoming chapters.

Java Enums(枚举):

Enums were introduced in java 5.0. Enums restrict a variable to have one of only a few predefined values(预定义的值). The values in this enumerated list are called enums.

With the use of enums it is possible to reduce the number of bugs in your code.

For example, if we consider an application for a fresh juice shop, it would be possible to restrict the glass size to small, medium and large. This would make sure that it would not allow anyone to order any size other than the small, medium or large.

Example:

复制代码
 1 class FreshJuice {
 2 
 3    enum FreshJuiceSize{ SMALL, MEDIUM, LARGE }
 4    FreshJuiceSize size;
 5 }
 6 
 7 public class FreshJuiceTest {
 8 
 9    public static void main(String args[]){
10       FreshJuice juice = new FreshJuice();
11       juice.size = FreshJuice. FreshJuiceSize.MEDIUM ;
12       System.out.println("Size: " + juice.size);
13    }
14 }
复制代码

 

 

Above example will produce the following result:

Size: MEDIUM

Note: enums can be declared as their own or inside a class. Methods, variables, constructors can be defined inside enums as well.

Inheritance(继承):

In Java, classes can be derived from classes. Basically if you need to create a new class and here is already a class that has some of the code you require, then it is possible to derive(源于,得自,起源) your new class from the already existing code.

This concept allows you to reuse the fields and methods of the existing class without having to rewrite the code in a new class. In this scenario the existing class is called the superclass and the derived class is called the subclass.

Interfaces(接口):

In Java language, an interface can be defined as a contract between objects on how to communicate with each other. Interfaces play a vital role when it comes to the concept of inheritance.

An interface defines the methods, a deriving class(subclass) should use. But the implementation of the methods is totally up to the subclass.

posted on   Linuxroot  阅读(250)  评论(0编辑  收藏  举报
编辑推荐:
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
阅读排行:
· 《HelloGitHub》第 108 期
· Windows桌面应用自动更新解决方案SharpUpdater5发布
· 我的家庭实验室服务器集群硬件清单
· Supergateway:MCP服务器的远程调试与集成工具
· C# 13 中的新增功能实操
点击右上角即可分享
微信分享提示