超类用 Java 中构造函数的默认值覆盖子类

Superclass overriding Subclass with default values from constructor in Java

我有一个赋值,我的超类默认值总是覆盖我在 Test main 方法中传递的值。在调试器中,我看到了 productNumber(1234) 和 productTitle("Daughter") 的传递,但随后它被默认值覆盖了。有什么想法,我一直在做小改动,检查变化,结果还是一样。

产品超类

public abstract class Product {
    private int productNumber;
    private String productTitle;
   
//Two constructors required
    public Product(){
        productNumber = 0;
        productTitle = "";
    }
    
    public Product(int productNumber, String productTitle) {
        this.productNumber = productNumber;
        this.productTitle = productTitle;
    }
  

    public void setProductNumber(int productNumber) {
        this.productNumber = productNumber;
    }

    public int getProductNumber() {
        return productNumber;
    }

    public void setProductTitle(String productTitle) {
        this.productTitle = productTitle;
    }
        
    public String getProductTitle() {
        return productTitle;
    }

    //Override toString() required
    @Override
    public String toString() {
        return productNumber + " " + productTitle;
    }
    
    // Required Product class declares abstract method with this signature: public String getDisplayText()
    public abstract String getDisplayText();
    
    //Override equals() required
    @Override
    public boolean equals(Object object) {
        if (object instanceof Product)  {
            Product product2 = (Product) object;
            if (productNumber == (product2.getProductNumber()) &&
                productTitle.equals(product2.getProductTitle())){
                    return true;
            }
        }
        return false;
    }  
}

音乐子类扩展了产品超类

public class Music extends Product {
    private String artist;
    private String style;
    private String medium;

    public Music() {
        super();
        artist = "";
        style = "";
        medium = "";
    }

    public Music(int productNumber, String productTitle, String artist, String style, String medium) {
        super();
        this.artist = artist;
        this.style = style;
        this.medium = medium;
    }


    public String getArtist() {
        return artist;
    }

    public void setArtist(String artist) {
        this.artist = artist;
    }
    public String getStyle() {
        return style;
    }

    public void setStyle(String style) {
        this.style = style;
    }

    public String getMedium() {
        return medium;
    }

    public void setMedium(String medium) {
        this.medium = medium;
    }

    @Override
    public String getDisplayText() {
        return super.toString() + " by " + artist + " " + style + " " + medium;
    }
 
    @Override
    public boolean equals(Object object){
        if (object instanceof Music){
            Music m = (Music) object;
            if (artist.equals(m.getArtist()) &&
                style.equals(m.getStyle()) &&
                medium.equals(m.getMedium())){
                    return true;
            }
        }
        return false;
    }
}

打印字符串

public class Test {

    public static void main(String[] args) {
        // Expected result: 1234 Daughter by Pearljam Alternative online
        Music music1 = new Music(1234,"Daughter", "Pearljam","Alternative","online");
        System.out.println(music1.getDisplayText());
    }
    
}

您需要在 super(..., ...) 构造函数内部的 super(..., ...) 调用中将 productNumberproductTitle 传递给父 class。

您没有将子类的值传递给父类

而不是 super() 你需要在下面做 -

super(productNumber,productTitle);

您的代码需要更新:-

public Music(int productNumber, String productTitle, String artist, String style, String medium) {
        super(productNumber,productTitle);
        this.artist = artist;
        this.style = style;
        this.medium = medium;
    }

您需要调用

super(productNumber, productTitle)

在 Music 构造函数中将参数传递给它的父级。