導航:首頁 > 編程語言 > java屬性欄位

java屬性欄位

發布時間:2023-01-29 09:23:47

java 可以動態創建 屬性欄位么

可以的,你用 javassist, cglib 或者更為底層的工具 ASM 都是可以。

ASM 的話相對復雜一些,參考代碼:

下面這個是用 ASM 工具為 Student 類添加一個 public String 類型的 address 屬性:

1,需要添加屬性的原始類:Student.java

Java code?

public class Student {
private int age;
private String name;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

2,添加屬性的適配器:AddFieldAdapter.java

Java code?

import org.objectweb.asm.ClassAdapter;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.FieldVisitor;

public class AddFieldAdapter extends ClassAdapter {
private int accessModifier;
private String name;
private String desc;
private boolean isFieldPresent;

public AddFieldAdapter(ClassVisitor cv, int accessModifier, String name, String desc) {
super(cv);
this.accessModifier = accessModifier;
this.name = name;
this.desc = desc;
}

@Override
public FieldVisitor visitField(int access, String name, String desc,
String signature, Object value) {
if (name.equals(this.name)) {
isFieldPresent = true;
}
return cv.visitField(access, name, desc, signature, value);
}

@Override
public void visitEnd() {
if (!isFieldPresent) {
FieldVisitor fv = cv.visitField(accessModifier, name, desc, null, null);
if (fv != null) {
fv.visitEnd();
}
}
cv.visitEnd();
}
}

3,添加屬性的工具 AddField.java

Java code?

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.objectweb.asm.ClassAdapter;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Opcodes;

public class AddField {

private Class clazz = null;
private ClassReader cr = null;
private ClassWriter cw = null;
private ClassAdapter ca = null;
private File classFile = null;

private final static String CLASS_FILE_SUFFIX = ".class";

public AddField(Class clazz) {
this.clazz = clazz;
}

/**
* 添加一個 public 的類成員
* @param fieldName 類成員名
* @param fieldDesc 類成員類型描述
*/
public void addPublicField(String fieldName, String fieldDesc) {
if(cr == null) {
try {
cr = new ClassReader(clazz.getCanonicalName());
} catch (IOException e) {
e.printStackTrace();
}
cw = new ClassWriter(cr, ClassWriter.COMPUTE_MAXS);
}
if(ca == null) {
ca = new AddFieldAdapter(cw, Opcodes.ACC_PUBLIC, fieldName, fieldDesc);
} else {
ca = new AddFieldAdapter(ca, Opcodes.ACC_PUBLIC, fieldName, fieldDesc);
}
}

/**
* 將位元組碼寫入類的 .class 文件
*
*/
public void writeByteCode() {
cr.accept(ca, ClassReader.SKIP_DEBUG);
byte[] bys = cw.toByteArray();
OutputStream os = null;
try {
os = new FileOutputStream(getFile());
os.write(bys);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

/**
* 獲得類文件的 File 對象
* @return
*/
private File getFile() {
if(classFile == null) {
StringBuffer sb = new StringBuffer();
sb.append(clazz.getResource("/"))
.append(clazz.getCanonicalName().replace(".", File.separator))
.append(CLASS_FILE_SUFFIX);
classFile = new File(sb.substring(6));
}
return classFile;
}
}

❷ java中的屬性和C#中的欄位有何練習和區別

C#裡面的欄位類似於Java中的屬性,也可以把它叫做類的成員變數,在C#中,屬性有特殊的含義。
在C#中,
public string aa;
aa就是欄位,它不足的就是無法限制值的輸入輸出,
public string aa
{
get;
set;
}
這時的aa就是屬性,它可以通過get來限制讀取的值,比如如果對像為空,就NEW一個,
通過set可以有效的控制值的輸入
set get 二者可為一個,也可以兩個都有,只有get時就是只讀,只有set時就是只寫,通常是兩都都有.

❸ java中類定義的屬性是什麼意思

java是面向對象編程的語言

什麼叫對象?

publicclassPerson{
privatestringname;
}
什麼叫類?
Person是定義了一個人的java類-----它是一類事物的統稱
既然是人了,那就應該有名字吧,name就是person的一個屬性,比如還有年齡啊,性別啊什麼的
實例化對象---就是通過類(Person)具體拿到某一個人
比如Personp=newPerson("張三")

張三就是Person中具體的一個對象
這就是面相對象編程明白吧

❹ java中的欄位和屬性的區別

一個是變數(String s),一個是成員變數的屬性(String name=user.getUsername())
欄位可以有方法( s.equals(user.getUsername())

但是屬性是欄位的時候也可以有欄位的方法

❺ java "欄位"啥意思

public final static InputStream in = nullInputStream();

nullInputStream是這樣實現的:
private static InputStream nullInputStream() throws NullPointerException {
if (currentTimeMillis() > 0)
return null;
throw new NullPointerException();
}

他不是返回null,就是拋出異常,如何初始化in呢?
解答:
看了一下java.lang.System的源代碼.
System類里有大量的native方法,是調用本地代碼的,這些代碼很可能是由虛擬機來調用的.
System類的開頭有一段:
static {
registerNatives();
}
這段代碼會在虛擬機啟動的時候就執行,它在虛擬機里注冊System需要使用的一些本地代碼
比如:
private static native Properties initProperties(Properties props);
private static native void setOut0(PrintStream out);
在windows下的話,它就告訴虛擬機到哪個dll文件里去找相應的實現

>然而,我知道out是一個PrintStream的對象,但我查看了有關的原代碼:public final static PrintStream out = nullPrintStream();
>public final static InputStream in = nullInputStream();
在nullInputStream()方法里有注釋解釋為什麼會設置為空:

/**
* The following two methods exist because in, out, and err must be
* initialized to null. The compiler, however, cannot be permitted to
* inline access to them, since they are later set to more sensible values
* by initializeSystemClass().
*/
private static InputStream nullInputStream() throws NullPointerException {
if (currentTimeMillis() > 0)
return null;
throw new NullPointerException();
}
也就說in, out, and err 初始化為null,然後會在後來由initializeSystemClass()方法類初始化成有意義的值
/**
* Initialize the system class. Called after thread initialization.
*/
private static void initializeSystemClass() {
props = new Properties();
initProperties(props);
sun.misc.Version.init();
FileInputStream fdIn = new FileInputStream(FileDescriptor.in);
FileOutputStream fdOut = new FileOutputStream(FileDescriptor.out);
FileOutputStream fdErr = new FileOutputStream(FileDescriptor.err);
setIn0(new BufferedInputStream(fdIn)); !!!
setOut0(new PrintStream(new BufferedOutputStream(fdOut, 128), true)); !!!
setErr0(new PrintStream(new BufferedOutputStream(fdErr, 128), true)); !!!

// Enough of the world is now in place that we can risk
// initializing the logging configuration.
try {
java.util.logging.LogManager.getLogManager().readConfiguration();
} catch (Exception ex) {
// System.err.println("Can′t read logging configuration:");
// ex.printStackTrace();
}

// Load the zip library now in order to keep java.util.zip.ZipFile
// from trying to use itself to load this library later.
loadLibrary("zip");

// Subsystems that are invoked ring initialization can invoke
// sun.misc.VM.isBooted() in order to avoid doing things that should
// wait until the application class loader has been set up.
sun.misc.VM.booted();
}
in,out,err就是在以上方法以下三條語句里初始化的.
setIn0(new BufferedInputStream(fdIn)); !!!
setOut0(new PrintStream(new BufferedOutputStream(fdOut, 128), true)); !!!
setErr0(new PrintStream(new BufferedOutputStream(fdErr, 128), true)); !!!

private static native void setIn0(InputStream in);
~~~~~~~
這是個native函數,是前面registerNatives()的時候注冊了的.這個函數應該是把實際連接到輸入輸出設備的句柄傳給虛擬機並賦值給in,out,err

至於:
>InputStream是個抽象的類,怎麼能使用char=(char)System.in.read()讀入一個字元
我想你還沒有明白什麼是面向對象.
看看下面代碼,我用OutputStream(也是抽象類,跟InputStream對應的輸出類)以方便演示:
import java.io.IOException;
import java.io.OutputStream;
public class HelloWorld {

public OutputStream out=null;

public void setOutputStream(OutputStream out){
this.out=out;
}

public static void main(String[] args) throws IOException{
HelloWorld h=new HelloWorld();
PrintStream myOut=System.out;//System.out是一個PrintStream
h.setOutputStream(myOut);
h.out.write("hello,world".getBytes());//一般沒人這么寫的
}
}
以上代碼執行後會輸出hello,world
h.out是OutputStream,也是個抽象類,為什麼能write(o)呢?
因為PrintStream是OutputStream的子類,所以能被"當作"OutputStream傳給h.setOutputStream(myOut);
h.out.write執行的時候實際上是調用這個傳進來的PrintStream實例的write方法
同樣System.in和out肯定也是在initializeSystemClass()的時候被賦予了一個實際的可用的子類
要能體會到面向對象的好處,就要逐漸適應"對介面編程"的思想,相同介面的對象可以根據需要方便的替換.
比如,我剛才傳了一個PrintStream,因此HelloWorld輸出到了屏幕上. 我如果傳給OutputStream的另一個子類FileOutputStream,就會輸出到文件里

>還有為什麼不是說字元流:writer和reader一般用於UniCode的讀寫嗎?為什麼鍵盤的輸入用reader類呢?
不知道你在哪裡看到說writer和reader一般用於UniCode的讀寫

❻ java 怎麼定義屬性

定義屬性的語法格式如下:
[修飾符] 屬性類型 屬性名 [=默認值]

屬性語法格式的詳細說明如下:
1、修飾符:修飾符可以省略,也可以是public、protected、private、static、final,其中public、protected、private三個最多隻能出現其中之一,可以與static、final組合起來修飾屬性。
2、屬性類型:屬性類型可以是Java語言允許的任何數據類型,包括基本類型和現在介紹的引用類型。
3、屬性名:屬性名則只要是一個合法的標識符即可,但這只是從語法角度來說的;如果從程序可讀性角度來看,屬性名應該由一個或多個有意義的單詞連綴而成,第一個單詞首字母小寫,後面每個單詞首字母大寫,其他字母全部小寫,單詞與單詞之間不需使用任何分隔符。
4、默認值:定義屬性還可以定義一個可選的默認值。
提示:屬性是一種比較傳統、也比較符合漢語習慣的說法,在Java的官方說法里,屬性被稱為Field,因此有的地方也把屬性翻譯為欄位。

定義方法的語法格式如下:
[修飾符] 方法返回值類型 方法么(形參列表)
{
//由零條到多條可執行性語句組成的方法體
}

❼ java中欄位是什麼怎麼定義欄位欄位是變數嗎和變數 方法 屬性有什麼區別欄位有返回值嗎。。。

欄位就是成員變數,注意是成員變數,不是局部變數。欄位也就是屬性了,一個類的屬性。
只是叫法不同,java的API中一般叫成員變數就欄位,有時也叫域。而我們一般的編程的時候,就欄位叫成員變數

補------------
out就是一個欄位,也就是是System類的一個成員變數。只不過個這成員變數是一個對象,也就是out是一個 PrintStream類的對象。這就是在一個類里執有其它類的對象的用法

❽ JAVA里的欄位是什麼意思

sm yisd什麼欄位?

❾ java中的屬性是什麼意思

屬性就是 這個類定義的一個成員 變數 舉個例子 就是一個人的胳膊和腿歡迎來我們的java世界ourjavasky 針對層次不同量身學習java技術 www.ourjavasky.cn

❿ Java中的屬性和欄位有什麼區別

區別在於java中屬性是要有get、set方法的,不是所有欄位都有get、set方法

閱讀全文

與java屬性欄位相關的資料

熱點內容
蘋果平板如何開啟隱私單個app 瀏覽:704
空調壓縮機一開就停止 瀏覽:528
如何下載虎牙app 瀏覽:847
日語年號的演算法 瀏覽:955
dev裡面的編譯日誌咋調出來 瀏覽:298
php函數引用返回 瀏覽:816
文件夾和文件夾的創建 瀏覽:259
香港加密貨幣牌照 瀏覽:838
程序員鼓勵自己的代碼 瀏覽:393
計算機網路原理pdf 瀏覽:752
吃雞國際體驗服為什麼伺服器繁忙 瀏覽:94
php中sleep 瀏覽:490
vr怎麼看視頻演算法 瀏覽:86
手機app如何申報個人所得稅零申報 瀏覽:694
如何截獲手機app連接的ip 瀏覽:331
冰箱壓縮機是否需要電容 瀏覽:346
python列表每一行數據求和 瀏覽:275
自己有一台伺服器可以玩什麼 瀏覽:657
社會學波普諾pdf 瀏覽:584
解壓做食物的小視頻 瀏覽:759