构造函数重载错误

时间:2015-12-06 06:48:51

标签: java constructor overloading

我无法弄清楚错误!但是在编译时会显示错误。请帮帮我.....

// This program is used to find the area of a circle and a rectangle    
// through constructor overloading concept.

class area {
    float radius;
    int l , b;
    public area(float r) {
        radius=r;
    }
    public area(int a , int d) {
        l=a;
        b=d;
    }
    public void display() {
        System.out.println("Area of Circle is = "+(3.14*radius*radius));
        System.out.println("Area of Rectangle is = "+(l*b));
    }
}

class constadd {
    public static void main(String arr[]) {
        area c = new area(4.5);
        c.display();
        area e=new area(4,5);
        e.display();
    }
}`

2 个答案:

答案 0 :(得分:1)

使用double而不是float。

import java.util.*;
import java.lang.*;
import java.io.*;


class area {
double radius;
int l , b;
public area(double r) {
    radius=r;
}
public area(int a , int d) {
    l=a;
    b=d;
}
public void display() {
    System.out.println("Area of Circle is = "+(3.14*radius*radius));
    System.out.println("Area of Rectangle is = "+(l*b));
}
}

class  Ideone {
    public static void main(String arr[]) {
    area c = new area(4.5);

    c.display();
     area e=new area(4,5);
    e.display();
}
}

答案 1 :(得分:0)

正如Anik所提到的,要么将构造函数更改为double而不是float,要么在调用此构造函数时使用后缀4.5和'f'来指定要传递float,即新区域(4.5f); < / p>