我想将变量传输到扩展类而不将其值更改为null

时间:2018-07-31 10:03:03

标签: java

这是我到目前为止创建的,我想将在类Subway中定义的q的值转移到扩展类Billing

地铁:

// This is the main part of the program from where everything will start

import java.util.*;

public class Subway extends Billing {

    public static void menu() {

        Scanner obj1 = new Scanner(System.in);

        Subway obj2 = new Subway();

        int a = 1;
        int b = 1;
        int c = 1;

        System.out.println("Loading...");

        for (a = 1; a <= 999999999; a++) {

        }

        for (b = 1; b <= 999999999; b++) {

        }

        for (c = 1; c <= 999999999; c++) {

        }

        System.out.println("Program has been Loaded Successfully");

        System.out.println("Welcome to Subway....");

        System.out.println(
                "==============================================================================================================================================");

        System.out.println("Sandwiches\t\t\t\tMRP(₹)\tSnacks\t\t\t\t\tMRP(₹)\tBreakfast\t\t\t\tMRP(₹)");

        System.out.println(
                "Aloo Patty(AP)\t\t\t\t₹ \tChatpata Chana SubWrap(CCSW)\t\t₹\tChicken Slice,Eggs and Cheese(CSEC)\t₹");

        System.out.println("Chicken Kofta(CK)\t\t\t₹ \tChicken Kofta SubWrap(CKSW)\t\t₹\tCheese and Egg(CE)\t\t\t₹");

        System.out.println(
                "Chatpata Chana Patty(CCP)\t\t₹ \tChicken Strips SubWrap(CSSW)\t\t₹\tWestern Cheese and Egg(WCE)\t\t₹");

        System.out.println(
                "Chicken Slices(CS)\t\t\t₹ \tVeg Shammi SubWrap(VSSW)\t\t₹\t==============================================");

        System.out.println(
                "Chicken Tandoori(CTA)\t\t\t₹ \t==============================================================================================");

        System.out.println("Chicken Tikka(CTI)\t\t\t₹ \tSides\t\t\t\t\tMRP(₹)\tDrinks\t\t\t\t\tMRP(₹)");

        System.out.println("Corn and Peas(CP)\t\t\t₹ \tChips(Small)\t\t\t\t₹ \tMirinda(300ml)\t\t\t\t₹ ");

        System.out.println("Hara Bhara Kebab(HBK)\t\t\t₹ \tChips(Big)\t\t\t\t₹ \tMirinda(500ml)\t\t\t\t₹ ");

        // The menu is incompelete
        System.out.println("Enter the Code in the brackets to order your item:");
        String i = obj1.nextLine();
        String iU = i.toUpperCase();// to convert code to upper case
        System.out.println("Quantity: ");
        int q = obj1.nextInt();
        switch (iU) {
        case "AP":
            obj2.AP();
            break;
        }
    }
}

结算:

import java.util.*;

public class Billing {
    int pr = 0, amt = 0;// global variables for price and amount
    boolean flag = false;// for if else

    public void main() {
        System.out.println("Option not found in the menu");
    }

    public static void AP(int q) {
        int pr = 125, amt = 0;
        boolean flag = false;
        amt = pr * q;

    }
}

请让我回答。 通过创建将值刷新为null的对象无法解决该问题。所以在回答这个问题之前,请记住这一点

2 个答案:

答案 0 :(得分:1)

您的方法AP的类型为int,但是您将其保留为空。其次,您的AP方法是静态的,用类名

调用静态方法是更好的方法
Billing.AP(q);

建议使用具有适当名称的变量和方法,例如 int amount; String country;

希望这会对您有所帮助。

答案 1 :(得分:0)

obj2.AP(q);

在您的“结算类别”上进行此更改。应该可以。