简体   繁体   English

如何在主要方法Java中调用类?

[英]How do i call a class in the main method java?

OK im kinda new in java and i made this averaging program in one class, but now if i want to like call it from another class, then how do i do it?I tryed some object stuff but its hard to understand for. 好的,我是Java的新手,我在一个类中制作了这个平均程序,但是现在如果我想从另一个类中调用它,那我该怎么做呢?我尝试了一些对象的东西,但是很难理解。 this is the code and i want this program to start when i call it. 这是代码,我希望在我调用该程序时启动它。

    package Gangsta;
import java.util.Scanner;

public class okidoki {
    public static void main(String []args){
        Scanner input = new Scanner(System.in);

        double average, tests, grades, total = 0;
        int cunter = 1, start = 0;

        System.out.println("Press 2 to start averaging, or press 1 to end");

        start = input.nextInt();
        while (cunter<start){
        System.out.println("Enter how many tests u have");
        tests = input.nextDouble();

        System.out.println("Enter tests grades");
        int counter = 0;
        while (counter<tests){
            counter++;
            grades = input.nextDouble();
            total = grades+total;
        }
            average = total/tests;
                System.out.println(average);
        System.out.println("Press 3 to end or 1 to average again");
        cunter = input.nextInt();}

    }

}

this is the code where i want to execute it 这是我要执行它的代码

    package Gangsta;

public class tuna {
    public static void main(String []args){
        okidoki okidokiObject = new okidoki();
        System.out.println(okidokiObject);
    }

}

In java the main method is the main (it's in the name) entry point of a program. 在java中, main方法是程序的主入口(在名称中)。 That said, there is only one main in your program. 也就是说,程序中只有一个main Nevertheless if you want to have your code wrapped in another class just do it: 不过,如果您想将代码包装在另一个类中,请执行以下操作:

public class MyClass {
    public void myFancyMethod() {
        Scanner input = new Scanner(System.in);
        //....rest of
        //....your code
        counter = input.nextInt();
    }
}

and access it like: 并像这样访问它:

MyClass myClassObject = new MyClass();
myClassObject.myFancyMethod();

You should really start reading (or read them again) the fundamentals of object oriented programming languages, naming conventions etc, because this is something you should understand to make progress in programming. 您应该真正开始阅读(或再次阅读它们)面向对象的编程语言,命名约定等基础知识,因为这是您在编程方面应取得的进步。

Object-Oriented Programming Concepts in Java Java中的面向对象的编程概念

For now, you can just do this in tuna.java to achieve what you want: 现在,您可以在tuna.java中执行此操作以实现所需的功能:

package Gangsta;

public class tuna {
    public static void main(String []args){
        okidoki okidokiObject = new okidoki();
        okidokiObject.main()
    }

}

System.out.println(okidokiObject) prints Gangsta.okidoki@659e0bfd because it is the hashcode of your object (Hashcode is something like an ID, See Object toString() ). System.out.println(okidokiObject)打印Gangsta.okidoki@659e0bfd因为它是对象的哈希码(哈希码类似于ID,请参见Object toString() )。 You usually do not want to print objects, but invoke their methods. 您通常不想打印对象,而是调用它们的方法。

If you change your main method in okidoki class to constructor it will work exactly like you wish ! 如果将okidoki类中的main方法更改为构造函数,它将完全按照您的意愿工作!

Example: 例:

Example.java 范例.java

public class Example {
    public Example() {
      System.out.println("Example class constructed");
    }
}

Main.java Main.java

public class Main {
  public static void main(String[] args) {
    System.out.println("Program started.Constructing Example class");
    Example exClass = new Example();
    System.out.println("Program finished.");
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM