首页 > 解决方案 > Java vanClass给出找不到符号错误

问题描述

我的代码有几个类,不同的汽车和一些类。下面是我的代码,我不知道为什么 vanClass van;不起作用,因为它基本上是过去有效课程的复制粘贴。任何帮助表示赞赏。澄清一下,我只对 autopark 类的最后几行有问题,我将 vanClass 启动为 van 并从那里开始。

import java.util.*;

class sedan {
    String make;
    String model;
    String color;
    int year;
    double price;
    boolean fourWD;
    boolean isheavyDuty;
    String carries;

    public sedan(String initMake, String initModel, String initColor, int initYear, double initPrice) {
        make = initMake;
        model = initModel;
        color = initColor;
        year = initYear;
        price = initPrice;
    }

    @Override
    public String toString() {
        String name = "Sedan";
        String main = (color + " " + make + " " + model + " " + name + " (" + year + ") costs $" + price);
        return main;
    }
}

class SUV {

    String make;
    String model;
    String color;
    int year;
    double price;
    boolean fourWD;
    String carries;

    public SUV(String initMake, String initModel, String initColor, int initYear, double initPrice, boolean initFourWD){
        make = initMake;
        model = initModel;
        color = initColor;
        year = initYear;
        price = initPrice;
        fourWD = initFourWD;
    }

    public String toString() {
        String name = "SUV";
        String main = new String();
        if (fourWD) {
            main = ("4WD " + color + " " + make + " " + model + " " + name + " (" + year + ") costs $" + price);
        }
        else {
            main = (color + " " + make + " " + model + " " + name + " (" + year + ") costs $" + price);
        }

        return main;
    }
}

class truckClass {

    String make;
    String model;
    String color;
    int year;
    double price;
    boolean fourWD;
    boolean isheavyDuty;
    String carries;

    public truckClass(String initMake, String initModel, int initYear, double initPrice, boolean initisheavyDuty, String initCarries){
        make = initMake;
        model = initModel;
        year = initYear;
        price = initPrice;
        isheavyDuty = initisheavyDuty;
        carries = initCarries;

    }

    public String toString() {
        String name = "Truck";
        String main = (make + " " + model + " " + name + " (" + year + ") carries" + carries + " costs $" + price);
        return main;
    }

class vanClass {

    String make;
    String model;
    int year;
    double price;
    boolean isCovered;
    String carries;

    public vanClass(String initMake, String initModel, int initYear, double initPrice, boolean initisCovered, String initCarries){
        make = initMake;
        model = initModel;
        year = initYear;
        price = initPrice;
        isCovered = initisCovered;
        carries = initCarries;

    }

    public String toString() {
        String name;
        String main;
        if (isCovered()){
            name = "covered Van";
            String main = (make + " " + model + " " + name + " (" + year + ") carries" + carries + " costs $" + price);
        }
        else {
            name = "Van";
            String main = (make + " " + model + " " + name + " (" + year + ") carries" + carries + " costs $" + price);
        }
        return main;
    }
}






public class autoPark {
    public static void main(String args[]) {
        sedan sedan1; // declaring cars object by name sedan1
        sedan1 = new sedan("Ford" , "Model-1" , "white" , 2015, 20000); // initialising sedan1 using sedan constructor
        System.out.println(sedan1); // printing sedan1 for invoking toString() method

        SUV suv; // declaring cars object by name suv
        suv = new SUV("Ford" , "Model-1" , "white" , 2015, 20000, true); // initialising suv using SUV constructor
        System.out.println(suv); // printing suv for invoking toString() method

        truckClass truck; //declaring cars object by name truck
        truck = new truckClass("Ford" , "Model-1" , 2015, 20000, true, "2"); // initialising truck using truck constructor
        System.out.println(truck); // printing truck for invoking toString() method

        vanClass van;
        van = new vanClass("Ford" , "Model-1" , 2015, 20000, true, "2";
        System.out.println(van);


    }
}

标签: java

解决方案


我使用继承修复并重构了你的类:

abstract class Vehicle{
    protected String maker;
    protected String model;
    protected int year;
    protected double price;

    public Vehicle(String maker, String model, int year, double price) {
        this.maker=maker;
        this.model=model;
        this.year=year;
        this.price=price;
    }

    abstract String getType();

    @Override
    public String toString() {
        return maker + " " + model + " " + getType() + " (" + year + ") costs $" + price;
    }
}

abstract class HeavyVehicle extends Vehicle{
    protected String carries;

    public HeavyVehicle(String maker, String model, int year, double price, String carries) {
        super(maker, model, year, price);
        this.carries=carries;
    }
}

class SUV extends HeavyVehicle{

    String color;
    boolean fourWD;

    public SUV(String maker, String model, String initColor, int year, double price,
        boolean initFourWD) {
        super(maker,model,year,price,"1");
        color = initColor;
        fourWD = initFourWD;
    }

    public String toString() {
        StringBuilder sb = new StringBuilder();
        if (fourWD) {
            sb.append("4WD ");
        }
        sb.append(color + " " + super.toString());
        return sb.toString();
    }

    @Override
    String getType() {
        return "SUV";
    }
}

class Truck extends HeavyVehicle{

    public Truck(String maker, String model, int year, double price,String carries) {
        super(maker,model,year,price,carries);
    }

    public String toString() {
        return maker + " " + model + " " + getType() + " (" + year + ") carries" + carries + " costs $" + price;
    }

    @Override
    String getType() {
        return "Truck";
    }
}

class Van extends HeavyVehicle{

    boolean isCovered;

    public Van(String maker, String model, int year, double price, boolean isCovered, String carries){
        super(maker,model,year,price,carries);
        this.isCovered = isCovered;

    }

    public String toString() {
        String name = isCovered ? "covered Van" : getType();
        return maker + " " + model + " " + name + " (" + year + ") carries" + carries + " costs $" + price;
    }

    @Override
    String getType() {
        return "Van";
    }
}


class Sedan extends Vehicle{
    String color;

    public Sedan(String maker, String model, String color, int year, double price) {
        super(maker,model,year,price);
        this.color = color;
    }

    @Override
    public String toString() {
        return color + " " + super.toString();
    }

    @Override
    String getType() {
        return "Sedan";
    }
}

public class Main {
    public static void main(String args[]) {
        Sedan sedan1; // declaring cars object by name sedan1
        sedan1 = new Sedan("Ford", "Model-1", "white", 2015, 20000); // initialising sedan1 using sedan constructor
        System.out.println(sedan1); // printing sedan1 for invoking toString() method

        SUV suv; // declaring cars object by name suv
        suv = new SUV("Ford", "Model-1", "white", 2015, 20000, true); // initialising suv using SUV constructor
        System.out.println(suv); // printing suv for invoking toString() method

        Truck truck; // declaring cars object by name truck
        truck = new Truck("Ford", "Model-1", 2015, 20000, "2"); // initialising truck using truck constructor
        System.out.println(truck); // printing truck for invoking toString() method

        Van van;
        van = new Van("Ford", "Model-1", 2015, 20000, true, "2");
        System.out.println(van);

    }
}

推荐阅读