我创建了3个抽象类:
班级文章:母亲班级
public abstract class Article{
//myPrivate Var Declarations
public Article(long reference, String title, float price, int quantity){
this.reference = reference;
this.title = title;
this.price = price;
this.quantity = quantity;
}
}
职业电工:文章的孩子
public abstract class Electromenager extends Article{
//myVar declarations
public Electromenager(long reference, String title, float price, int quantity, int power, String model) {
super(reference, title, price, quantity);
this.power = power;
this.model = model;
}
}
类Alimentaire:文章的另一个孩子
public abstract class Alimentaire extends Article{
private int expire;
public Alimentaire(long reference, String title, float price, int quantity,int expire){
super(reference, title, price, quantity);
this.expire = expire;
}
}
让我们假设这些类必须是抽象的,所以基本上在主类中,我不能直接实例化它们的对象,所以我们需要做一些基本的扩展。:
class TV extends Electromenager {
public TV(long reference, String title, float price, int quantity, int power, String model){
super(reference,title,price,quantity,power,model);
}
}
class EnergyDrink extends alimentaire {
public EnergyDrink(long reference, String title, float price, int quantity,int expire){
super(reference,title,price,quantity,expire);
}
}
所以在这里,我的困惑开始出现了!在main()中写入时:
Article art = new TV (145278, "OLED TV", 1000 , 1 ,220, "LG");
EnergyDrink art2 = new EnergyDrink (155278 , "Eau Miniral" , 6 , 10, 2020);
令人惊讶的是,我得到了零错误!!!!我不应该键入:
TV art = new TV (145278, "OLED TV", 1000 , 1 ,220, "LG");
//instead of
Article art = new TV (145278, "OLED TV", 1000 , 1 ,220, "LG");
为什么两种书写都是正确的?Java编译器如何理解这一点?