简体   繁体   中英

How to define a method that would take an object as a parameter and return that into another object?

I have to classes, one is a SmartCard and another one is CardLock. The SmartCard class creates a new object with a name and staff status that can be true or false.

Now, CardLock class is supposed to have a method where I can swipe a card and get the information from the last card that was swiped.

My code looks like this:

public class CardLock{
SmartCard lastCard;


public SmartCard swipeCard(SmartCard newCard){
    lastCard = newCard;
}

public SmartCard getLastCardSeen(){
    return lastCard;
}

public static void main(String[] args){
    System.out.println("Swiping card");
    SmartCard cardA = new SmartCard("Anna Undergrad", false);
    swipeCard(cardA);
    System.out.println(cardA.getOwner() + " card swiped.");
    SmartCard cardB = new SmartCard("Dr. John Turpentine", true);


    System.out.println("The last card swiped was by " + cardA.getLastCardSeen().getOwner());

}

Now, I get an error "non-static method SwipeCard(SmartCard) cannot be referenced from a static context", which I have difficulties in understanding.

Another error is down at cardA.getLastCardSeen().getOwner() where it fails to locate getOwner method even though it is in SmartCard and is public.

Thanks for the thelp.

The swipeCard method is defined for an CardLock object. You need a CardLock object on which to invoke that method.

I don't exactly get what you are trying to achieve but the reason you are getting the "non-static method SwipeCard(SmartCard) cannot be referenced from a static context" error is because main method is static and your swipeCard() method is not. Make it static and the error will go away. You can only reference static methods from withing a static method. Also cardA object is of SmartCard type. Since you have not posted that class I can't really be sure but I think SmartCard class doesn't have that method. Can you be a bit more specific of what you are trying to do and post the code for the SmartCard class. I edited your code below see if this helps.

public class CardLock {
    SmartCard lastCard;


    public static void swipeCard(SmartCard newCard) {
        lastCard = newCard;
    }

    public static SmartCard getLastCardSeen() {
        return lastCard;
    }

    public static void main(String[] args) {
        System.out.println("Swiping card");
        SmartCard cardA = new SmartCard("Anna Undergrad", false);
        swipeCard(cardA);
        System.out.println(cardA.getOwner() + " card swiped.");
        SmartCard cardB = new SmartCard("Dr. John Turpentine", true);


        System.out.println("The last card swiped was by " + getLastCardSeen().getOwner());

    }
}

First your function SmartCard swipeCard(SmartCard newCard) is not well defined. You should define it like:

public void swipeCard(SmartCard newCard)

if you want it to perform as you write it.

Next, you can't call swipeCard(cardA); like this because it is not a static method. If you want to do it so you have to add the keyword static to your function like:

public static void swipeCard(SmartCard newCard)

You can read more about static methods here Static methods vs instance methods java

And finally, it will be helpful to show us a bit of your SmartCard class.

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