简体   繁体   English

具有抽象多态性和继承的类分配错误

[英]class assignment error with abstract polymorphism and inheritance

public class LawClient extends Client
{
    boolean defendant;
    String caseTopic;

        LawClient (String n, boolean d)
    {
        name = n;
        defendant = d;  
    }

        LawClient (String n, boolean d, String c, String e)
    {
        name = n;
        defendant = d;
        caseTopic = c;
        email = e;
    }

    public String determineStatus()
    {
        if(caseTopic == null)
        {
            return "none";
        }
        else
        {
        String s = "";
        s += "defendant: " + defendant +"\n" + "CaseTopic: " + caseTopic;
        return s;
        }
    }
}

I get 2 errors for no sutable constructor for the lawclient constructors but don't know what I did wrong or how to fix it . 对于Lawclient构造函数没有可构造函数,我遇到2个错误,但是不知道我做错了什么或如何解决。

Here is super class so you can run it or look at it. 这是超类,因此您可以运行它或查看它。

abstract class Client
{
    protected String name;
    protected double balance;
    protected String email;

    Client (String n)
    {
        name = n;
    }

    Client (String n, String e)
    {
        name = n;
        email = e;
    }


    public String getName()
    {
        return name;
    }

    public double getBalance()
    {
        return balance;
    }

    public String getEmail()
    {
        return email;
    }

    public String setName(String a)
    {
        name = a;
        return name;
    }

    public double adjustBalance(double b)
    {
        balance = b;
        return balance;
    }

    public String setEmail(String e)
    {
        email = e;
        return email;
    }

    public abstract String determineStatus();

    public String toString()
    {
        String a = "";
        a += "name: " +name + ("\n")+"Balance: " +balance + ("\n")+"Email: " +email + ("\n");
        return a;
    }
}

The problem is how constructors work in Java for inherited classes. 问题是构造函数如何在Java中为继承的类工作。 If you do not specify a call to a constructor of the parent class, Java automatically inserts the super() method at the top of the constructor. 如果您未指定对父类的构造函数的调用,则Java会自动在构造函数的顶部插入super()方法。

For the following constructor of LawClient: 对于以下LawClient的构造函数:

LawClient (String n, boolean d)
{
    name = n;
    defendant = d;  
}

Java is making a call to super() before attempting to assign n to name, but there is no constructor that matches in your Client class. Java在尝试为名称分配n之前先调用super(),但是Client类中没有匹配的构造函数。

If you add a no-args constructor to the Client class everything should work: 如果向Client类添加一个无参数的构造函数,那么一切都应该正常工作:

Client () {
    //no-args
}

Alternatively, you can call the proper super-class constructor inside of the LawClient constructor as so: 或者,您可以这样在LawClient构造函数内部调用适当的超类构造函数:

LawClient (String n, boolean d)
{
    super(n); // this will call the first constructor of the Client class
    name = n;
    defendant = d;  
}

Just invoke the Client constructor on both LawClient constructors with the appropriate arguments super() 只需在两个LawClient构造函数上使用适当的参数super()调用Client构造函数

For example 例如

LawClient (String n, boolean d)
{  
    super(n);
    defendant = d;
}

LawClient (String n, boolean d, String c, String e)
{
    super(n, e);
    defendant = d;
    caseTopic = c;
}

When you define any constructor for a class (like you did for the Client class), the compiler does not automatically generate a default (no-argument) constructor for that class. 当您为类定义任何构造函数时(就像您为Client类所做的那样),编译器不会自动为该类生成默认(无参数)构造函数。 However, when you define a constructor in a subclass and do not explicitly invoke a superclass constructor, the compiler automatically inserts a call to the default constructor of the superclass. 但是,当您在子类中定义构造函数且未显式调用超类构造函数时,编译器会自动将对超类默认构造函数的调用插入。 Since Client does not have a default constructor, that's what's causing the error. 由于Client没有默认的构造函数,所以才是导致错误的原因。

The solution is to rewrite the constructors of your LawClient class to invoke the appropriate superclass constructors: 解决方案是重写LawClient类的构造函数以调用适当的超类构造函数:

LawClient (String n, boolean d)
{
    super(n);
    defendant = d;  
}

LawClient (String n, boolean d, String c, String e)
{
    super(n, e);
    defendant = d;
    caseTopic = c;
}

An alternative would be to explicitly define a default constructor for Client . 一种替代方法是显式定义Client的默认构造函数。 Your current code would work but this would violate both encapsulation (since you would be initializing Client fields from the subclass constructor) as well as the DRY principle . 您当前的代码可以工作,但是这会违反封装(因为您将从子类构造函数初始化Client字段)以及DRY原则

All classes require a constructor. 所有类都需要一个构造函数。 The first line of any constructor must be a call to a constructor of the parent class. 任何构造函数的第一行都必须是对父类的构造函数的调用。

To be helpful java creates a default empty constructor only if you have not specified any and it also creates a call to the the default parent class constructor if you do not specify a call so 为了帮助您,java仅在您未指定任何默认空构造函数的情况下,才创建一个默认的空构造函数;如果您未指定任何调用,它还将创建对默认父类构造函数的调用。

class Parent{
}

class Child extends Parent{
}

is exactly the same as 与...完全相同

class Parent{
    public Parent(){
        super();
    }
}

class Child extends Parent{
    public Child (){
        super();
    }
}

what your code is trying to do is 您的代码试图做的是

public class LawClient extends Client
{
    ....
    LawClient (String n, boolean d)
    {
        super();
        name = n;
        defendant = d;  
    }

    LawClient (String n, boolean d, String c, String e)
    {
        super();
        name = n;
        defendant = d;
        caseTopic = c;
        email = e;
    }

which is not working as 这不起作用

Client(){
}

is not present. 不存在。

so either you need to specify a no argument constructor or call a spefic constructor in each of the LawClient constructors. 因此,您需要指定一个无参数的构造函数,或者在每个LawClient构造函数中调用一个特殊的构造函数。

I see two things: 我看到两件事:

  1. When you define constructors with parameters in the parent class, they must be called explicitly by the child. 当您在父类中使用参数定义构造函数时,子类必须显式调用它们。

  2. Check the visibility of your classes and constructors. 检查您的类和构造函数的可见性。 They are not all public . 他们不是所有人都public When there is no modifier, the it is only visible from the same package. 如果没有修饰符,则只能从同一包中看到它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM