User defined and standard constructor?

Ok, I'm trying to circle around me:

  • Write an application that creates a class for the student object with the following attributes: Student number, name, address, phone number, and course.

Write a test program that sets and receives each attribute in the class. The testing program should also display all the attributes in the class.

  • Using the student’s class and its attributes from the previous question, write an application (extend the previous program) that includes both a custom and a standard constructor.

Write a test program to demonstrate the use of both constructors.

This is a college worksheet for some revision of objects in Java.

The part that bothers me is the one where it asks for both a custom and a standard constructor?

It seemed to me that this is impossible to do? Because if you do not provide the constructor yourself, the JVM will provide one (default constructor?). But if you define any constructor, then by default it becomes inaccessible?

Is this just a poorly worded task, or could it mean something else?

+4
source share
6 answers

I am sure that the one who created the worksheet had in mind the "No-arg constructor" instead of the "default constructor".

( ), no-arg ( ).

, , .

+7

java, , : thar . , .

, :

class Student {
    String name;
    int age;
    // ...
}

// ...

Student myself = new Student();

:

class Student {
    String name;
    int age;

    // ...

    public Student(String name) {
        this.name = name;
    }
}

// ...

Student myself = new Student(); // compilation error: use new Student("Jhon Smith");

.

+3

Java , . , default, . class A{} java, :

public class A{

   public A(){//auto generated constructor

   }
}

, .

0

, . , "default" , .

:

public class MyClass {

public MyClass () {
}

public MyClass (long studentNumber, String name, String address....) {
}
0

, , OO " " , . , , .

Student(){     //default constructor
   number = 0;
   name = "bob";
   //etc etc
}

student(int nm, int nm, etc etc){   //parametrized constructor
   number = nm;
   name = nm;
   //etc etc
}
0

, Java , . 196:

, , , , 2 , : ("studentName") , 2 ("studentName", " studentId"), , , 3 ("studentName", "studentId", " studentPhoneNumber").

. :

public class student {

    // attributes
    private String studentName;
    private int studentID;
    private int studentPhoneNumber;
    // constructor with one argument
    public student (String studentNameIn) {
        studentName = studentNameIn;
    }

    // constructor with 2 arguments
    public student (String studentNameIn,  int studentIdIn) {
        studentName = studentNameIn;
        studentID = studentIdIn;
    }

    // constructor with 3 arguments
    public student (String studentNameIn,  int studentIdIn, int studentPhoneNumberIn) {
        studentName = studentNameIn;
        studentID = studentIdIn;
        studentPhoneNumber = studentPhoneNumberIn;
    }

    // default constructor REINSERTED no argument
    public student () {}

    // methods
}

. OBJECT, -, , / , 0 ( REINSERTED) 1 , / , 2 .. .

A superclass or several classes are not required, as the constructor may be overloaded, as shown above.

0
source

Source: https://habr.com/ru/post/1530296/


All Articles