Telphone類(lèi)中,sendMessage方法后面括號(hào)里為什么不要寫(xiě)引用參數(shù)?變量在方法外,方法里要使用不要定義參數(shù)嗎?
public class InitailTelphone
{
????public static void main(String[] args)
????{
????????????Telphone phone=new Telphone( );
????????????phone.screen=5.0f;
????????????phone.cpu=1.4f;
????????????phone.mem=2.0f;
????????????phone.sendMessage();
????}
}
public class Telphone
{
????float screen;
????float cpu;
????float mem;
????void sendMessage()
????{
????????System.out.println("screen:"+screen+"cup:"+cup+"mem:"+mem+"Telphone有發(fā)短信的功能");
????}
}
2016-12-24
2016-12-22
又如這段代碼,其中方法使用變量卻定義參數(shù)。這個(gè)參數(shù)到底什么時(shí)候用什么時(shí)候不用?
public class HelloWorld {
? ? public static void main(String[] args) {
? ? ? ??
// 創(chuàng)建對(duì)象,對(duì)象名為hello
HelloWorld hello = new HelloWorld();
? ? ? ? // 調(diào)用方法,傳入兩門(mén)課程的成績(jī)
hello.calcAvg(94, 81);
}
/*
* 功能:計(jì)算兩門(mén)課程考試成績(jī)的平均分并輸出平均分
* 定義一個(gè)包含兩個(gè)參數(shù)的方法,用來(lái)傳入兩門(mén)課程的成績(jī)
*/
? ? public void calcAvg(int a,int b)
? ? {
? ? ? ? int a;
? ? ? ? int b;
? ? ? ? int avg=a/b;
? ? ? ? System.out.println("平均分:"+avg);
? ? }