简体   繁体   中英

How would i use another method from another class

I have a counter class and a clock class. In my counter class i have an Increment method. In my clock class i want to use that increment method from counter class to increase my _seconds. Right now i have an error on the line "_seconds.Increment();"

public class Clock
{


    public int _hours;
    public int _minutes;
    public int _seconds;

   public Clock()
    {

        _hours = 0;
        _minutes = 0;
        _seconds = 0;
    }

    public void Tick()
    {

        if(_seconds < 60)
        {
            _seconds.Increment();
        }

This is my counter class

public class Counter
{

    private int _count;
    private string _name;


    public Counter(string name)
    {
        _name = name;
        _count = 0;
    }

    public void Increment()
    {
        _count++;
    }

    public void Reset()
    {
        _count = 0;
    }

    public string Name
    {
        get
        {
            return _name;
        }
        set
        {
            _name = value;
        }
    }

    public int Count
    {
        get
        {
            return _count;
        }
    }
}

Either you do _seconds++; or you do this:

public class Clock
{
    public int _hours;
    public int _minutes;
    public Counter _seconds;

   public Clock()
    {

        _hours = 0;
        _minutes = 0;
        _seconds = new Counter("Seconds");
    }

    public void Tick()
    {

        if(_seconds.Count < 60)
        {
            _seconds.Increment();
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM