有一些类似的主题,但我找不到一个有足够答案的主题.
我想知道在Java中构造函数重载的最佳实践是什么.我已经对这个问题有了自己的想法,但我想听听更多建议.
我指的是简单类中的构造函数重载和构造函数重载,同时继承已经重载的类(意味着基类具有重载的构造函数).
谢谢 :)
我如何在C#中使用构造函数,如下所示:
public Point2D(double x, double y)
{
// ... Contracts ...
X = x;
Y = y;
}
public Point2D(Point2D point)
{
if (point == null)
ArgumentNullException("point");
Contract.EndContractsBlock();
this(point.X, point.Y);
}
Run Code Online (Sandbox Code Playgroud)
我需要它不要从另一个构造函数复制代码...
我无法理解为什么使用参数执行构造函数Double[]?
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MyConsoleApp
{
class Program
{
static void Main(string[] args)
{
D myD = new D(null);
Console.ReadLine();
}
}
public class D
{
public D(object o)
{
Console.WriteLine("Object");
}
public D(double[] array)
{
Console.WriteLine("Array");
}
public D(int i)
{
Console.WriteLine("Int");
}
}
}
Run Code Online (Sandbox Code Playgroud)
我认为因为第一个构造函数采用引用类型的参数.带引号参数的第一个构造函数因为null是引用类型的默认值.
但我不明白为什么不object,它也是一个参考类型.
我遇到了这段代码,有一条我不放弃了解它的含义或它正在做什么.
public Digraph(In in) {
this(in.readInt());
int E = in.readInt();
for (int i = 0; i < E; i++) {
int v = in.readInt();
int w = in.readInt();
addEdge(v, w);
}
}
Run Code Online (Sandbox Code Playgroud)
我明白了什么this.method()或this.variable有,但什么是this()?
#include <iostream>
struct uct
{
uct() { std::cerr << "default" << std::endl; }
uct(const uct &) { std::cerr << "copy" << std::endl; }
uct( uct&&) { std::cerr << "move" << std::endl; }
uct(const int &) { std::cerr << "int" << std::endl; }
uct( int &&) { std::cerr << "int" << std::endl; }
template <typename T>
uct(T &&) { std::cerr << "template" << std::endl; }
};
int main()
{
uct u1 ; // default
uct u2( 5); // int
uct u3(u1); …Run Code Online (Sandbox Code Playgroud) c++ copy-constructor constructor-overloading function-templates overload-resolution
在讨论FileInputStream之前,我首先介绍了一个场景,其中有两个完全有效的重载方法,但编译器会感到困惑,然后报告编译时错误以响应某些输入.
这是方法.
double calcAverage(double marks1, int marks2) {
return (marks1 + marks2)/2.0;
}
double calcAverage(int marks1, double marks2) {
return (marks1 + marks2)/2.0;
}
Run Code Online (Sandbox Code Playgroud)
以下是显示方法用法的完整代码:
class MyClass {
double calcAverage(double marks1, int marks2) {
return (marks1 + marks2)/2.0;
}
double calcAverage(int marks1, double marks2) {
return (marks1 + marks2)/2.0;
}
public static void main(String args[]) {
MyClass myClass = new MyClass();
myClass.calcAverage(2, 3);
}
}
Run Code Online (Sandbox Code Playgroud)
因为int文字值可以传递给double类型的变量,所以这两个方法都是文字值2和3的可接受候选者,因此编译器无法决定选择哪个方法.
当我对我采用上述概念,进一步深入Java 7 API到FileInputStream类,并研究该类的两个重载构造函数时,我就会感到困惑.
为了允许std::string从std::string_view模板构造函数进行构造
template<class T>
explicit basic_string(const T& t, const Allocator& alloc = Allocator());
Run Code Online (Sandbox Code Playgroud)
const T&仅当可转换为std::basic_string_view<CharT, Traits>(链接)时才启用。
同时有专门的推演指南可以推演basic_string(basic_string_view链接)。该指南的评论说:
需要指南 (2-3),因为 std::basic_string_views 的 std::basic_string 构造函数被制作为模板,以避免在现有代码中引起歧义,并且这些模板不支持类模板参数推导。
所以我很好奇,需要有演绎指南和模板构造函数而不是简单的构造函数的歧义是什么std::basic_string_view,例如类似的东西
explicit basic_string(basic_string_view<CharT, Traits> sv, const Allocator& alloc = Allocator());
Run Code Online (Sandbox Code Playgroud)
请注意,我并不是问为什么构造函数被标记为显式。
可以说我有类Date和类Year,Month和Day.
struct Date {
Date(Year year, Month month, Day day) : d(day), m(month), y(year) {};
Date(Month month, Day day, Year year) : d(day), m(month), y(year) {};
Date(Day day, Month month, Year year) : d(day), m(month), y(year) {};
Date(Day day, Year year, Month month) : d(day), m(month), y(year) {};
...
...
private:
Day d;
Month m;
Year y;
}
Run Code Online (Sandbox Code Playgroud)
这允许我没有特定的参数布局,Date因为我有很多过载.
我能自动生成所有排列/过载吗?
只是要清楚:
我创建了以下类:
export class MyItem {
public name: string;
public surname: string;
public category: string;
public address: string;
constructor();
constructor(name:string, surname: string, category: string, address?: string);
constructor(name:string, surname: string, category: string, address?: string) {
this.name = name;
this.surname = surname;
this.category = category;
this.address = address;
}
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
过载签名与功能实现不兼容
我尝试了几种方法来重载构造函数,我尝试的最后一个是我在上面发布的(我从这里得到).
但我仍然得到同样的错误.我的代码出了什么问题?
constructor class constructor-overloading typescript angular
是否有可能重载的构造函数以某种方式调用类中的另一个构造函数,类似于下面的代码?
class A {
public:
A(std::string str) : m_str(str) {}
A(int i) { *this = std::move(A(std::to_string(i))); }
std::string m_str;
};
Run Code Online (Sandbox Code Playgroud)
上面的代码有效,但我担心在构造函数中调用它可能会导致未定义的行为.
如果确实如此,请解释原因并提出更好的选择?
constructor ×6
c++ ×4
java ×3
c# ×2
overloading ×2
this ×2
angular ×1
c++11 ×1
c++17 ×1
class ×1
rvalue ×1
string ×1
string-view ×1
typescript ×1