我如何将所有变量串在一起以便在一句话中将它们清楚地表达出来?

时间:2019-07-19 05:50:20

标签: java string inheritance overriding superclass

当前代码单独打印语句,而我想将有关汽车的所有变量(颜色,汽缸数和座椅数量)串在一起,并将有关卡车的所有变量(颜色,汽缸数,和牵引能力)。可变席位在子类 example: <p>{{showContent}}</p> <button ng-click="shwMoreOrLess()">{{buttonName}}</button> var a = "your string of 100 char"; $scope.showContent = a;//default $scope.buttonName = "Show Less";//default $scope.shwMoreOrLess = function(){ var length = ($scope.buttonName = "Show More" ) ? 100 : 50;//setting length of the string. $scope.showContent = a.slice(0, length); $scope.buttonName = ($scope.buttonName = "Show More" ) ? "Show Less" : "Show More"; //your code goes here... } 中,并打印其自己的独立语句;变量牵引能力在子类Car中,其也打印其自己的语句。我希望用一句话说明汽车的所有变量,而要用一句话说明卡车的所有变量。

Truck

1 个答案:

答案 0 :(得分:0)

您不需要一些代码,因为我在下面做了同样的评论。 使用下面给出的toString方法。

public class CreateVehicle {
    public static void main(String[] args) {

        Car CarObject = new Car();
        Truck TruckObject = new Truck();

        //CarObject.getColor();
        CarObject.setColor();
        //  CarObject.getNoOfCylinders();
        CarObject.setNoOfCylinders();
        //CarObject.toString();
        CarObject.setNoOfSeats();

        //TruckObject.getColor();
        TruckObject.setColor();
        //TruckObject.getNoOfCylinders();
        TruckObject.setNoOfCylinders();
        //TruckObject.toString();
        TruckObject.setTowingCapacity();

        System.out.print(("\nThe car ") + CarObject.toString());
        System.out.print(("\nThe truck ") + TruckObject.toString());
    }
}

    class Car extends Vehicle {
        public String toString() {
            String information;
            information = super.toString() + " and " + noOfSeats + " seats";
            return information;
        }
    }

    class Truck extends Vehicle {
        public String toString() {
            String information;
            information = super.toString() + " and has a towing capacity of " + 
            towingCapacity;
            return information;
        }
    }