① 以下java代碼中的this表示的是什麼,可以用什麼來替換,求解

this代表當前對象
大部分this可以不寫,但重名時必須得寫,如一類中有類變數value (int類型)
有一setValue函數 其參數也是value,就是將參數value的值賦值給類變數value,這時必須使用this
如下 public void setValue(int value)
{
this.value=value;//總不能寫成value=value吧
}

② java反射怎麼設置屬性獲取屬性

Field 有方法 get/set
Object get(Object obj)Returns the value of the field represented by this Field, on
the specified object.
~~~~~~~~~~
void set(Object obj, Object value)Sets the field represented by this Field object on the
specified object argument to the specified new value.

~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

③ java反射機制詳解

在Java運行時刻,能否知道一個類的屬性方法並調用改動之?對於任意一個對象,能否知道他的所屬類,並調用他的方法?答案是肯定的。這種動態的獲取信息及動態調用方法的機制在Java中稱為「反射」(reflection)。
Java反射機制主要提供以下功能:
在運行時判斷任意一個對象所屬的類;
在運行時構造任意一個類的對象;
在運行時判斷任意一個類所具有的成員變數和方法;
在運行時調用任意一個對象的方法。
Reflection 是Java被視為動態(或准動態)語言的一個關鍵性質。這個機制允許程序在運行時透過Reflection APIs取得任何一個已知名稱的class的內部信息,包括其modifiers(諸如public, static 等等)、superclass(例如Object)、實現之interfaces(例如Serializable),也包括fields和methods 的所有信息,並可於運行時改變fields內容或調用methods。
一般而言,開發者社群說到動態語言,大致認同的一個定義是:「程序運行時,允許改變程序結構或變數類型,這種語言稱為動態語言」。
在JDK中,主要由以下類來實現Java反射機制,這些類都位於java.lang.reflect包中:
Class類:代表一個類;
Field 類:代表類的成員變數(成員變數也稱為類的屬性);
Method類:代表類的方法;
Constructor 類:代表類的構造方法;
Array類:提供了動態創建數組,以及訪問數組的元素的靜態方法;

至於全部的你可以看看參考資料。我看這個資料不錯

④ java反射給欄位賦值就是給實體類的set賦值,怎麼做

親,以下是我寫的例子,你可以參考:
import java.lang.reflect.Field;
import java.util.Arrays;
import static java.lang.System.out;

enum Tweedle { DEE, DUM }

public class Book {
public long chapters = 0;
public String[] characters = { "Alice", "White Rabbit" };
public Tweedle twin = Tweedle.DEE;

public static void main(String... args) {
Book book = new Book();
String fmt = "%6S: %-12s = %s%n";

try {
Class<?> c = book.getClass();

Field chap = c.getDeclaredField("chapters");
out.format(fmt, "before", "chapters", book.chapters);
chap.setLong(book, 12);
out.format(fmt, "after", "chapters", chap.getLong(book));

Field chars = c.getDeclaredField("characters");
out.format(fmt, "before", "characters",
Arrays.asList(book.characters));
String[] newChars = { "Queen", "King" };
chars.set(book, newChars);
out.format(fmt, "after", "characters",
Arrays.asList(book.characters));

Field t = c.getDeclaredField("twin");
out.format(fmt, "before", "twin", book.twin);
t.set(book, Tweedle.DUM);
out.format(fmt, "after", "twin", t.get(book));

// proction code should handle these exceptions more gracefully
} catch (NoSuchFieldException x) {
x.printStackTrace();
} catch (IllegalAccessException x) {
x.printStackTrace();
}
}
}

輸出:
BEFORE: chapters = 0
AFTER: chapters = 12
BEFORE: characters = [Alice, White Rabbit]
AFTER: characters = [Queen, King]
BEFORE: twin = DEE
AFTER: twin = DUM

如果對你有幫助的話,請點贊一下。如果有任何問題,都可以聯系我!

⑤ Java中return this的作用。不要和我說返回當前對象,我就是不理解這句話,最好能有代碼,

網上復找的說的很對你制可以看下;連續操作類里方法,每次返回類實例,用return this;

class Test2 {
String s="";
public static void main(String[] args) {
Test2 t = new Test2();
t.method("111");
t.method("222");
t.method("333");
t.method2();

}

public Test2 method(String sb) {
s+=sb;
return this;
}

public void method2() {

System.out.println(s);
}
} 結果: 111222333

⑥ Java反射中如何使用泛型

這個和泛型無關
Class.forName生成的是一個Class,這個Class的類型是無法確定的,不一定就是Collection,所以會有一個提醒

⑦ java反射取對象屬性值 不是通過get方法取值

反射不是通過get取值的,是通過該對象的信息,比如說名稱、內存地址等來訪問類,內方法,屬性等,容可以獲取任意對象的信息,但不能獲得私有屬性(private String s=「sd」;)的值,切記

推薦《JAVA核心技術》這本書會對你有幫助的

⑧ 什麼是java反射技術,有沒有核心代碼

看個例子吧 也算不上核心 幫助理解下反射
package test;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Admin {
public Admin(){ }
private String id = "";
public String getId() {
System.out.print(id);
return id;
}
public void setId(String id) {
this.id = id;
}

public static void main(String[] args)
throws ClassNotFoundException, InstantiationException, IllegalAccessException,
SecurityException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException {
String str = "test.Admin";
Class c = Class.forName(str);
Object obj = c.newInstance(); //得到Admin類
Method m = c.getMethod("setId",new Class[]{Class.forName("java.lang.String")}); //這里是設置屬性的值的方法(setId方法名)
m.invoke(obj,new Object[]{"admin"}); //調用方法<實體類,參數>)
m = c.getMethod("getId",new Class[]{}); //這里是里獲取屬性的值
m.invoke(obj,new Object []{});
}
}

⑨ 利用JAVA反射技術執行一個類的方法

public class Test {

public Test() {

}

public static void main(String[] args) throws IllegalArgumentException,
SecurityException, IllegalAccessException,
InvocationTargetException, NoSuchMethodException {
Bean bean = new Bean();
System.out.println(bean);
Bean.class.getMethod("setName", String.class).invoke(bean, "Jerry");
Bean.class.getMethod("setAge", int.class).invoke(bean, 25);
System.out.println("After reflection....\n" + bean);
}

}

class Bean {
private String name;
private int age;

public Bean() {
}

public Bean(String name, int age) {
super();
this.name = name;
this.age = age;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public String toString() {
return "NAME: " + this.getName() + "\nAGE: " + this.getAge();
}
}