java - 多态性:构造函数不接受参数

标签 java constructor arguments polymorphism

当我编译这个时,我收到一条错误消息,指出 Video 的预期构造函数参数是“无参数”

import java.util.*;

abstract class Thing
{

}

class Video extends Thing
{
    String description;
    int price;
    String title;

    void Video ( String d , int p, String t)
    {
        this.description = d;
        this.price = p;
        this.title = t;
    }

    String getDescription()
    {
        return description;
    }
}

class BookOnTape extends Thing
{
    String description;

    void BookOnTape ( String d )
    {
        description = d;
    }

    String getDescription()
    {
        return description;
    }
}

class Funiture extends Thing
{
   String description;

    void Furniture ( String d )
    {
        description = d;
    }

    String getDescription()
    {
        return description;
    }
}

public class Lookup
{
    /*private static HashMap<Thing, Integer> rentalList;

    static
    {
        rentalList = new HashMap<Thing, Integer>();
        rentalList.put( new Video(5), new Integer( 0 ) );
    }*/

    public static void main( String args[] )
    {
        System.out.println("fart");

        Thing farmer = new Video( "fgfg", 5, "gfgf" );
    }
}

当我编译这个时:

class A
{
}

class B extends A
{
    String name;

    B ( String n )
    {
        this.name = n;
    }
}


class TestPoly
{
    public static void main( String args[] )
    {
        A poly = new B( "test" );
    }
}

效果很好。我看不出有什么区别。这里有什么问题吗?...

最佳答案

void Video ( String d , int p, String t)

这不是 constructor 。这就是编译器提示的原因:它找不到构造函数 Video(String, int, String) 因为它不存在于代码中。删除 void 会将其变成一个,因为方法需要返回值,而构造函数没有返回值。

(更多)信息丰富的讨论是 here .

关于java - 多态性:构造函数不接受参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15283375/

相关文章:

java - 如何在android中获取第三方应用程序的详细信息?

java - 尚未应用带有 AspectJ 抛出建议的删除向导指标

java - Rpg 调用 Java 构造函数,在异常处理程序上出现错误 ClassDefNotFound

javascript - knockout JS : Passing observable array as argument

java - 无法连接Hadoop

Eclipse 4.9 上的 Java 11 和 E(fx)clipse JavaFX 插件 : An error has occurred - see the log file

c# - 坚持对象创建

Java 的 Date(...) 构造函数已弃用;这意味着什么?

C 默认参数

python - 参数中的 RandomForest 分类器对象