简体   繁体   English

抽象方法不抽象类

[英]Abstract method do not abstract class

Somehow, I am getting an JNI error for the code below when using an abstract.不知何故,我在使用摘要时收到以下代码的 JNI 错误。 This shouldn't happen as I think I have wrote it correct.这不应该发生,因为我认为我写得正确。 Can please someone have a look.可以请人看看。

package Abstraction;

public class Abstraction1_After {
    public static void main(String[] args) {
        Iphone obj = new Iphone();
        Samsung obj1 = new Samsung();
        showPrice(obj);
    }


    public static void showPrice(Phone obj) {
        obj.showPrice();
    }
}

abstract class Phone {
    public abstract void showPrice();
}


class Iphone extends Phone {
    public void IphonePrice() {
        System.out.println("Price of Iphone Xr is 500€");
    }
}


class Samsung extends Phone {
    public void Samsungs9Price() {
        System.out.println("Price of Samsung S9 is 600€");
    }
}

Your Iphone class is inheriting the abstract method showPrice() from the Phone class.您的Iphone类继承了Phone类的抽象方法 showPrice()。 You have to override it since a non-abstract class can't have an abstract method您必须覆盖它,因为非抽象类不能有抽象方法

You probably want something like this:你可能想要这样的东西:

class Iphone extends Phone {
    public void showPrice() {   
        System.out.println("Price of Iphone Xr is 500€");
    }
}

If you're trying to override a method you have to let the compiler know by giving the new method the same name.如果您试图覆盖一个方法,您必须通过为新方法提供相同的名称来让编译器知道。 So just change the method name to be the same as the parents method and you should be good to go.因此,只需将方法名称更改为与父方法相同,您就可以开始使用了。

The same goes for your Samsung class of course当然,您的Samsung班级也是如此

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

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