简体   繁体   English

Java:如何将参数传递给构造函数? 未定义的方法错误?

[英]Java: how to pass arguments into constructor? Undefined method error?

Ok, so I'm in need of some basic help. 好的,所以我需要一些基本的帮助。 Here's the tutorial I'm trying to learn from ( http://docs.oracle.com/javase/tutorial/java/javaOO/classes.html ), but I'm confused as to how to actually pass data into it. 这是我想从中学习的教程( http://docs.oracle.com/javase/tutorial/java/javaOO/classes.html ),但是我对如何实际将数据传递给它感到困惑。 The problem is I've got my Pascal brain on when trying to learn java... 问题是尝试学习Java时我的Pascal头脑已经开始...

Here's my code. 这是我的代码。 What am I doing wrong? 我究竟做错了什么?

public class OrigClass{
    public static void main(String[] args){
        StudentData(17, "Jack"); //error here: the method StudentData(int, String) is undefined for the type OrigClass
    }

    public class Student{
        public void StudentData(int age, String name){
            int sAge = age;
            String sName = name;
            System.out.println("Student Name: " + sName + " | Student Age: " + sAge);
        }
    }
}

Thanks in advance for the help :) 先谢谢您的帮助 :)

Constructor is not just a method: you need to give it the same name as the class, and call it with a new operator, like this: 构造函数不仅仅是一个方法:您需要给它一个与类相同的名称,并使用一个new运算符来调用它,如下所示:

public class Student{
    // Declare the fields that you plan to assign in the constructor
    private int sAge;
    private String sName;
    // No return type, same name as the class
    public Student(int age, String name) {
        // Assignments should not re-declare the fields
        sAge = age;
        sName = name;
        System.out.println("Student Name: " + sName + " | Student Age: " + sAge);
    }
}
// This goes in the main()
Student sd = new Student(17, "Jack");

If I am assuming you have written Student class correctly without considering Java naming convention in mind and StudentData is method then the way to call method StudentData is incorrect.First create object of Student class and then call the method 如果我假设您没有考虑Java命名约定就正确编写了Student类,而StudentData是方法,则调用方法StudentData方法是不正确的。首先创建Student类的对象,然后调用该方法

Update: Considering Student is inner class 更新:考虑到学生是内部阶级

 public static void main(String[] args){
    new OrigClass().new Student().StudentData(17, "Jack");// Considering Student is inner class
  }

There are several issues in your code. 您的代码中有几个问题。

Firstly, you've defined the constructor of StudentData like a normal method - constructors have no return type. 首先,您已经像普通方法一样定义了StudentData的构造函数-构造函数没有返回类型。

Secondly, you need use the new keyword to create a non-primitive object in Java. 其次,您需要使用new关键字在Java中创建一个非原始对象。

public class OrigClass{
    public static void main(String[] args){
        new Student(17, "Jack"); 
    }
}

public class Student{
   public Student(int age, String name){
       int sAge = age;
       String sName = name;
       System.out.println("Student Name: " + sName + " | Student Age: " + sAge);
   }
}
public class OrigClass {

    public static void main(String[] args) {
        Student obj = new Student();
        obj.studentData(17, "Jack");

    }
}

public class Student {   

    public void studentData(int sName, String sAge) {
        System.out.println("Student Name: " + sName + " | Student Age: " + sAge);
    }
}

so, first of all, you can't access a non-static member( StudentData(int age, String name) ) from within a static method( public static void main(String[] args) ). 因此,首先,您不能从静态方法( public static void main(String [] args) )内部访问非静态成员( StudentData(int age,String name )。 So if you want to access the method from within your static main method you would need to do the following: 因此,如果要从静态main方法内部访问该方法,则需要执行以下操作:

  1. Create an alternate method(for eg: name it xyz()), that will create an object(eg: std) for your inner class Student and then calls the StudentData method with the syntax std.StudentData(17,"JACK");. 创建一个替代方法(例如:将其命名为XYZ()),这将创建一个对象:为你的内部类学生(例如STD),然后调用StudentData方法与语法std.StudentData(17,“杰克”); 。
  2. Next thing call this alternate method from the static main method by creating an object (eg: obj) of the outer class and invoking the alternate method with the help of this obj like obj.alternateMethodName(); 接下来,通过创建外部类的对象(例如obj)并通过此obj(如obj.alternateMethodName();)调用替代方法,从静态main方法中调用此替代方法。

Here's the complete code showing how you can achieve the desired output: 以下是完整的代码,显示了如何实现所需的输出:

public class OrigClass{
public static void main(String[] args){
    OrigClass obj= new OrigClass();
    obj.getStudentData();
}
public void getStudentData(){
    Student std = new Student();
    std.StudentData(17, "Jack");
}

public class Student{
    public void StudentData(int age, String name){
        int sAge = age;
        String sName = name;
        System.out.println("Student Name: " + sName + " | Student Age: " + sAge);
    }
}

} }

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

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