Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我可以采取什么步骤来修正“警告”;参数化类的原始使用';类别'&引用;?_Java_Class_Object_Oop_Inheritance - Fatal编程技术网

Java 我可以采取什么步骤来修正“警告”;参数化类的原始使用';类别'&引用;?

Java 我可以采取什么步骤来修正“警告”;参数化类的原始使用';类别'&引用;?,java,class,object,oop,inheritance,Java,Class,Object,Oop,Inheritance,我正在运行以下程序。在这个程序中,我有Cow类,Dragon类派生自Cow类,IceDragon类派生自Dragon类。Cow类、Dragon类和Ice Dragon类在名为HeiferGenerator的类中实现。我在一个名为CowSay的类中运行main函数,在这个类中,我实现了HeiferGenerator类以及Cow类、Dragon类和IceDragon类。但是,请看下面的HeifferGenerator类。我在以下行中收到警告“参数化类'class'的原始使用”: private st

我正在运行以下程序。在这个程序中,我有Cow类,Dragon类派生自Cow类,IceDragon类派生自Dragon类。Cow类、Dragon类和Ice Dragon类在名为HeiferGenerator的类中实现。我在一个名为CowSay的类中运行main函数,在这个类中,我实现了HeiferGenerator类以及Cow类、Dragon类和IceDragon类。但是,请看下面的HeifferGenerator类。我在以下行中收到警告“参数化类'class'的原始使用”:

private static final Class[] dragonTypes = {Dragon.class, IceDragon.class};
我试着看看答案,但没有任何帮助。如何修复此警告

// Cow class

public class Cow {

    // Declaring attributes name and image
    private final String name;
    private String image;

    // Constructor to create a new Cow object with parameter name
    public Cow (String name) {
        this.name = name;
        this.image = null;
    }

    // Accessor to return the name of the cow
    public String getName() {
        return this.name;
    }

    // Accessor to return the image used to display the cow after the message
    public String getImage() {
        return this.image;
    }

    // Mutator to set the image used to display the cow after the message
    public void setImage(String _image) {
        this.image = _image;
    }
}
enter code here

// Dragon class derived from the Cow class

public class Dragon extends Cow {
    // Constructor to create a new Dragon object with parameters name and image
    public Dragon (String name, String image) {
        super(name);
        setImage(image);
    }

    // Function to return true for the default Dragon type
    public boolean canBreatheFire() {
        return true;
    }
}



// IceDragon class derived from the Dragon class

public class IceDragon extends Dragon {
    // Constructor to create a new IceDragon object with parameters name and image
    public IceDragon (String name, String image) {
        super(name, image);
    }

    // Function to return false for the IceDragon type
    public boolean canBreatheFire() {
        return false;
    }
}
  


import java.lang.reflect.Constructor;

public class HeiferGenerator
{
    public static Cow[] getCows()
    {
        if (cows == null)
        {
            cows = new Cow[cowNames.length + dragonNames.length];

            // Add the "regular" cows
            for (int index = 0; index < cowNames.length; index++)
            {
                cows[index] = new Cow(cowNames[index]);
                cows[index].setImage(quoteLines + cowImages[index]);
            }

            // Add the dragons
            for (int offset = cowNames.length, index = 0; index < dragonNames.length; index++)
            {
                try
                {
                    @SuppressWarnings("unchecked")
                    Constructor<Dragon> constructor = dragonTypes[index].getConstructor(String.class, String.class);
                    cows[offset + index] = constructor.newInstance(dragonNames[index], quoteLines + dragonImage);
                }
                catch (Exception ignored) { }
            }
        }

        return cows;
    }

    // Hard-coded values for some of the cows
    private static final String[] cowNames = { "heifer", "kitteh" };

    private static final String quoteLines =        "       \\\n" +
            "        \\\n" +
            "         \\\n";

