如何在java中编写包含自定义类作为参数的默认构造函数

wooyq4lh  于 2021-07-06  发布在  Java
关注(0)|答案(2)|浏览(415)

我正试着为一个大学班写一个程序,但我被困在构造器上了。我的教授希望我们使用“this”作为默认构造函数,但我不知道如何编写第三个示例。我已附上相关代码。person类是我在另一个src文件中定义的类。

public class Vehicle {
private String manufacturer;
private int cylinders;
private Person owner;

public void setManufacturer (String mf) { manufacturer = new String (mf); }
public String getManufacturer () { return new String (manufacturer); }
public void setCylinders (int c) { cylinders = c; }
public int getCylinders () { return cylinders; }
public void setOwner (Person own) { owner = new Person (own); }
public Person getOwner () { return new Person (owner); }

public Vehicle () {     //constructor
    this ("",0, ???);  //this is the line where I get the error
}

下面是正在引用的person类:

// ---------------------------------------------------
//                    Person.java
// ---------------------------------------------------

public class Person {
    // Instance Variables/Data

    private StringBuffer firstName;         // Person's First Name
    private StringBuffer lastName;          // Person's Last Name
    private int age;                        // Person's Age
    private String ssn;                     // Person's Social Security Number
    private static int population;          // Number of Person objects
    private boolean finalizeNotCalled;      // Flag to prevent multiple finalize executes

    // Class Behaviors -- Getters and Setters

    public void         setFirstName ( String fn ) { firstName = new StringBuffer( fn ); }
    public StringBuffer getFirstName( )            { return new StringBuffer( firstName.toString()); }
    public void         setLastName ( String ln )  { lastName = new StringBuffer( ln ); }
    public StringBuffer getLastName( )             { return new StringBuffer( lastName.toString()); }
    public void         setAge ( int a )           { age = a; }
    public int          getAge ( )                 { return age; }
    public void         setSsn ( String s )        { ssn = s; }
    public String       getSsn ( )                 { return ssn; }
    public static int   getPopulation ( )          { return population; }

    public Person ( ) {
        this ( " ", " ", 0, " ");
        System.out.println( "Person - default, no-arg constructor ");
    }

    public Person ( String fn, String ln, int a, String s ) {
        firstName = new StringBuffer( fn );
        lastName  = new StringBuffer( ln );
        age = a;
        ssn = s;
        population++;
        finalizeNotCalled = true;
        System.out.println( "Person - Overloaded 4-arg constructor ");
    }

    public Person ( String fn, String ln ) {
        // Some comment
        this ( fn, ln, 0, " ");
        System.out.println( "Person - Overloaded 2-arg constructor ");
    }

    public Person ( Person original ) throws NullPointerException {

        if ( original == null )
            throw new NullPointerException( "null argument passed to Person copy constructor" );

        firstName = new StringBuffer( original.firstName.toString());
        lastName  = new StringBuffer( original.lastName.toString() );
        age = original.age;
        ssn = "999-45-6789";
        population++;
        finalizeNotCalled = true;
        System.out.println( "Person - copy constructor ");
    }

    public Person clone ( ) {
        return new Person(this);
    }

    static {
        population = 0;
        System.out.println("Person - Static initializer block.");
    }

    @Override
    public String toString ( ) {
        String x;
        x = " " + firstName + " " + lastName + " " + age +
                " " + ssn + " " + population + " ";
        return x;
    }

    @Override
    public boolean equals ( Object obj ) {
        if ( this == obj ) return true;
        if ( obj == null ) return false;
        if ( getClass() != obj.getClass() ) return false;

        Person d = (Person) obj;   // Creating a copy of the reference, not an object

        if ( firstName.toString().equals ( d.firstName.toString()) &&
                lastName.toString(). equals ( d.lastName.toString())  &&
                age == d.age  &&
                finalizeNotCalled == d.finalizeNotCalled )
        {
            return true;
        }
        else
            return false;
    }

    @Override
    public void finalize ( ) {
        if ( finalizeNotCalled ) {
            population--;
            finalizeNotCalled = false;
            System.out.println ( "Person - finalize method - " + toString() );
        }
    }
}
hwamh0ep

hwamh0ep1#

此(…)必须是构造函数的第一个语句,请删除super();
这个(…)用给定的参数调用构造函数。
例子:

public Vehicule() {
  this("default manufacturer"); // call Vehicule(String manufacturer)
}

public Vehicule(String manufacturer) {
  this.manufacturer = manufacturer;
}
4dbbbstv

4dbbbstv2#

super() 以及 this(...) 不能一起使用。我想你需要这样的东西:

...

    // Default no-arg constructor
    // Calls the other constructor, supplying default values
    public Vehicle() {
        this("", 0, new Person());
    }

    public Vehicle(String manufacturer, int cylinders, Person owner) {
        this.manufacturer = manufacturer;
        this.cylinders = cylinders;
        this.person = person;
    }

    ...

祝你好运!

相关问题