从一个类传递到另一个类

huangapple go评论75阅读模式
英文:

Input from one class to another

问题

以下是翻译好的部分:

Main.Java:

import java.util.Scanner;

public class Main {
    boolean exit = false;

    public void runMenu() {
        printHeader();
        while (!exit) {
            mainMenu();
            int choice = getInput();
            performAction(choice);
        }
    }

    private void performAction(int choice) {
        switch(choice){
            case 1:
                Lead lead1 = new Lead();
                lead1.primeLead();
            case 2:
            case 3:
            case 4:
            case 5:
                exit = true;
                System.out.println("Bye!");
                break;
        }
    }

    public void printHeader() {
        System.out.println("===========================================");
        System.out.println("                  Hello user!              ");
        System.out.println("               Welcome to our lead         ");
        System.out.println("                 Management tool           ");
        System.out.println("===========================================");
    }

    public void mainMenu() {
        System.out.println("\nPlease select one of the following options: ");
        System.out.println("1) Create a new lead");
        System.out.println("2) View all the leads");
        System.out.println("3) Connect ");
        System.out.println("4) View statistics");
        System.out.println("5) Exit   ");
    }

    private int getInput() {
        Scanner kb = new Scanner(System.in);
        int choice = -1;
        while (choice < 0 || choice > 5) {
            try {
                System.out.print("\nEnter your choice:  ");
                choice = Integer.parseInt(kb.nextLine());
            }
            catch (NumberFormatException e) {
                System.out.println("Invalid selection, please try again.");
            }
        }
        return choice;
    }

    public static void main(String[] args) {
        Main menu = new Main();
        menu.runMenu();
    }
}

Lead.java :

import java.util.ArrayList;
import java.util.Scanner;

public class Lead extends Main {
    String nameLead;
    int ageLead;
    int phoneLead;
    String cityLead;
    String email;
    String otherNotes;
    int choicelead;
    int indexOfLead = 0;
    int i = indexOfLead;

    ArrayList<String> names = new ArrayList<String>();
    ArrayList<Integer> ages = new ArrayList<Integer>();
    ArrayList<Integer> phones = new ArrayList<Integer>();
    ArrayList<String> cities = new ArrayList<String>();
    ArrayList<String> emails = new ArrayList<String>();
    ArrayList<String> notes = new ArrayList<String>();

    Scanner leads = new Scanner(System.in);
    
    Lead() {
        i = 0;
    }

    public  void primeLead() {
        i = 0;
        System.out.println("============================================");
        System.out.println("  Please enter by the following order : ");
        System.out.println("     Name, age, phone , city, mail     ");
        System.out.println("============================================");
    
        System.out.println("Please enter the name of the Lead :  ");
        names.add(leads.nextLine());
    
        System.out.println("Age? :  ");
        ages.add(Integer.parseInt(leads.nextLine()));
    
        System.out.println("Phone number? ");
        phones.add(Integer.parseInt(leads.nextLine()));
    
        System.out.println("Would you like to add ... ");
        System.out.println("1) City? ");
        System.out.println("2) Email? ");
        System.out.println("3) Notes?  ");
        System.out.println("4) All of the above?");

        if (leads.nextLine().equals("1")) {
            System.out.println("Please add City: ");
            cities.add(leads.nextLine());
            runMenu();
        }
        else if (leads.nextLine().equals("2")) {
            System.out.println("Please add email : ");
            emails.add(leads.nextLine());
            runMenu();
        }
        else if (leads.nextLine().equals("3")) {
            System.out.println("Please add any other notes you may have:  ");
            notes.add(leads.nextLine());
            runMenu();
        }

        if (leads.nextLine().equals("4")) {
            System.out.println("Please add a City: ");
            cities.add(leads.nextLine());

            System.out.println("Please add email : ");
            emails.add(leads.nextLine());

            System.out.println("Please add any other notes you may have:  ");
            notes.add(leads.nextLine());
        }
    }
}

View.java:

public class View extends Main {
    public class ViewLeads extends Lead {
    
    }
}
英文:

I'm receiving input in class Main, sorting it to different ArrayLists in Lead.java and want to make it so View.java is able to view all the information stored in Lead.java, triggered by the correct input from class Main - however I understand there is no such thing as extending two classes. How do I do it ?

Main.Java:

import java.util.Scanner;

public class Main {
    boolean exit = false;

    public void runMenu() {
        printHeader();
        while (!exit) {
            mainMenu();
            int choice = getInput();
            performAction(choice);
        }
    }

    private void performAction(int choice) {
        switch(choice){
            case 1:
                Lead lead1 = new Lead();
                lead1.primeLead();
            case 2:
            case 3:
            case 4:
            case 5:
                exit = true;
                System.out.println(&quot;Bye!&quot;);
                break;
        }
    }

    public void printHeader() {
        System.out.println(&quot;===========================================&quot;);
        System.out.println(&quot;                  Hello user!              &quot;);
        System.out.println(&quot;               Welcome to our lead         &quot;);
        System.out.println(&quot;                 Management tool           &quot;);
        System.out.println(&quot;===========================================&quot;);
    }

