不兼容的类型问题

时间:2015-02-05 18:54:09

标签: java

我正在尝试运行此程序,该程序必须包含由其他2个类BuildingTradeCenter扩展的SportCenter类。该程序必须打印3个建筑物(阵列)的信息。问题是我一直在为数组中的int得到Incompatible types issue错误,我不知道如何修复它,因为我是一个新手。有人可以帮忙吗?

class Building {
    String Name;
    String Addres;
    private int floors;

    Building(String N, String A, int F) {
        Name = N;
        Addres = A;
        floors = F;
    }

    public void SetFloors(int f) {
        if (f <= 0)
            floors = 1;
        else
            floors = f;
    }

    public int GetFloors() {
        return floors;
    }

    void Print() {
        System.out.print("Name=" + Name + "Addres=" + Addres + "floors=" + floors);
    }
}

class TradingCenter extends Building {
    String Owner;
    String Brands[] = {"Brand"};

    TradingCenter(String N, String A, int F, String O, String B[]) {
        super(N, A, F);
        Owner = O;
        Brands = B;
    }
}

class SportCenter extends Building {
    String Sports[] = {"Sport"};
    String Teams[] = {"Team"};

    SportCenter(String N, String A, int F, String S[], String T[]) {
        super(N, A, F);
        Sports = S;
        Teams = T;
    }
}

class DemoBuilding {
    public static void main(String args[]) {
        Building arr[] = new Building[3];
        arr[0] = new TradingCenter("Mall Plovdiv", "Smirnenski", 3, "Unknown", "Brand");
        arr[1] = new SportCenter("ATLETIK", "Trakia", 1, "Sport1", "Team3");
        arr[2] = new TradingCenter("Paradise Mall", "Sofia, bul. Cherni vruh 100", 5, "Unknown", "Brand");
        for (int i = 0; i < arr.length; i++)
            arr[i].Print();
    }
}

4 个答案:

答案 0 :(得分:2)

因为您实际上传递的是String,而不是将int作为参数传递。而且,不是传递String [],而是简单地传递字符串。

您对TradingCenter构造函数的定义是

TradingCenter(String N, String A, int F, String O, String B[])  
// 3rd argument is supposed to be of type int here and the last(5th) argument is supposed to be of type String[]

您对SportCenter构造函数的定义是

SportCenter(String N, String A, int F, String S[], String T[])
// 3rd argument is supposed to be of type int here and the second-last(4th) and the last(5th) argument is supposed to be of type String[]

检查以下内容: -

arr[0]=new TradingCenter("Mall Plovdiv","Smirnenski","(int)3","Unknown","Brand 2");   
//  "(int)3" is a String,not int; "Brand 2" is not a String Array
arr[1]=new SportCenter("ATLETIK","Trakia","(int)1","Sport 1","Team 3"); 
//  "(int)1" is a String,not int; "Unknown" & "Brand 2" are Strings,not the String[]
arr[2]=new TradingCenter("Paradise Mall","Sofia, bul. Cherni vruh 100","(int)5","Unknown","Brand 2");
//  "(int)5" is a String,not int; "Brand 2" is not a String Array
  

要简单地使用 3,1,5 作为整数在构造函数调用中直接传递3,1,5,不带引号。

     

此外,使用&#34;未知&#34; &#34;品牌2&#34; 作为 String [] 将它们传递给   新的String [] {&#34; Unknown}和新的String [] {&#34;品牌2&#34;}在构造函数中

答案 1 :(得分:1)

您正在使用此声明

 arr[1] = new SportCenter("ATLETIK", "Trakia", "(int)1", "Sport 1", "Team 3");

"(int)1"这不会将字符串 1 转换为整数,它仍然是字符串。

在需要的地方使用

 arr[1] = new SportCenter("ATLETIK", "Trakia", 1, "Sport 1", "Team 3");

并将字符串解析为整数

Integer inputAsInteger=Integer.parseInt(InputString)

<强>更新

另一个错误是您在字符串数组

中传递字符串

你需要像这样传递

arr[1] = new SportCenter("ATLETIK", "Trakia", 1, new String[]{"Sport 1"}, new String[]{"Team 3"});

答案 2 :(得分:0)

传递的值是A String,即&#34;(int)3&#34;。 &#34;内部的任何内容&#34;被认为是一个字符串。但是你的构造函数除了整数值。 因此要么将constuctor参数更改为String,要么将其作为整数传递(不带引号)。

P.S。如果要将String转换为int,请使用Integer.parseInt(str); e.g。

String str = "8";
int i = Integer.parseInt(str);

i的值现在是8。

答案 3 :(得分:0)

您在main方法中使用的构造函数与类中列出的类型不匹配。

您尝试使用五个连续的字符串参数在main方法中构建TradingCenter,但是,您对交易中心的类定义要求:

(String,String,int,String,String [])

SportCenter存在类似问题......构造函数与类定义不匹配。