吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1560|回复: 3
收起左侧

[Java 转载] 一个汽车Auto类,然后继承重写,也算是一个记录自己学习的过程吧

[复制链接]
ny666999 发表于 2020-11-11 20:53
package com.Weekend.Dwh;

/**
*
* @AuThor Administrator
* @category 建立一个汽车Auto类,包括轮胎个数,汽车颜色,车身重量、速度等成员变量。
*           并通过不同的构造方法创建实例。至少要求:汽车能够加速,减速,停车。再定义一个小汽车类Car,继承Auto,
*           并添加空调、CD等成员变量,覆盖加速,减速的方法
*/
public class Work18 {
        public static void main(String[] args) {
                Car2 c = new Car2("小汽车", "蓝色", 2, 1.5, "空调", "CD");
                c.showInfo();
                c.speedUp();
                c.speedUp();
                c.speedUp();
                c.speedUp();
                c.move();
                c.speedDown();
                c.stop();
        }
}

class Auto {
        private String color;
        private int size;
        private double weight;
        double speed;

        public Auto(String color, int size, double weight) {
                super();
                this.color = color;
                this.size = size;
                this.weight = weight;
        }

        public void speedUp() {
                if (speed < 200) {
                        System.out.println("正在加速中......");
                        this.speed += 10;
                } else if (speed == 0) {
                        System.out.println("停车");
                } else {
                        this.speed = 200;
                }
        }

        public void speedDown() {
                if (speed > 10) {
                        System.out.println("正在减速中......");
                        this.speed -= 10;
                } else {
                        stop();
                }
        }

        public void stop() {
                this.speed = 0;
                System.out.println("停车");
        }

        public void move() {
                System.out.println(this.getSize() + "座的车,以" + this.getSpeed() + "公里/小时的速度正在行驶中");
        }

        public void showInfo() {
                System.out.println("一辆" + this.getColor() + "汽车," + this.getSize() + "个轮胎," + "车身重量" + this.weight + "吨");
        }

        public double getSpeed() {
                return speed;
        }

        public void setSpeed(double speed) {
                this.speed = speed;
        }

        public String getColor() {
                return color;
        }

        public void setColor(String color) {
                this.color = color;
        }

        public int getSize() {
                return size;
        }

        public void setSize(int size) {
                this.size = size;
        }

        public double getWeight() {
                return weight;
        }

        public void setWeight(double weight) {
                this.weight = weight;
        }

}

class Car2 extends Auto {
        String name;
        String airCondition;
        String CD;

        public Car2(String name, String color, int size, double weight, String airCondition, String CD) {
                super(color, size, weight);
                this.airCondition = airCondition;
                this.CD = CD;
        }

        //重写父类的方法
        @Override
        public void showInfo() {
                System.out.println("一辆" + this.getColor() + "汽车," + this.getSize() + "个轮胎," + "车身重量" + this.getWeight() + "吨"
                                + "\t有" + this.airCondition + "\t有" + this.CD);
        }

        public String getName() {
                return name;
        }

        public void setName(String name) {
                this.name = name;
        }

        public String getAirCondition() {
                return airCondition;
        }

        public void setAirCondition(String airCondition) {
                this.airCondition = airCondition;
        }

        public String getCD() {
                return CD;
        }

        public void setCD(String cD) {
                CD = cD;
        }
}

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

14788871771 发表于 2020-11-11 23:18
你不会是温大的吧,咋看着这么熟悉
18077123969 发表于 2020-11-11 23:30
 楼主| ny666999 发表于 2020-11-12 09:22
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-30 04:16

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表