課程
/后端開(kāi)發(fā)
/Java
/Java入門第二季
方法改為 private ?怎么調(diào)用?
2017-10-15
源自:Java入門第二季 7-3
正在回答
這個(gè)是書上的一個(gè)例子看懂了就?
public?class?Student?{ private?String?stuno; private?String?name; private?float?math; private?float?english; private?float?computer; public?Student()?{ } public?Student(String?stuno,String?name,float?math,float?english,float?computer)?{ this.setStuno(stuno); this.setName(name); this.setMath(math); this.setEnglish(english); this.setComputer(computer); } public?void?setStuno(String?s)?{ stuno?=?s; } public?void?setName(String?n)?{ name?=?n; } public?void?setMath(float?m)?{ math?=?m; } public?void?setEnglish(float?e)?{ english?=?e; } public?void?setComputer(float?c)?{ computer?=?c; } public?String?getStuno()?{ return?stuno; } public?String?getName()?{ return?name; } public?float?getMath()?{ return?math; } public?float?getEnglish()?{ return?english; } public?float?getComputer()?{ return?computer; } public?float?sum()?{ return?math+english+computer; } public?float?avg()?{ return?this.sum()/3; } public?float?max()?{ float?max?=?math; max?=?max?>?computer?max:computer; max=max>english?max:english; return?max; } public?float?min()?{ float?max?=?math; max?=?max?<?computer?max:computer; max=max<english?max:english; return?max; }};
Java調(diào)用private方法利用的是Java中的類反射機(jī)制 。定義一個(gè)class,定義一個(gè)內(nèi)部private方法:public class PrivateTest {private void print() {System.out.println("this is a private method"); }}再定義一個(gè)class去訪問(wèn)剛才定義的private方法,也就是print()如下import java.lang.reflect.Method;public class PrivateTest2 { public static void main(String[] args) {try { Method method = PrivateTest.class.getDeclaredMethod("print", new Class[]{}); method.setAccessible(true); Method.invoke(new PrivateTest(), new Object[] {});} catch (Exception ex) { System.out.println(ex.toString());}}}利用java的反射機(jī)制,即使是private方法,也可以被調(diào)用使用。
舉報(bào)
課程升級(jí)!以終為始告別枯燥,在開(kāi)發(fā)和重構(gòu)中體會(huì)Java面向?qū)ο缶幊痰膴W妙
1 回答求各位大神講解
1 回答老師 求解答
2 回答老師求解啊
2 回答老師們求解!
3 回答求大神講解下返回值
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號(hào)-11 京公網(wǎng)安備11010802030151號(hào)
購(gòu)課補(bǔ)貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動(dòng)學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號(hào)
2020-07-04
這個(gè)是書上的一個(gè)例子看懂了就?
2020-07-04
public?class?Student?{ private?String?stuno; private?String?name; private?float?math; private?float?english; private?float?computer; public?Student()?{ } public?Student(String?stuno,String?name,float?math,float?english,float?computer)?{ this.setStuno(stuno); this.setName(name); this.setMath(math); this.setEnglish(english); this.setComputer(computer); } public?void?setStuno(String?s)?{ stuno?=?s; } public?void?setName(String?n)?{ name?=?n; } public?void?setMath(float?m)?{ math?=?m; } public?void?setEnglish(float?e)?{ english?=?e; } public?void?setComputer(float?c)?{ computer?=?c; } public?String?getStuno()?{ return?stuno; } public?String?getName()?{ return?name; } public?float?getMath()?{ return?math; } public?float?getEnglish()?{ return?english; } public?float?getComputer()?{ return?computer; } public?float?sum()?{ return?math+english+computer; } public?float?avg()?{ return?this.sum()/3; } public?float?max()?{ float?max?=?math; max?=?max?>?computer?max:computer; max=max>english?max:english; return?max; } public?float?min()?{ float?max?=?math; max?=?max?<?computer?max:computer; max=max<english?max:english; return?max; }};2017-10-15
Java調(diào)用private方法利用的是Java中的類反射機(jī)制 。
定義一個(gè)class,定義一個(gè)內(nèi)部private方法:
public class PrivateTest {
private void print() {
System.out.println("this is a private method");
}
}
再定義一個(gè)class去訪問(wèn)剛才定義的private方法,也就是print()如下
import java.lang.reflect.Method;
public class PrivateTest2 {
public static void main(String[] args) {
try {
Method method = PrivateTest.class.getDeclaredMethod("print", new Class[]{});
method.setAccessible(true);
Method.invoke(new PrivateTest(), new Object[] {});
} catch (Exception ex) {
System.out.println(ex.toString());
}
}
}
利用java的反射機(jī)制,即使是private方法,也可以被調(diào)用使用。