初始化vs實例化
阿新 • • 發佈:2018-02-21
對象 () dir 加載機制 log tin tar def acl https://cs.brown.edu/courses/cs015/docs/HelpfulHandout.pdf
對Declaration, Instantiation 和Initialization一直都存在疑惑,直到看了Brown大學的這個文檔。
對象(也就是類)被實例化,變量被初始化。
實例化例子:
new Guitarist(); 這就是實例化了Guitarist
初始化例子:
Guitarist guitarist = new Guitarist();
_guitarist = new Guitarist();
int num = 3;
這樣,我們就初始化了實例變量_guitarist 和局部變量guitarist,num。
初始化一個變量就是指給一個變量賦初始值,使其不為空null。
聲明一個變量和初始化不同,以下是初始化的例子
privateGuitarist_guitarist;_guitarist is null, or has no value //_guitarist 是空值或者沒有值
Guitaristguitarist;again, guitarist is null.//guitarist 值為空
初始化一個變量
intnum=5;here, num is given an initial value of 5. //num被賦予的值為5
Remember that “=” means assignment or “gets,” not “is equal to”//記住”=“是賦值,不是等於
實例化
_guitarist=newGuitarist();this is instantiating an object. We create a new instance of a Guitaristand initialize _guitaristto be the value of the new Guitarist
//這是實例化一個對象
既然說到了初始化和實例化,那我們再來看一下ClassLoader,因為類加載機制也是一個容易使人迷惑的概念。
https://docs.oracle.com/javase/7/docs/api/java/lang/ClassLoader.html
java.lang
Class ClassLoader
public abstract class ClassLoader extends Object //Object是所有類的基類 A class loader is an object that is responsible for loading classes. The class ClassLoader is an abstract class. Given the binary name of a class, a class loader should attempt to locate or generate data that constitutes a definition for the class. A typical strategy is to transform the name into a file name and then read a "class file" of that name from a file system. //類加載器就是一個負責加載類的對象。ClassLoader是一個抽象類,給定類的二進制名,類加載器應能嘗試定位或者生成構成類的定義的數據。一個經典的策略是將二進制名轉換為文件名然後從文件系統中讀取該名稱的類文件。
- java.lang.Object
- java.lang.ClassLoader
- Direct Known Subclasses:
public abstract class ClassLoader extends Object //Object是所有類的基類 A class loader is an object that is responsible for loading classes. The class ClassLoader is an abstract class. Given the binary name of a class, a class loader should attempt to locate or generate data that constitutes a definition for the class. A typical strategy is to transform the name into a file name and then read a "class file" of that name from a file system. //類加載器就是一個負責加載類的對象。ClassLoader是一個抽象類,給定類的二進制名,類加載器應能嘗試定位或者生成構成類的定義的數據。一個經典的策略是將二進制名轉換為文件名然後從文件系統中讀取該名稱的類文件。
初始化vs實例化