Java 如何实现抽象类的方法?(爪哇)
我试图使用一个抽象类中的方法来实现一个接口。当我调用一个方法时,我总是得到一个空指针异常,我不知道为什么。有什么想法吗?谢谢Java 如何实现抽象类的方法?(爪哇),java,inheritance,polymorphism,Java,Inheritance,Polymorphism,我试图使用一个抽象类中的方法来实现一个接口。当我调用一个方法时,我总是得到一个空指针异常,我不知道为什么。有什么想法吗?谢谢 package start; public class Automobile extends Vehicle { // code with main method public static void main(String[] args) { Vehicle[] automobiles = new Vehicle[3]; au
package start;
public class Automobile extends Vehicle { // code with main method
public static void main(String[] args) {
Vehicle[] automobiles = new Vehicle[3];
automobiles[0].setVehicleName("Corvette");
}
}
//////////////////////////////////////////////////////////////////////////
package start;
public abstract class Vehicle implements Movable {
String name = "Unidentified"; // variables for vehicles
String manufacturer = "Factory";
String car = "Unknown";
int yearOfManufacture = 0000;
int horsepower = 0;
static int instances = 0;
int passengers = 0; // variables for methods below
int speed = 0;
public int getNoPassengers() { // returns how many passengers there are
instances = instances + 1;
return passengers;
}
public void setNoPassengers(int noPassengers) { // sets the number of passengers
instances = instances + 1;
passengers = noPassengers;
}
public int getTopSpeed() { // returns how fast a movable vehicle is
instances = instances + 1;
return speed;
}
public void setTopSpeed(int topSpeed) { // changes the speed of a movable vehicle
instances = instances + 1;
speed = topSpeed;
}
public void setVehicleName(String title) { // changes the name of a vehicle
instances = instances + 1;
name = title;
}
public String getVehicleName(String car){
return car;
}
public void setManufacturer(String creator) { // changes the manufacturer
instances = instances + 1;
manufacturer = creator;
}
public String getManufacturer(String type){
return type;
}
}
package start;
interface Movable { // interface
int getNoPassengers(); // returns how many passengers there are
void setNoPassengers(int noPassangers); // sets the number of passengers
int getTopSpeed(); // returns how fast a movable vehicle is
void setTopSpeed(int topSpeed); // changes the speed of a movable vehicle
}
//////////////////////////////////////////////
package start;
public abstract class Vehicle implements Movable {
String name = "Unidentified"; // variables for vehicles
String manufacturer = "Factory";
String car = "Unknown";
int yearOfManufacture = 0000;
int horsepower = 0;
static int instances = 0;
int passengers = 0; // variables for methods below
int speed = 0;
public int getNoPassengers() { // returns how many passengers there are
instances = instances + 1;
return passengers;
}
public void setNoPassengers(int noPassengers) { // sets the number of passengers
instances = instances + 1;
passengers = noPassengers;
}
public int getTopSpeed() { // returns how fast a movable vehicle is
instances = instances + 1;
return speed;
}
public void setTopSpeed(int topSpeed) { // changes the speed of a movable vehicle
instances = instances + 1;
speed = topSpeed;
}
public void setVehicleName(String title) { // changes the name of a vehicle
instances = instances + 1;
name = title;
}
public String getVehicleName(String car){
return car;
}
public void setManufacturer(String creator) { // changes the manufacturer
instances = instances + 1;
manufacturer = creator;
}
public String getManufacturer(String type){
return type;
}
}
package start;
interface Movable { // interface
int getNoPassengers(); // returns how many passengers there are
void setNoPassengers(int noPassangers); // sets the number of passengers
int getTopSpeed(); // returns how fast a movable vehicle is
void setTopSpeed(int topSpeed); // changes the speed of a movable vehicle
}
您在邮件中的代码如下:
Vehicle[] automobiles = new Vehicle[3];
automobiles[0].setVehicleName("Corvette");
在这里,您只分配了数组,但数组中的元素仍然为null(因此在对null对象调用setter时出现null指针异常),并且需要初始化,如下所示:
automobiles[0] = new ....;
//then access method from within automobiles[0]
问题是您仅在以下行中创建了车辆阵列-
Vehicle[] automobiles = new Vehicle[3];
您仍然需要使用new Vehicle(或者new Automobile,因为Vehicle是一个抽象类,无法实例化)将变量初始化为对象,然后才能访问它们
范例-
Vehicle[] automobiles = new Vehicle[3];
automobiles[0] = new Automobile();
automobiles[0].setVehicleName("Corvette");
请在使用[0]之前将新对象分配给它。就像这样做——汽车;自动设置车辆名称(“a”);欢迎,没关系,每个人都会犯错,从中学习很重要。