2016-04-03 18 views
1

İki basamaklı bir sınıftan yöntemleri çağırırken dört basamaklı bir sınıf (saat gibi) oluşturuyorum.Başka bir sınıftan set değer yöntemini çağırıyor

Dört basamaklı sınıfın iki segmenti vardır. Birinci segment ayarlanmış maksimum değere ulaştığında, ikinci bölüm bir artırılmalıdır.

bu

benim ilk sınıftan Metotlarımı şunlardır:

/* 
* method to set the value 
*/ 
public void setValue(int anyValue){ 
    if((anyValue < TOO_HIGH) && (anyValue >= 0)){ 
     value = anyValue;} 
} 

/* 
* method to set the value plus one 
*/ 
public void setValuePlusOne(){ 
    int tempValue = value + 1; 
    if(tempValue < TOO_HIGH){ 
     value = tempValue;} 
    else{ 
     value = 0; 

     // value = tempValue % TOO_HIGH;} 

    } 

Bu benim ikinci dört haneli sınıfından olduğunu.

/* 
* method to set the increment 
*/ 
public void setIncrement(int increment){ 
    rightSegment.setValuePlusOne(); 
    if(increment == 0) 
     leftSegment.setValuePlusOne(); 

    } 

herhangi tavsiye yardımcı olacağını ben deneyin zaman derleme değil benim artım == 0 ile yanlış bir şey varsa (rightsegment.setValuePlusOne() == 0)

olabileceğini düşünüyorum. Teşekkür ederim!!

+1

'rightSegment.setValuePlusOne();' 'int' derleme dönmelidir. –

+0

Lütfen kodunuzu otomatik olarak girip biçimlendiren bir IDE kullanın. Gönderdiğiniz kod biraz dağınık, özellikle kapanış parantezleri kafa karıştırıcı. –

+0

Peki ... neden iki ayrı sınıf kullanmak yerine, bunları yalnızca bir araya getirmiyorsunuz ve artırımları kontrol edecek ifadeler kullanıyorsunuz? Programınızın amacı hakkında biraz daha fazla fikir, sizi doğru yöne yönlendirmeye yardımcı olacaktır. –

cevap

1

setValuePlusOne (...) hiçbir şey döndürmez. If önce setValuePlusOne çağrısı yapın ve sonra if (use) (rightsegment.getValue() == 0) 'ı kullanın.

0

Lütfen aşağıdaki kodu deneyin. Umarım aşağıdaki kod, uygulamanıza ulaşmanıza yardımcı olur. Aşağıdaki kodun başka bir blokunda, TOO_HIGH tamsayı değerini ayarlamak yerine, bunu sırasıyla Saat sınıfını genişleten RightSegment ve LeftSegment sınıflarında ayarlayabilirsiniz. sayesinde

package stackoverflow; 

public class Clock { 

private int value; 
private int TOO_HIGH; 
private Clock rightSegment; 
private Clock leftSegment; 


/* 
* method to set the value 
*/ 
public void setValue(int anyValue, String position){ 
    if(position.equals("right")){ 
     TOO_HIGH = 60; 
    }else if(position.equals("left")){ 
     TOO_HIGH = 13; 
    } 

    if((anyValue < TOO_HIGH) && (anyValue >= 0)){ 
     value = anyValue;} 
} 

/* 
* method to set the value plus one 
*/ 
public void setValuePlusOne(){ 
    int tempValue = value + 1; 
    if(tempValue < TOO_HIGH){ 
     value = tempValue;} 
    else{ 
     value = 0; 
    } 

     // value = tempValue % TOO_HIGH;} 

    } 


    /* 
    * method to set the i`ncrement 
    */ 
    public void setIncrement(int increment, Clock right, Clock left){ 
     rightSegment = right; 
     leftSegment = left; 
     //rightSegment = new Clock(); 
     //leftSegment = new Clock(); 
     rightSegment.setValuePlusOne(); 
     if(increment == 0){ 
      leftSegment.setValuePlusOne(); 
     } 

    } 

    public static void main (String args[]){ 
     Clock clock = new Clock(); 
     clock.rightSegment = new Clock(); 
     clock.leftSegment = new Clock(); 
     clock.rightSegment.setValue(12, "right"); 
     clock.leftSegment.setValue(12, "left"); 
     clock.rightSegment.setIncrement(0, clock.rightSegment, clock.leftSegment); 
     System.out.println("Time "+clock.leftSegment.value+":"+clock.rightSegment.value); 
    } 

}

İlgili konular