简体   繁体   中英

Java OOP issue with example. Seeking assistance

I have a question regarding a situation I am experiencing. I wrote a really basic program to help show you an example of what my issue is. I wondering how (if even possible) to do something a certain way. It involves a variable from one class, a method from another class. And using them together to get a result in the Main.

Main:

// The main, which creates an object of Testing and Yupp, then Tries to run the method to add +3 and display it, repeatidly.

import javax.swing.JOptionPane;

public class Runit
{

    public static void main(String[] args)
    {
        Testing testing = new Testing();
        Yupp yupp = new Yupp();

        JOptionPane.showMessageDialog(null, testing.number);
        yupp.doMath();
        JOptionPane.showMessageDialog(null, testing.number);
        yupp.doMath();
        JOptionPane.showMessageDialog(null, testing.number);
        yupp.doMath();
        JOptionPane.showMessageDialog(null, testing.number);
        yupp.doMath();
        JOptionPane.showMessageDialog(null, testing.number);
    }

}

Yupp Class:

//Just extending all the information from Testing class( which holds the "number" variable so I have access to it ) Then making the simple personal method to add 3 to it.

public class Yupp extends Testing
{

    public void doMath()
    {
        number = number + 3;
    }

}

Testing Class:

// Just holding the number variable in this class.


public class Testing
{
    int number = 3;
}

So essentially what I want to happen, regardless of if its proper coding, which I'm sure it isn't. What should(I want to) happen is the "number" variable, should just increase by 3 on each separate JOptionPane window. Like I said, its just a basic code example I wrote to explain my problem. I think it makes a lot of sense this way. Thank you if you can help me figure out what to do to make this possible.

Currently the number always comes back as 3. Instead of 3 + 3, 6 + 3, 9 + 3, etc.

problem:

testing.number

You are getting the same number from that instance of Testing which is not incremented thus giving you 3 all the time

solution:

use your Yupp object to get the incremented value from the method call doMath

sample:

    JOptionPane.showMessageDialog(null, yupp.number);
    yupp.doMath();
    JOptionPane.showMessageDialog(null, yupp.number);
    yupp.doMath();
    JOptionPane.showMessageDialog(null, yupp.number);
    yupp.doMath();
    JOptionPane.showMessageDialog(null, yupp.number);
    yupp.doMath();
    JOptionPane.showMessageDialog(null, yupp.number);

In short:

You are creating two different objects:

Testing testing = new Testing();
Yupp yupp = new Yupp();

then, you are calling the operation ( doMath ) in one ( yupp ), but printing the attribute number of the other ( testing ). The last one, doesn't change in the whole program.

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