    private static final String[] cowImages = { "        ^__^\n" +
            "        (oo)\\_______\n" +
            "        (__)\\       )\\/\\\n" +
            "            ||----w |\n" +
            "            ||     ||\n",


            "       (\"`-'  '-/\") .___..--' ' \"`-._\n" +
                    "         ` *_ *  )    `-.   (      ) .`-.__. `)\n" +
                    "         (_Y_.) ' ._   )   `._` ;  `` -. .-'\n" +
                    "      _.. `--'_..-_/   /--' _ .' ,4\n" +
                    "   ( i l ),-''  ( l i),'  ( ( ! .-'\n"
    };

    private static final  String[] dragonNames = { "dragon", "ice-dragon" };
    private static final Class[] dragonTypes = {Dragon.class, Dragon.class};


    private static final String dragonImage =     "           |\\___/|       /\\  //|\\\\\n" +
            "           /0  0  \\__   /  \\// | \\ \\\n" +
            "          /     /  \\/_ /   //  |  \\  \\\n" +
            "          \\_^_\\'/   \\/_   //   |   \\   \\\n" +
            "          //_^_/     \\/_ //    |    \\    \\\n" +
            "       ( //) |        \\ //     |     \\     \\\n" +
            "     ( / /) _|_ /   )   //     |      \\     _\\\n" +
            "   ( // /) '/,_ _ _/  ( ; -.   |    _ _\\.-~       .-~~~^-.\n" +
            " (( / / )) ,-{        _      `.|.-~-.          .~         `.\n" +
            "(( // / ))  '/\\      /                ~-. _.-~      .-~^-.  \\\n" +
            "(( /// ))      `.   {            }                 /      \\  \\\n" +
            " (( / ))     .----~-.\\        \\-'               .~         \\  `.   __\n" +
            "            ///.----..>        \\            _ -~            `.  ^-`  \\\n" +
            "              ///-._ _ _ _ _ _ _}^ - - - - ~                   `-----'\n";

      private static Cow[] cows = null;

  }
//Cow类
公营母牛{
//声明属性名称和图像
私有最终字符串名;
私有字符串图像;
//构造函数创建具有参数名的新Cow对象
公共Cow(字符串名称){
this.name=名称;
this.image=null;
}
//访问器返回奶牛的名称
公共字符串getName(){
返回此.name;
}
//访问者返回用于在消息后显示cow的图像
公共字符串getImage(){
返回此.image;
}
//Mutator设置用于在消息后显示cow的图像
公共void setImage(字符串\u图像){
this.image=\u image;
}
}
在这里输入代码
//龙类派生自Cow类
公营龙牛{
//构造函数创建具有参数名称和图像的新Dragon对象
公共龙(字符串名称、字符串图像){
超级(姓名);
设置图像(图像);
}
//函数为默认的Dragon类型返回true
公共布尔canBreatheFire(){
返回true;
}
}
//IceDragon类派生自Dragon类
公共级冰龙{
//构造函数使用参数名称和图像创建新的IceDragon对象
公共IceDragon(字符串名称、字符串图像){
超级(名称、图像);
}
//函数返回IceDragon类型的false
公共布尔canBreatheFire(){
返回false;
}
}
导入java.lang.reflect.Constructor;
公共类HeiferGenerator
{
公共静态Cow[]getCows()
{
if(cows==null)
{
cows=新的Cow[cowNames.length+dragonNames.length];
//添加“普通”奶牛
对于(int index=0;index\\\\-~`.^-`\\\\n”+
“//-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-;
私有静态Cow[]cows=null;
}

TL;DR:您必须使用数组吗?如果不需要,请使用列表

替换此项:

Constructor=dragonTypes[index].getConstructor(String.class,String.class);

为此:

Constructor=dragonTypes.get(index).getConstructor(String.class,String.class);


这是:

private static final Class[]dragonTypes={Dragon.Class,Dragon.Class};

为此:


private static final List dragonTypes=Arrays.asList(Dragon.class,Dragon.class);
不允许实例化
private static final Class<?>[] dragonTypes = {Dragon.class, IceDragon.class};
@SuppressWarnings("unchecked")
Constructor<Dragon> constructor =(Constructor<Dragon>) dragonTypes[index].getConstructor(String.class, String.class);