java learning (day2) CLOCK

Seen the python object-oriented, so start with java in general is relatively fast.

I write a simple clock after watching video learning

There are two parts a clock hours and minutes. Then a large number as well as month and date.

Analyze is that these properties after reaching a value, re-zero.

We can write a first class, for the above-mentioned properties used to represent

package newJava;

public class newjava {

    private int value = 0;
    private int limit = 0;
    
    public newjava(int limit) {
        this.limit = limit;
    }
    
    public void increase() {
        value ++;
        if(value == limit)
        {
            value = 0;
        }
    }
    
    public int getValue() {
        return value;
    }
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub
    }
}

This class represents the input a number (limit), this number to zero.

Next, we need a slightly larger objects. The hours and minutes added.

Analyze the relationship between these two classes, when the sub-zero, and when will be incremented. We can use this to write a function

package newJava;

public class clock {
    private newjava hour = new newjava(24);
    private newjava minute = new newjava(60);
    
    public void start() {
        while(true) {
            minute.increase();
        if(minute.getValue() == 0) {
            hour.increase();
        }
            System.out.printf("%2d:%2d\n",hour.getValue(),minute.getValue());

        }
    }
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        clock clock = new clock();
        clock.start();
    }

}

This class will be able to complete the above-mentioned increment of time.

 

java modifiers.

We can see in the above code is defined and named when used in the private and public.

When inside the class definition of variables, most with private. Represents private, relatively safe to do so, class variables will not be modified, it can only be run in accordance with the class rules.

But the public is not the same, can be accessed by any class.

 

Guess you like

Origin www.cnblogs.com/afei123/p/11333214.html