【问题标题】:Creating Object Inside Java Method在 Java 方法中创建对象
【发布时间】:2021-01-16 22:23:44
【问题描述】:
public static String[] Form() {
        Scanner Input=new Scanner(System.in); 
        String array[];
        array = new String[2];
        RegistrationForm RegisteredStudents = new RegistrationForm();
        for(int i=0; i<2; i++){
            System.out.println("Enter Name");
            String name= Input.next();
            System.out.println("Enter Address");
            String address= Input.next();
            System.out.println("Enter E-Mail");
            String email= Input.next();
            System.out.println(i+1+" Student Registration complete");
            System.out.println("Your Rollnumber is "+(i+1));
            int roll=i+1;
            String array[i]=new array(name,address,email,roll);
    }
return array;}

是否可以在 java 方法中创建对象而不是在 main 方法中创建对象? & 可以使用单个对象在数组中存储不同的数据吗?

【问题讨论】:

  • 1.是的,您可以从包括 main 在内的任何方法创建对象。 2.可以,只要“不同的数据”与你声明的数组的数据类型相同即可。
  • new array(name,address,email,roll) - 这不会编译。您必须创建一个名为 array 的类,该类必须扩展 String - 这是不可能的,因为 Stringfinal
  • 顺便说一句。请遵循 Java 命名约定 - 变量名以“lowerCamelCase”编写
  • 您正在使用数组来存储某个实体(可能是学生)的不同属性。您应该使用这些属性创建一个类Student。您还应该学习面向对象编程的基础知识。

标签: java oop object instance-variables


【解决方案1】:

是的,您可以在 Java 中以任何类型的方法创建对象。因此,根据您的代码 sn-p,您正在尝试将具有一组属性的学生存储在数组中。为此,我认为最好的方法是使用如果您不确定要注册的学生人数,则使用 ArrayList,但如果您确切知道人数[这意味着如果您可以指定数组大小],您可以使用数组。但是在这两种方式中,您首先需要有一个包含学生属性的模型类。创建Student模型类后,您可以创建一个数组或ArrayList类型的Student。然后您可以将您的学生详细信息存储在选定的方法中。

例如:

第 1 步:创建 Student 模型类

 public class Student {
         
        //Member variables
        private String name;
        private String address;
        private String email;
        private int roll;
    
        //Default constructor
        public Student(){}

       //Overloaded constructor
        public Student(String name, String address, String email, int roll) {
            this.name = name;
            this.address = address;
            this.email = email;
            this.roll = roll;
        }

        //Getters and Setters
        public void setRoll(int roll){
             this.roll = roll;
        }

        public int getRoll() {
            return roll;
        }
    
        public void setName(String name){
            this.name =name;   
        }
        public String getName() {
            return name;
        }
        public void setAddress(String address){
            this.address = address;
        }
       
        public String getAddress() {
            return address;
        }
         public void setEmail(String email){
            this.email = email;
         }
        public String getEmail() {
            return email;
        }

    //Better if you can also implement the toString() method in here
    }

第 2 步:ArrayList 或数组实现

使用数组实现

public class Form{

     public static void main(String[] args) {
            Scanner Input = new Scanner(System.in);
            //Creation of Student type array
            Student studentsArr[] = new Studnet[10]; //Considering there are only 10 Students to register
            for (int i = 0; i < 10; i++) {
                System.out.println("Enter Name");
                String name = Input.next();
                System.out.println("Enter Address");
                String address = Input.next();
                System.out.println("Enter E-Mail");
                String email = Input.next();
                int roll = i + 1;
                studentsArr[i] = new Student(name, address, email, roll);
                System.out.println(i + 1 + " Student Registration complete");
                System.out.println("Your Rollnumber is " + (i + 1));
            }
        }
}


使用 ArrayList 实现

public class Form{

     public static void main(String[] args) {
            Scanner Input = new Scanner(System.in);
            //Creation of Student type array
            List<Student> studentsList = new ArrayList<>(); //Doesn't know the students count ,because of that we are using a list
            int end = 0;//Terminating condition initial value
            int key = -1;
            do{
                key++;
                System.out.println("Enter Name");
                String name = Input.next();
                System.out.println("Enter Address");
                String address = Input.next();
                System.out.println("Enter E-Mail");
                String email = Input.next();
                int roll = key + 1;
                studentsList.add(new Student(name, address, email, 
roll));//Adding the new student to list
                 System.out.println(key + 1 + " Student Registration complete");
                System.out.println("Your Rollnumber is " + (key + 1));
                System.out.println("press 0 to continue or -1 to end the process");
                end = input.nextInt();

            }while(end != -1);
      }
}


【讨论】:

    【解决方案2】:

    这样的事情就可以解决问题。

    我建议您使用 Arraylist 而不是数组,但我认为这是一些课堂作业,所以我不会过多地修改您的示例。

    import java.util.Scanner;
    
    class Customer {
    
        private String name;
        private String address;
        private String email;
        private int roll;
    
        public Customer(String name, String address, String email, int roll) {
            this.name = name;
            this.address = address;
            this.email = email;
            this.roll = roll;
        }
    
        public int getRoll() {
            return roll;
        }
    
        public String getName() {
            return name;
        }
    
        public String getAddress() {
            return address;
        }
    
        public String getEmail() {
            return email;
        }
    }
    
    class Scratch {
        public static void main(String[] args) {
            Scanner Input = new Scanner(System.in);
            Customer customers[] = new Customer[10];
            for (int i = 0; i < 2; i++) {
                System.out.println("Enter Name");
                String name = Input.next();
                System.out.println("Enter Address");
                String address = Input.next();
                System.out.println("Enter E-Mail");
                String email = Input.next();
                System.out.println(i + 1 + " Student Registration complete");
                System.out.println("Your Rollnumber is " + (i + 1));
                int roll = i + 1;
                customers[i] = new Customer(name, address, email, roll);
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-28
      • 1970-01-01
      • 2015-05-22
      • 1970-01-01
      • 1970-01-01
      • 2018-03-27
      • 1970-01-01
      相关资源
      最近更新 更多