看板 java 關於我們 聯絡資訊
麻煩一下各位先進QQ 以下是我的問題 __________________________________________________ import java.io.* ; public class main { public static void main(String[] args)throws IOException { test t =new test(); t.setname(); System.out.println(t.name); } } __________________________________________________ public class test { String name="test"; public void setname() { name = returnname(); } public String returnname() { name = "abc"; return name; } } ___________________________________________________ 我想做的事情是 想想要在class main的程式碼執行後 可以修改到class test中的name的值 意思是我想看到class test 中的name = "test"; 可以變成name = "abc"; 我知道上面那兩段程式碼,無法做到 他頂多只能讓我去輸出t.name得到abc這個結果 可是無法改變程式碼內的數值 希望各位先進,可以給我一個方向,讓我去解決這個問題 如果我想做的是不可能的事情,請各位先進跟提點一下 謝謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 163.15.172.130
maxxi:那在 t.setname 之前先加一個 System.out.println(t.name); 05/29 01:01
TonyQ:test 完全不改的話不可能... 05/29 01:02
qrtt1:改 bytecode 算嗎@@? 05/29 01:16
melkor:我知道這兩個程式碼做不到 05/29 02:38
pico2k:try reflection 05/29 09:57
pico2k:sample-> http://0rz.tw/esTYB 05/29 10:00