下午!我在課堂「電視」中遇到了一些麻煩。我基本上試圖完成的是每當調用該方法時,我想要切換powerStatus的值。實際和正式的方法列表長度不同
/**
* @author Thomas Samuel
* @version 1.0
* @since 22/02/2017
* <h1>Television Remote/Lab Sheet 2</h1>
* <p1>The following program is made for a television remote to control power, channel information, and volume.</p1>
*/
class Television {
Television Television = new Television();
boolean powerStatus = false;
int currentChannel;
int currentVolume = 50;
boolean togglePower(boolean powerStatus) {
if(powerStatus = false) {
powerStatus = true;
} else if(powerStatus = true) {
powerStatus = false;
}
return powerStatus;
}
boolean getPowerStatus(boolean powerStatus) {
return powerStatus;
}
}
public class Controller {
public static void main(String[] args) {
Television.togglePower();
}
}
我收到的錯誤是如下:
Controller.java:27: error: method togglePower in class Television cannot be applied to given types;
Television.togglePower();
^
required: boolean
found: no arguments
reason: actual and formal argument lists differ in length
1 error
當你調用'togglePower'方法時,你有**沒有通過**'boolean'參數。 –
'電視臺{電視電視=新電視(); ......爲什麼你將'Television'的一個實例實例化爲它自身的非靜態成員?你爲什麼要將一個變量命名爲它的類?這些都是你沒有理解一些基本原理的指標,並且使你的代碼非常難以閱讀。例如,在你的主要方法'Television.togglePower();'看起來像一個靜態方法調用,但它不是。它的作用只是因爲你的名字「電視」超載。 –
接下來,您將得到一條消息,指出無法從靜態上下文中引用非靜態方法。 –