第十一周上课练习

1、(1)定义一个汽车类Vehicle,要求如下:(知识点:类的继承 方法的覆盖)
(a)属性包括:汽车品牌brand(String类型)、颜色color(String类型)和速度speed(double类型)。
(b)至少提供一个有参的构造方法(要求品牌和颜色可以初始化为任意值,但速度的初始值必须为0)。
(c)为属性提供访问器方法。注意:汽车品牌一旦初始化之后不能修改。
(d)定义一个一般方法run(),用打印语句描述汽车奔跑的功能
定义测试类VehicleTest,在其main方法中创建一个品牌为“benz”、颜色为“black”的汽车。
package demo.java;

public class Vehicle {
        String brand;
        String color;
        double speed;
        void setCar(String brand,String color,double speed){
            this.brand=brand;
            this.color=color;
            this.speed=speed;
        } 
        void run(){
            System.out.println("品牌为"+brand+"颜色为"+color+"速度为"+speed);
        }
        void setCar(String brand,String color){
            this.brand="null";
        }
}
package demo.java;

public class VehicleTest {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Vehicle p=new Vehicle();
        p.brand="benz";
        p.color="black";
        p.run();
    }

}

猜你喜欢

转载自www.cnblogs.com/108-com/p/12891876.html