    public void mainMenu() {
        System.out.println(&quot;\nPlease select one of the following options: &quot;);
        System.out.println(&quot;1) Create a new lead&quot;);
        System.out.println(&quot;2) View all the leads&quot;);
        System.out.println(&quot;3) Connect &quot;);
        System.out.println(&quot;4) View statistics&quot;);
        System.out.println(&quot;5) Exit   &quot;);
    }

    private int getInput() { // Scanner takes input from user, returns his choice.
        Scanner kb = new Scanner(System.in);
        int choice = -1;
        while (choice &lt; 0 || choice &gt; 5) {
            try {
                System.out.print(&quot;\nEnter your choice:  &quot;);
                choice = Integer.parseInt(kb.nextLine()); // What is Integer.parseInt ? what is . next line ?
            }
            catch (NumberFormatException e) {
                System.out.println(&quot;Invalid selection, please try again.&quot;);
            }
        }
        return choice;
    }

    public static void main(String[] args) {
        Main menu = new Main();
        menu.runMenu();
    }
}

Lead.java :

import java.util.ArrayList;
import java.util.Scanner;

public class Lead extends Main {
    String nameLead;
    int ageLead;
    int phoneLead;
    String cityLead;
    String email;
    String otherNotes;
    int choicelead;
    int indexOfLead = 0;
    int i = indexOfLead;

    ArrayList&lt;String&gt; names = new ArrayList&lt;String&gt;();
    ArrayList&lt;Integer&gt; ages = new ArrayList&lt;Integer&gt;();
    ArrayList&lt;Integer&gt; phones = new ArrayList&lt;Integer&gt;();
    ArrayList&lt;String&gt; cities = new ArrayList&lt;String&gt;();
    ArrayList&lt;String&gt; emails = new ArrayList&lt;String&gt;();
    ArrayList&lt;String&gt; notes = new ArrayList&lt;String&gt;();

    Scanner leads = new Scanner(System.in);
    
    Lead() {
        i = 0;
        // Need to create an ArrayList that has all the Arraylists above.
    }

    public  void primeLead() {
        i = 0;
        System.out.println(&quot;============================================&quot;);
        System.out.println(&quot;  Please enter by the following order : &quot;);
        System.out.println(&quot;     Name, age, phone , city, mail     &quot;);
        System.out.println(&quot;============================================&quot;);
    
        System.out.println(&quot;Please enter the name of the Lead :  &quot;);
        names.add(leads.nextLine());
    
        System.out.println(&quot;Age? :  &quot;);
        ages.add(Integer.parseInt(leads.nextLine()));
    
        System.out.println(&quot;Phone number? &quot;);
        phones.add(Integer.parseInt(leads.nextLine()));
    
        System.out.println(&quot;Would you like to add ... &quot;);
        System.out.println(&quot;1) City? &quot;);
        System.out.println(&quot;2) Email? &quot;);
        System.out.println(&quot;3) Notes?  &quot;);
        System.out.println(&quot;4) All of the above?&quot;);

        if (leads.nextLine().equals(&quot;1&quot;)) {
            System.out.println(&quot;Please add City: &quot;);
            cities.add(leads.nextLine());
            runMenu();
        }
        else if (leads.nextLine().equals(&quot;2&quot;)) {
            System.out.println(&quot;Please add email : &quot;);
            emails.add(leads.nextLine());
            runMenu();
        }
        else if (leads.nextLine().equals(&quot;3&quot;)) {
            System.out.println(&quot;Please add any other notes you may have:  &quot;);
            notes.add(leads.nextLine());
            runMenu();
        }

        if (leads.nextLine().equals(&quot;4&quot;)) {
            System.out.println(&quot;Please add a City: &quot;);
            cities.add(leads.nextLine());

            System.out.println(&quot;Please add email : &quot;);
            emails.add(leads.nextLine());

            System.out.println(&quot;Please add any other notes you may have:  &quot;);
            notes.add(leads.nextLine());
        }
    }
}

View.java:

public class View extends Main {
    public ViewLeads extends Lead {
    
    }
}

答案1

得分: 1

public class View {
    leadReferenceObj = new Lead();
    // 假设你的类都在同一个包中,所有公共属性和公共方法都可以通过 leadReferenceObj 访问
    // 要访问 Lead.java 中的 primLead 方法:
    leadReferenceObj.primLead();
}
> 这是 Java 面向对象编程的基本规则之一由于 primeLead() 方法被声明为 public 访问修饰符你可以从另一个类中访问它primeLead() 方法使用了从父类 Main.java 分配的 Lead.java 类中的值

想要了解更多
https://stackify.com/oops-concepts-in-java/#:~:text=OOP%20concepts%20in%20Java%20are,encapsulation%2C%20inheritance%2C%20polymorphism.&amp;text=Basically%2C%20Java%20OOP%20concepts%20let,of%20them%20without%20compromising%20security.
英文:
public class View{
leadReferenceObj = new Lead();
//Assuming your classes are in the same package, all the public attributes
//and public methods are accessible using leadReferenceObj
//to access the primeLead method in Lead.java;
leadReferenceObj.primLead();
}

> This one of the basic rules of OOP programming in java. Since the method primeLead() is declared with a public access modifier, you can access it from another class. primeLead() method is using values from the Lead.java class that are assigned from the parent Main.java class.

To read a little more:

https://stackify.com/oops-concepts-in-java/#:~:text=OOP%20concepts%20in%20Java%20are,encapsulation%2C%20inheritance%2C%20and%20polymorphism.&amp;text=Basically%2C%20Java%20OOP%20concepts%20let,of%20them%20without%20compromising%20security.

huangapple
  • 本文由 发表于 2020年9月19日 02:40:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/63961163.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定