<java基础学习>RE 基础语法(2)

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.

原文地址:https://www.cnblogs.com/linuxroot/p/3558340.html