从类 getValue 调用 Java
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5586967/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java calling from a class getValue
提问by tekman22
Alright so I'm trying to create a method called: setIncrement(). I have two classes I am using, FourDigit, which contains two TwoDigit objects that I have made. I've called each one segment1 and segment2. Their relationship is: segment1.segment2. This method will increment segment2 and when it resets to zero, the method will increment segment1.
好的,所以我正在尝试创建一个名为:setIncrement() 的方法。我有两个正在使用的类,FourDigit,其中包含我创建的两个 TwoDigit 对象。我已经分别调用了 segment1 和 segment2。它们的关系是:segment1.segment2。该方法将增加segment2,当它重置为零时,该方法将增加segment1。
The problem is within this piece of code:
问题出在这段代码中:
public void setIncrement(){
segment2.incrementValue();
if(segment2.getValue == 0){
segment1.value = segment1.value +1;
}else{
segment2=aNum%tooHighB;
}
getDisplayString();
}
It says: cannot find symbol variable: getValue() ???
它说:找不到符号变量:getValue() ???
Here are the classes:
以下是课程:
FourDigits class:
四位数类:
public class FourDigits
{
/**
* Fields
*/
private Digits segment1;
private Digits segment2;
private String displayString;
/**
* Constructor for objects of class FourDigits
*/
public FourDigits(int anyHigha, int anyHighb)
{
segment1 = new Digits(anyHigha);
segment2 = new Digits(anyHighb);
}
/**
* Mutator method to set values
*/
public void setValues(int anyNuma, int anyNumb){
segment1.setValue(anyNuma);
segment2.setValue(anyNumb);
}
/**
* Mutator method to set the increments
*/
public void setIncrement(){
segment2.incrementValue();
if(segment2.getValue == 0){
segment1.value = segment1.value +1;
}else{
segment2=aNum%tooHighB;
}
getDisplayString();
}
/**
* Mutator method to getDisplayString
*/
public void getDisplayString(){
System.out.println(segment1.displayString() ="." + segment2.displayString());
}
}
Which is taking methods from the Digits class:
它从 Digits 类中获取方法:
public class Digits
{
/**
* Fields
*/
private int value;
private int tooHigh;
private String displayString;
/**
* Constructors
*/
public Digits(int anyNum)
{
value = 0;
tooHigh=anyNum;
displayString = "";
}
/**
* Mutator method to set the value
*/
public void setValue(int anyValue){
if((anyValue < tooHigh) && (anyValue >= 0)){
value = anyValue;
}else{
value=0;
}
}
/**
* Mutator method to add to the value
*/
public void addValue(){
if(value < tooHigh){
value=value+1;
}else{
value=tooHigh;
}
}
/**
* Mutator method to increment the value
*/
public void incrementValue(){
int incrementValue;
incrementValue = value + 1;
value = incrementValue % tooHigh;
}
/**
* Mutator method to display the string
*/
public void displayString(){
if(value<10){
displayString="0" + value;
}else{
displayString="" + value;
}
System.out.println(displayString);
}
public int getValue(){
return value;
}
}
采纳答案by Buhake Sindi
Besides what everybody said:
除了大家所说的:
segment2.incrementValue()
Your class method Digits.incrementValue()
is of type void
so this comparison check will neverwork:
您的类方法Digits.incrementValue()
属于类型,void
因此此比较检查永远不会起作用:
if(segment2.incrementValue() == 0){ //Corrected your equals operation
I would suggest doing:
我建议这样做:
- Create a getter method for instance variable
value
(asvalue
is private) in theDigits
class. - Do a comparison check as in the example (assuming a getter for
value
is implemented):
- 为类中的实例变量
value
(如value
私有)创建一个 getter 方法Digits
。 - 像示例中那样进行比较检查(假设实现了 getter
value
):
Example:
例子:
segment2.incrementValue();
if (segment2.getValue() == 0) {
//Do work....
segment1.setValue(segment1.getValue() + 1);
}
回答by Jon Skeet
As well as using the assignment operator instead of an equality comparison, you're also trying to set segment2 instead of segment2.value. I suspect the block should be:
除了使用赋值运算符而不是相等比较之外,您还尝试设置 segment2 而不是 segment2.value。我怀疑块应该是:
if (segment2.incrementValue() == 0) {
segment1.setValue(segment1.getValue() + 1);
} else {
segment2.setValue(aNum % tooHighB);
}
I can't see any declarations for aNum
or tooHighB
, mind you... and incrementValue()
returns void at the moment, whereas presumably you want it to return the new value.
我看不到aNum
or 的任何声明tooHighB
,请注意...并且目前incrementValue()
返回 void ,而大概您希望它返回新值。
(I'd encourage you to make use of spaces to make your code easier to read, by the way.)
(顺便说一下,我鼓励您使用空格使您的代码更易于阅读。)
回答by jberg
You need to be using '==' when comparing two numbers instead of '=' which assigns values.
比较两个数字时,您需要使用 '==' 而不是分配值的 '='。
e.g.:
例如:
if(segment2.incrementValue() == 0){
回答by Michael Borgwardt
Your problem is this line:
你的问题是这一行:
if(segment2.incrementValue() = 0){
And the compiler tells you exactly what's wrong: you're trying to assign to something that's a value, not a variable. Presumably you want to compare rather than assign, so the line should be:
编译器会准确地告诉您哪里出了问题:您正在尝试为某个值而不是变量赋值。大概你想比较而不是分配,所以该行应该是:
if(segment2.incrementValue() == 0){
回答by Swaranga Sarma
Why do you have this kind of comparison [which is an assignment instead of a comparison]:
为什么要进行这种比较 [这是赋值而不是比较]:
if(segment2.incrementValue() = 0)
Shouldn't it be
不应该是
if(segment2.incrementValue() == 0)
Also change your increment() method to this:
还将您的 increment() 方法更改为:
public int incrementValue(){
int incrementValue;
incrementValue = value + 1;
value = incrementValue % tooHigh;
return value;
}
回答by unholysampler
Everyone got the ==
, so I'll point out a different error.
每个人都得到了==
,所以我会指出一个不同的错误。
FourDigits
doesn't have access rights to Digits.value
. You should be using the setValue()
method you have in Digits
.
FourDigits
没有访问权限Digits.value
。您应该使用setValue()
您在Digits
.
Edit for update:
编辑更新:
if(segment2.getValue == 0){
getValue is a method and need to have parenthesis at the end of it. Instead you need this:
getValue 是一个方法,需要在它的末尾加上括号。相反,你需要这个:
if(segment2.getValue() == 0){