简体   繁体   English

如何在java中实例化一个对象?

[英]How to instantiate an object in java?

I'm new in programming and I would like to know where did I go wrong in instantiating an object.我是编程新手,我想知道在实例化对象时我哪里出错了。 Below is the code:下面是代码:

public class Testing{
    private int Sample(int c)
    {
        int a = 1;
        int b = 2;
        c = a + b;
        return c;
    }
    public static void main(String []args)
    {
        Sample myTest = new Sample();
        System.out.println(c);
    }
}

There is no Sample class in your code .您的代码中没有Sample类。 The one which you have declared is a private method .您已声明的方法是私有方法。

// private method which takes an int as parameter and returns another int
private int Sample(int c)
{
  int a = 1;
  int b = 2;
  c = a + b;
  return c;
}

With the current snippet , You need to instantiate the Testing class and make use of the Sample method.使用当前代码段,您需要实例化Testing类并使用Sample方法。 Notice your class definition is preceded by the keyword class , in this case class Testing .请注意,您的类定义前面是关键字class ,在本例中为class Testing

public class Testing{
  private int Sample(int c)
  {
    int a = 1;
    int b = 2;
    c = a + b;
    return c;
 }
  public static void main(String []args)
 {
    Testing t = new Testing(); // instantiate a Testing class object
    int result = t.Sample(1); // use the instance t to invoke a method on it
    System.out.println(result);
 }
}

But that doesn't really make sense, your Sample method always returns 3 .但这并没有真正意义,您的Sample方法总是返回3

Are you trying to do something like this :您是否正在尝试做这样的事情:

class Sample {
 int a;
 int b;

 Sample(int a, int b) {
    this.a = a;
    this.b = b;
 }

 public int sum() {
    return a + b;
 }
}

public class Testing {
 public static void main(String[] args) {
    Sample myTest = new Sample(1, 2);
    int sum = myTest.sum();
    System.out.println(sum);
 }
}

I doubt you actually want to create an object.我怀疑你真的想创建一个对象。

From your code snippet, I understand that you want to run a 'method' named Sample which adds two numbers.从您的代码片段中,我了解到您想要运行一个名为Sample的“方法”,该“方法”将两个数字相加。 And in JAVA you don't have to instantiate methods.而在 JAVA 中,您不必实例化方法。 Objects are instances of class .对象是class实例。 A method is just a behavior which this class has.方法只是此类具有的行为。

For your requirement, you don't need to explicitly instantiate anything as when you run the compiled code JAVA automatically creates an instance of your class and looks for main() method in it to execute.根据您的要求,您不需要显式实例化任何内容,因为当您运行编译代码时 JAVA 会自动创建您的类的实例并在其中查找main()方法以执行。

Probably you want to just do following:可能您只想执行以下操作:

public class Testing{
    private int sample(int a, int b) {
        return a + b;
    }
    public static void main(String[] args) {
        int c = sample(1, 2);
        System.out.println(c);
    }
}

Note: I changed Sample to sample as it's generally accepted practice to start a method name with lower-case and class name with an upper-case letter, so Testing is correct on that front.注意:我将Sample更改为sample因为普遍接受的做法是以小写开头的方法名称和以大写字母开头的类名称,因此Testing在这方面是正确的。

You instantiating correctly with new keyword ,But your calss name and method invoking is wrong您使用new关键字正确实例化,但是您的类名称和方法调用是错误的

 Testing myTest = new Testing();
  int result =myTest.Sample(1);  //pass any integer value
  System.out.println(result );

Sample is not a class, it is just a method. Sample 不是一个类,它只是一个方法。 You cannot create instances of it.您不能创建它的实例。 You only run it -你只运行它 -

int sample = Sample(3);

If you wish for sample to be a class, define it as a class.如果您希望样本成为一个类,请将其定义为一个类。

In your case, the method is not static is so you cannot directly access it from the Static method Main.在您的情况下,该方法不是静态的,因此您无法从静态方法 Main 直接访问它。 Make it static so you could access it.将其设为静态,以便您可以访问它。 Or just create a new instance of Testing and use it -或者只是创建一个新的测试实例并使用它 -

Testing testing = new Testing();
int sample = testing.Sample(3);

Sample method returns integer, so get the result and use it anywhere.示例方法返回整数,因此获取结果并在任何地方使用它。

public static void main(String []args)
{
    int myTest = Sample(4555);//input may be any int else
    System.out.println(myTest);
}

This is how you should be doing this.这就是你应该这样做的方式。

public class Testing{
public int Sample(int c)
{
    int a = 1;
    int b = 2;
    c = a + b;
    return c;
}
public static void main(String []args)
{
    // Creating an Instance of Testing Class
    Testing myTest = new Testing();
    int c =0;
    // Invoking the Sample() function of the Testing Class
    System.out.println(myTest.Sample(c));
}

Well , it's easy.嗯,这很容易。 To you instantiate an object in Java you should to use class name and to do with that it class received a valor.要在 Java 中实例化一个对象,您应该使用类名并使用类名来处理它。 For exemplo :例如:

... Car c1 = new Car(); ... Car c1 = new Car();

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

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