請(qǐng)大神求解obj1.way()為什么會(huì)報(bào)錯(cuò)
public class Vehicles {? ? ? ?//父類(lèi)
public static void main(String[] args) {
Vehicles obj1 = new Vehicles();
Vehicles obj2 = new Bus();
Vehicles obj3 = new boat();
Vehicles obj4 = new airplane();
obj1.way();? ?
}}
}
public class Bus extends Vehicles {? //子類(lèi)BUS
int number=50;
String name="bus";
String function="陸地";
? public? void way() {
System.out.println("載客"+number+"人的"+name+"是在"+function+"上運(yùn)行的");
}
}
public class boat extends Vehicles {? //子類(lèi)BUS
int number=500;
String name="boat";
String function="海洋";
? public? void way() {
System.out.println("載客"+number+"人的"+name+"是在"+function+"上運(yùn)行的");
}
}
public class airplane extends Vehicles {? ? //子類(lèi)airplane
int number=1000;
String name="airplant";
String function="天空";
void way() {
System.out.println("載客"+number+"人的"+name+"是在"+function+"上運(yùn)行的");
}
}
2018-09-19
obj1是父類(lèi)的對(duì)象,而父類(lèi)里并沒(méi)有定義"way()",所以報(bào)錯(cuò)
2018-09-20
是不是因?yàn)槎xobj1234時(shí),都是用父類(lèi)引用的,而way是子類(lèi)的方法,不能調(diào)用 視頻8分12秒有講