简体   繁体   English

如何创建公共默认构造函数和重载的构造函数?

[英]How do I create a public default constructor and an overloaded constructor?

I am extremely new to Java and coding in general. 我对Java和一般编码非常陌生。 I am working on a project where I have created two interfaces and am now creating a concrete class named VehicleChassis that implements my Chassis interface. 我正在一个项目中创建了两个接口,现在正在创建一个名为VehicleChassis的具体类来实现我的Chassis接口。 First, I created a String named chassisName instance variable. 首先,我创建了一个名为到底盘名称实例变量的字符串。 Now, I need to create a default constructor and overloaded constructor with the following value - A String with a parameter value of chassisName. 现在,我需要使用以下值创建一个默认的构造函数和重载的构造函数-一个带有到底盘参数的String值。 I have searched the Internet on how to do both, but I am so confused. 我已经在互联网上搜索了这两种方法的搜索结果,但是我很困惑。 Help! 救命!

Below is the code I have so far. 下面是我到目前为止的代码。

public abstract class VehicleChassis implements Chassis{
public String chassisName;
}

You mean this? 你是这个意思?

public abstract class VehicleChassis implements Chassis{
    public String chassisName;
    VehicleChassis() {
        chassisName = "name";
    }
    VehicleChassis(final String chassisName) {
        this.chassisName = chassisName;
    }
}

You won't be able to instantiate this VehicleChassis because you've declared it as abstract. 您将无法实例化此VehicleChassis因为您已将其声明为抽象。 You can use the constructors if you extend this class, though. 但是,如果扩展此类,则可以使用构造函数。 Consider declaring the constructors protected if that's what you intend to do. 如果您打算这样做,请考虑声明受保护的构造方法。

If you arre not implement all the methods in Chassis keep VehicleChassis as abstract . 如果您不打算在Chassis实现所有方法,请将VehicleChassis保持为abstract

You can overload constructor as many as you want. 您可以根据需要重载构造函数。

public String chassisName;
public VehicleChassis() {
    chassisName = "No";
}
public VehicleChassis(String chassisName) {
    this.chassisName = chassisName;
}

If you have another instance variable: 如果您还有另一个实例变量:

public VehicleChassis(String chassisName, int price) {
    this.chassisName = chassisName;
    this.price = price;
}

For more about constructor overloading read this question answer . 有关构造函数重载的更多信息,请阅读此问题的答案

You can create constructor as below.If the instance variable is more and all are not mandatory then you can use builder pattern to build your object step by step. 您可以按如下所示创建构造函数。如果实例变量更多,并且不是必需的,则可以使用构建器模式逐步构建对象。

 public class VehicleChassis implements Chassis {
        public String chassisName;

        public VehicleChassis() {
            // some code
        }

        public VehicleChassis(String chassisName) {
            this.chassisName = chassisName;
        }

    }

A class which is labelled as abstract could not be instantiate directly. 标记为抽象的类无法直接实例化。 so we don't need to create constructor and overloaded constructor. 因此我们不需要创建构造函数和重载的构造函数。

暂无
暂无

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

相关问题 如何创建包含 Date 作为 Date 类型的默认构造函数? - How do I create a default constructor including a Date as type Date? 如何从另一个类中的public类型的参数化构造函数中调用默认类型的参数化构造函数? - How can I call parameterized constructor of default type from a parameterized constructor of type public which is in another class? 如何为构造函数创建实例? - How do I create a instance for Constructor? 如何在没有默认构造函数的情况下创建类 - How to create class with no default constructor 如何使用 ByteBuddy 创建默认构造函数? - How to create a default constructor with ByteBuddy? 当我忘记在GWT Serializable对象中创建默认构造函数时,如何让Eclipse警告我? - How do I get Eclipse to warn me when I forget to create a default constructor in a GWT Serializable object? 如何将构造函数传递给构造函数? - How do I pass a constructor to a constructor? 使用在 Java 中调用三参数构造函数的重载默认构造函数? - Using an overloaded default constructor that calls a three parameter constructor in Java? “默认构造函数的隐式超级构造函数BaseTestMethod()未定义。 必须定义一个显式构造函数”如何解决此错误? - “Implicit super constructor BaseTestMethod() is undefined for default constructor. Must define an explicit constructor” How do i fix this error? 在Java中创建默认构造函数 - Create a default constructor in Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM