Nullary constructor
Appearance
From Wikipedia, the free encyclopedia
In programming, an object-creating function that takes no arguments
In computer programming, a nullary constructor is a constructor that takes no arguments.[1] Also known as a 0-argument constructor, no-argument constructor,[2] parameterless constructor or default constructor.[3]
Object-oriented constructors
[edit ]In object-oriented programming, a constructor is code that is run when an object is created. Default constructors of objects are usually nullary.[4]
Java example
[edit ]publicclass MyInteger{ privateintdata; // Nullary constructor publicMyInteger(){ this(0); } // Non-nullary constructor publicMyInteger(intvalue){ this.data=value; } intgetData(){ returndata; } voidsetData(intvalue){ data=value; } }
C++ example
[edit ]classInteger{ private: intdata; public: // Default constructor with parameters // Leaving parameters unspecified defaults to the default value Integer(intvalue=0): data{value}{} [[nodiscard]] intgetData()constnoexcept{ returndata; } voidsetData(intvalue)noexcept{ data=value; } }
Algebraic data types
[edit ]In algebraic data types, a constructor is one of many tags that wrap data. If a constructor does not take any data arguments, it is nullary.
Haskell example
[edit ]-- nullary type constructor with two nullary data constructors dataBool=False |True -- non-nullary type constructor with one non-nullary data constructor dataPointa=Pointaa -- non-nullary type constructor with... dataMaybea=Nothing-- ...nullary data constructor |Justa-- ...unary data constructor
See also
[edit ]References
[edit ]- ^ "Default Constructor in Java – Class Constructor Example". freeCodeCamp.org. 2022年01月13日. Retrieved 2022年03月23日.
- ^ "No-argument Constructor". chortle.ccsu.edu. Retrieved 2022年03月23日.
- ^ "Default constructors - cppreference.com". en.cppreference.com. Retrieved 2023年04月12日.
- ^ Ottinger, Joseph B.; Linwood, Jeff; Minter, Dave (2022), Ottinger, Joseph B.; Linwood, Jeff; Minter, Dave (eds.), "An Introduction to Hibernate 6" , Beginning Hibernate 6: Java Persistence from Beginner to Pro, Berkeley, CA: Apress, pp. 1–25, doi:10.1007/978-1-4842-7337-1_1, ISBN 978-1-4842-7337-1 , retrieved 2022年03月23日
Stub icon
This computer science article is a stub. You can help Wikipedia by expanding it.