简体   繁体   中英

How to extends class twice?

Class passenger will get the number of passenger as well as passengers' information. Class destination will get the user's choice of destination. Class transaction will compute the number of passenger and the user's choice of destination.

I extended the class transaction to class destination to know what the user's choice and compute it. But the number of passengers is in the class passenger.

HOW CAN I ACCESS THE VARIABLE NUM FROM THE CLASS PASSENGER TO THE CLASS TRANSACTION?

class passenger{

public int num;
public static Scanner in = new Scanner(System.in);

void passengerNum(){
    System.out.print("Enter number of Passenger: ");
    **num** = in.nextInt();

    String[] Lname = new String[num];
    String[] Fname = new String[num];
    String[] MI = new String[num];
    String[] Alias = new String[num];
    int[] age = new int[num];
    int underage = 0;

    System.out.println("\nENTER PASSENGER INFORMATION");
    for(int i = 0; i < num; i++){
        System.out.print("Last Name: ");
        Lname[i] = in.next();
        System.out.print("First Name: ");
        Fname[i] = in.next();
        System.out.print("Middle Initial: ");
        MI[i] = in.next();
        System.out.print("Alias/Prefix/Suffix: ");
        Alias[i] = in.next();
        System.out.print("Age: ");
        age[i] = in.nextInt();
        System.out.println();     
}

class destination{

public static Scanner in = new Scanner(System.in);
public static char busClass;
public static char bConvert;

void selectDestination(){

    System.out.print("Select Destination: ");
    passDestination = in.next().charAt(0);
    dConvert = Character.toUpperCase(passDestination);
}
void busClass(){

    System.out.print("Select Bus Class: ");
    busClass = in.next().charAt(0);
    bConvert = Character.toUpperCase(busClass);

}

}

class transaction extends destination{

public int money;
public double total;
public double change;
public double discount;
public static int AA;

void passengerMoney(){
    System.out.println("Enter payment: ");
    money = in.nextInt();
}

void compute(){
    if(dConvert=='A'){
        if(bConvert=='A'){
            total = AA * num; 
            change = money - total;
        }
    }

create object of Passenger class inside transaction class , hence you can access num variable of passenger class inside transaction class

Correction: inside transaction class

 Passenger pObject = new Passenger;
           total = AA * pObject.num;

Full code:

public class Passenger{
              public static Scanner in = new Scanner(System.in);
                    public static int num;
                    void passengerNum(){
                        System.out.print("Enter number of Passenger: ");
                        int num = in.nextInt();
                        String[] Lname = new String[num], Fname = new String[num], MI = new String[num], Alias = new String[num];
                        int[] age = new int[num];
                        int underage = 0;

                        System.out.println("\nENTER PASSENGER INFORMATION");
                        for(int i = 0; i < num; i++){
                            System.out.print("Last Name: ");
                            Lname[i] = in.next();
                            System.out.print("First Name: ");
                            Fname[i] = in.next();
                            System.out.print("Middle Initial: ");
                            MI[i] = in.next();
                            System.out.print("Alias/Prefix/Suffix: ");
                            Alias[i] = in.next();
                            System.out.print("Age: ");
                            age[i] = in.nextInt();
                            System.out.println();     
                    }
                }
            }
    public class Destination {
                public static Scanner in = new Scanner(System.in);
                public static char busClass;
                public static char bConvert;
                void selectDestination(){
                    System.out.print("Select Destination: ");
                }
                char passDestination = in.next().charAt(0), dConvert = Character.toUpperCase(passDestination);
                void busClass(){
                    System.out.print("Select Bus Class: ");
                    busClass = in.next().charAt(0);
                    bConvert = Character.toUpperCase(busClass);
                }
            }
    public class Transaction extends Destination{
                Passenger pObject = new Passenger();
                public int money;
                public double total, change, discount;
                public static int AA;

                void passengerMoney(){
                    System.out.println("Enter payment: ");
                    money = in.nextInt();
                }
                void compute(){
                    if(dConvert=='A'){
                        if(bConvert=='A'){
                            total = AA * pObject.num;
                            change = money - total;
                        }
                    }
                }
            }`

I would point out an issue on the design side of your application.

Why would you extend the destination with the transaction class? Aren't they two distinct entities in the application's logic? In this case, is preferable to have an instance of the destination class inside the transaction class, hence separating the two entities.

People are giving wrong information -

In java , one class can be extended any number of times - unlimited times

Like -

public  class TestMethod {
    static class A{
        void aMethod(int a) {
            System.out.println("aMethod");
        }
    }
    static class B extends A{
        void bMethod(int a) {
            System.out.println("bMethod");
        }
    }
    static class C extends B{
        void cMethod(int a) {
            System.out.println("cMethod");
        }
    }
    static class D extends C{
        void dMethod(int a) {
            System.out.println("dMethod");
        }
    }
    static class E extends D{
        void eMethod(int a) {
            System.out.println("eMethod");
        }
    }
    static class F extends E{
        void fMethod(int a) {
            System.out.println("fMethod");
        }
    }
    static class G extends F{
        void gMethod(int a) {
            System.out.println("gMethod");
        }
    }
    static class H extends G{
        void hMethod(int a) {
            System.out.println("hMethod");
        }
    }
    static class I extends H{
        void iMethod(int a) {
            System.out.println("iMethod");
        }
    }
    static class J extends I{
        void jMethod(int a) {
            System.out.println("jMethod");
        }
    }
    static class K extends J{
        void kMethod(int a) {
            System.out.println("kMethod");
        }
    }
    static class L extends K{
        void lMethod(int a) {
            System.out.println("lMethod");
        }
    }
    static class M extends L{
        void mMethod(int a) {
            System.out.println("mMethod");
        }
    }
    static class N extends M{
        void nMethod(int a) {
            System.out.println("nMethod");
        }
    }
    static class O extends N{
        void oMethod(int a) {
            System.out.println("oMethod");
        }
    }
    static class P extends O{
        void pMethod(int a) {
            System.out.println("pMethod");
        }
    }
    static class Q extends P{
        void qMethod(int a) {
            System.out.println("qMethod");
        }
    }
    static class R extends Q{
        void rMethod(int a) {
            System.out.println("rMethod");
        }
    }
    static class S extends R{
        void sMethod(int a) {
            System.out.println("sMethod");
        }
    }
    static class T extends S{
        void tMethod(int a) {
            System.out.println("tMethod");
        }
    }
    static class U extends T{
        void uMethod(int a) {
            System.out.println("uMethod");
        }
    }
    static class V extends U{
        void vMethod(int a) {
            System.out.println("vMethod");
        }
    }
    static class W extends V{
        void wMethod(int a) {
            System.out.println("wMethod");
        }
    }
    static class X extends W{
        void xMethod(int a) {
            System.out.println("xMethod");
        }
    }
    static class Y extends X{
        void yMethod(int a) {
            System.out.println("yMethod");
        }
    }
    static class Z extends Y{
        void zMethod(int a) {
            System.out.println("zMethod");
        }
    }
public static void main(String[] args) {
    A aobject = new A(); B bobject = new B();C cobject = new C();D dobject = new D();E eobject = new E();F fobject = new F();G gobject = new G();H hobject = new H();I iobject = new I();J jobject = new J();K kobject = new K();L lobject = new L();M mobject = new M();N nobject = new N();O oobject = new O();P pobject = new P();Q qobject = new Q();R robject = new R();S sobject = new S();T tobject = new T();U uobject = new U();V vobject = new V();W wobject = new W();X xobject = new X();Y yobject = new Y();Z zobject = new Z();
    aobject.aMethod(10);bobject.bMethod(10);cobject.cMethod(10);dobject.dMethod(10);eobject.eMethod(10);fobject.fMethod(10);gobject.gMethod(10);hobject.hMethod(10);iobject.iMethod(10);jobject.jMethod(10);kobject.kMethod(10);lobject.lMethod(10);mobject.mMethod(10);nobject.nMethod(10);oobject.oMethod(10);pobject.pMethod(10);qobject.qMethod(10);robject.rMethod(10);sobject.sMethod(10);tobject.tMethod(10);uobject.uMethod(10);vobject.vMethod(10);wobject.wMethod(10);xobject.xMethod(10);yobject.yMethod(10);zobject.zMethod(10);
}
}

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