简体   繁体   中英

Cannot find symbol when creating an object in IO class using inheritance

I'm trying to create a tourist object on my IO class and the parameters are specified correctly in its respective class. However, it wouldn't compile as it says it cannot find symbol. Is it because I'm using tourist as a sub-classes? The compiler says that it cannot find symbol for variable "nam" etc.. Thanks for helping.

This is a method in my IO class..

private void addMemberCard()
        {
            System.out.println("Enter Member name");
            String name = reader.nextLine();
            System.out.println("Select 1. Tourist, 2. Business");
            System.out.println("Enter your choice");
            int choice = reader.nextInt();


            MemberCard m;
            if (choice == 1){m = new Tourist (nam, rat, cred, cit);}
            else if (choice == 2){m = new Business(nam, rat);}
            preston.addMemberCard(m);
    }

and this is my constructor in Tourist Class

public Tourist (String nam, int rat, int cred, String cit)
    {
        super(nam, rat, cred);
        city = cit;
    }

This has nothing to do with inheritance. You need to understand the difference between parameters and arguments. The parameters nam , rat , cred , and cit exist only within the Tourist constructor. In the addMemberCard() method, nam , rat , cred , and cit don't exist; markspace indirectly pointed this out. Now you need to use arguments, existing variables with an actual value.

This should hopefully help: http://en.wikipedia.org/wiki/Parameter_%28computer_programming%29#Parameters_and_arguments

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