一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - 基于Java class對象說明、Java 靜態變量聲明和賦值說明(詳解)

基于Java class對象說明、Java 靜態變量聲明和賦值說明(詳解)

2020-11-04 16:47Java教程網 Java教程

下面小編就為大家帶來一篇基于Java class對象說明、Java 靜態變量聲明和賦值說明(詳解)。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

先看下JDK中的說明:

class="jb51code">
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
java.lang.Object
  java.lang.Class<T>
  
Instances of the class Class represent classes and interfaces in a running Java application. An enum is a kind of class and an annotation is a kind of interface. Every array also belongs to a class that is reflected as a Class object that is shared by all arrays with the same element type and number of dimensions. The primitive Java types (boolean, byte, char, short, int, long, float, and double), and the keyword void are also represented as Class objects.
 
Class has no public constructor. Instead Class objects are constructed automatically by the Java Virtual Machine as classes are loaded and by calls to the defineClass method in the class loader.
 
The following example uses a Class object to print the class name of an object:
 
   void printClassName(Object obj) {
     System.out.println("The class of " + obj +
              " is " + obj.getClass().getName());
   }
 
It is also possible to get the Class object for a named type (or for void) using a class literal.
 
  System.out.println("The name of class Foo is: "+Foo.class.getName());
 
在一個運行著的JAVA應用中,類的任何實例都可以用Class對象來指代,Class可以指代所有的類和接口。枚舉屬于類,注解屬于接口,均可以用Class指代。每個數組均屬于反射的Class對象,數組中的每個元素和維度也同樣擁有Class對象。Java基本類型(boolean, byte, char, short, int, long, float, and double)以及關鍵字void也都可以用Class指代。
 
Class類不存在構造函數,當類被加載過程中由JVM通過調用類加載器中的defineClass方法自動構造。
 
下面的例子是將一個對象通過Class對象打印出類名。
   void printClassName(Object obj) {
     System.out.println("The class of " + obj +
              " is " + obj.getClass().getName());
   }
通過class關鍵字指定類型也是可以得到Class對象的
  System.out.println("The name of class Foo is: "+Foo.class.getName());

上面內容總結下,就是Java中所有的對象以及基本類型都是可以用Class指代的。接下來看一個Demo。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/**
 *
 * 旨在測試Class對象和Instance之間的關系;
 * 旨在測試靜態變量的聲明和賦值過程;
 * @author zzy
 *
 */
public class ObjClass {
  private enum tmpEnum {A, B, C};
  public static void main(String[] args){
    int[] tmpArray = {1,2,3};
    Class classType;
    
    try {
      // 通過類名直接獲取Class對象,JVM中沒有加載。
      classType = InClass.class;
      System.out.println(".class: " + classType);
      System.out.println(".class finish.");
      
      // Java加載類
      classType = Class.forName("InClass");
      System.out.println("Class.forName: " + classType);
      System.out.println("Class.forName: finish.");
 
      
      // 創建實例
      InClass newClassType = new InClass();
      classType = newClassType.getClass();
      System.out.println("new Object.getClass: " + classType);
      System.out.println("new Object.getClass: finish.");
 
      // 數組對象
      classType = tmpArray.getClass();
      System.out.println("Array.getClass:" + classType.getSimpleName());
      System.out.println("Array.getClass: finish.");
      
      // 枚舉對象
      classType = tmpEnum.class;
      System.out.println("enum.class:" + classType);
      System.out.println("enum.class: finish.");
 
    } catch (ClassNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }     
  
}
class InClass{
  // 對靜態變量聲明之前賦值
  {
    staticPara = 10;
  }
  public static int staticPara;
  
  // 構造函數
  public InClass(){
    System.out.println("construction...");
  }
  
  // 靜態代碼塊
  static {
    System.out.println("static function...");
  }
  
  // 靜態變量賦值
  
    staticPara = 20;
  }
  
  {
    System.out.println("normal function, staticPara:" + staticPara);
  }
  
  // 靜態變量賦值
  {
    staticPara = 30;
  
}

輸出結果:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
.class: class InClass
.class finish.
static function...
Class.forName: class InClass
Class.forName: finish.
normal function, staticPara:20
construction...
new Object.getClass: class InClass
new Object.getClass: finish.
Array.getClass:int[]
Array.getClass: finish.
enum.class:class ObjClass$tmpEnum
enum.class: finish.

說明:

1.  .class方式獲取類文件的Class對象,并不需要加載JVM中。

2.  Class.forName的方式JVM會加載類,同時會編譯。 如此,類中的靜態代碼塊就會被執行。

3.  創建實例過程中(已經加載至JVM中,也就是說編譯過了),調用構造函數,并執行非靜態代碼塊。

4.  靜態代碼對于變量的聲明和賦值順序是沒有影響的(編譯過程處理),所以結果是20。

5. 數據對象和枚舉對象也是可以被Class對象指定的。

以上這篇基于Java class對象說明、Java 靜態變量聲明和賦值說明(詳解)就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国内精品视频一区二区三区八戒 | 91se精品免费观看 | 韩国美女豪爽一级毛片 | 91桃色视频在线观看 | 亚洲AV 无码AV 中文字幕 | 国产思妍小仙女一二区 | 天堂资源8中文最新版 | 亚洲人成网站在线观看青青 | 成年人免费看的视频 | 高清在线观看免费入口 | 91香蕉视频在线观看 | 三级午夜宅宅伦不卡在线 | 亚洲成在人线久久综合 | 天天操天天爽天天射 | 国产成人激情 | 1313午夜精品久久午夜片 | 日韩精品一区二区三区老鸭窝 | 免费观看俄罗斯特黄特色 | 国产麻豆精品视频 | 国内9lporm自拍视频区 | 欧美高清在线精品一区二区不卡 | 校花在公车上被内射好舒服 | 国产欧美日韩不卡一区二区三区 | 亚洲AV无码国产精品色在线看 | 高h辣h双处全是肉军婚 | 成年女人免费 | 欧美日韩视频在线一区二区 | yellow在线 | futa百合高肉全h | 亚洲图片一区二区三区 | 国产成人无精品久久久久国语 | 9999热视频| 国内精品国语自产拍在线观看55 | 精精国产www视频在线观看免费 | 天天干天天色综合网 | 好大好硬好湿好紧h | 视频免费视频观看网站 | 五月天婷婷亚洲 | 无码人妻丰满熟妇啪啪网不卡 | 和两个男人玩3p好爽视频 | 国产永久在线观看 |