构造函数重载的优点 [英] Advantages of Constructor Overloading

查看:134
本文介绍了构造函数重载的优点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Java非常新,试图学习这个主题,只在HTML / CSS中有以前的编程风险。我从赫伯特·希尔特开始,经过几页。



我无法理解构造函数重载的确切优点。使用单个构造函数来灵活地重载方法不是更容易吗?此外,如果我试图使用构造函数重载使用一个对象来初始化另一个,有更简单的方法来做到这一点!

解决方案

构造函数重载对于模拟默认值非常有用,或从现有实例(复制)构建对象

以下是一个示例:

  public class Color {

public int R,G,B,A;

// base ctr
public Color(int R,int G,int B,int A){
this.R = R;
this.G = G;
this.B = B;
this.A = A;
}

// base,default alpha = 255(opaque)
public Color(int R,int G,int B){
this(R,G ,B,255);
}

// btr从双值
public color(double R,double G,double B,double A){
this((int) * 255),(int)(G * 255),(int)(B * 255),(int)(A * 255)
}

// copy ctr
public Color(Color c){
this(c.R,c.G,c.B,c.A);
}

}

这里,第一个构造函数非常简单。您指定R,G,B&颜色的Alpha值。



虽然这足以使用Color类,但您提供了第二个构造函数liter,如果用户未指定,则自动将255分配给alpha。

第三个ctr显示你可以实例化Color对象的双精度在0.和1.而不是整数之间。



最后一个将Color作为唯一参数,它复制给定的对象。



其优点还在于,第一个构造函数总是被调用,你可以使用手动计数您的实例。假设你有一个 private static int count = 0 属性,你可以这样跟踪Color实例的数量:

  // base ctr 
public Color(int R,int G,int B,int A){
this.R = R;
this.G = G;
this.B = B;
this.A = A;
++ count;
}

从任何构造函数。


I am very new to Java and trying to learn the subject, having previous programming exposure in only HTML/CSS. I have started with Herbert Schildt and progressed through a few pages.

I am unable to understand the exact advantages of Constructor Overloading. Isn't it easier to Overload Methods using single constructor for flexibility? Moreover if I am trying to use constructor overloading to use one object to initialize another, there are simpler ways to do it! So what are the benefits and in which situation should I use Constructor Overloading.

解决方案

Constructor overloading is very useful for simulate default values, or to construct an object from an already existing instance (copy)
Here is an example:

public class Color {

    public int  R, G, B, A;

    // base ctr
    public Color(int R, int G, int B, int A) {
        this.R = R;
        this.G = G;
        this.B = B;
        this.A = A;
    }

    // base, default alpha=255 (opaque) 
    public Color(int R, int G, int B) {
        this(R, G, B, 255);
    }

    // ctr from double values
    public Color(double R, double G, double B, double A) {
        this((int) (R * 255), (int) (G * 255), (int) (B * 255), (int) (A * 255));
    }

    // copy ctr
    public Color(Color c) {
        this(c.R, c.G, c.B, c.A);
    }

}

Here, the first constructor is very simple. You specify R,G,B & Alpha value for a color.

While this is enough to use the Color class, you provide a second constructor, liter, which auto assign 255 to alpha if not specified by the user.

The third ctr shows that you can instantiate your Color object with double between 0. and 1. instead of integers.

The last one takes an Color as unique argument, it copies the given object.

The benefits lies also in the fact that the first constructor is always called, and you can use that to manually count your instances. Let say you have a private static int count=0 attribute, you can track the number of Color instance like this:

    // base ctr
    public Color(int R, int G, int B, int A) {
        this.R = R;
        this.G = G;
        this.B = B;
        this.A = A;
        ++count;
    }

countis incremented from any constructor.

这篇关于构造函数重载的优点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