zoukankan      html  css  js  c++  java
  • 继承-car

    编写一个Car类,具有final类型的属性品牌,具有功能drive;

    定义其子类Aodi和Benchi,具有属性:价格、型号;具有功能:变速;

    定义主类E,在其main方法中分别创建Aodi和Benchi的对象并测试对象的特

    性。

    package homework;
    
    public class Car {
    
        private  final String pinpai="";
        
        
        public void drive()
        {
            
            System.out.println("正在行驶");
            
        }
    
    
        public String getPinpai() {
            return pinpai;
        }
        
        
        
        
        
    }
    package homework;
    
    public class Aodi extends Car{
    
        
        
        private double jiage;
        private String xinghao;
        public double getJiage() {
            return jiage;
        }
        public void setJiage(double jiage) {
            this.jiage = jiage;
        }
        public String getXinghao() {
            return xinghao;
        }
        public void setXinghao(String xinghao) {
            this.xinghao = xinghao;
        }
        
        
        
        
    }
    package homework;
    
    public class Benchi extends Car{
    
        private double speed;
        
        public double getSpeed() {
            return speed;
        }
    
    
        public void setSpeed(double speed) {
            this.speed = speed;
        }
        
        public void changeSpeed(double speed)
        {
            for(int i=1;i<=20;i++)
            {
                speed+=10;
                if(speed>=150)
                {
                    System.out.println("当前速度为:"+speed+"请减速慢行");
                }            
                
                
            }
        }
    
    
        
        
        
    }
    package homework;
    
    public class E {
    
        public static void main(String[] args) {
            // TODO 自动生成的方法存根
    
            Aodi ai=new Aodi();
            ai.setJiage(100000.0);
            ai.setXinghao("a6L");
            ai.drive();
            
            Benchi bc=new Benchi();
            bc.changeSpeed(20);
            
            
        }
    
    }

    运行结果:

     

  • 相关阅读:
    JDK内置工具使用
    awk 数组
    c++面试题
    C++内存分配
    awk 数字比较
    awk脚本 排序
    awk 读取 shell 变量的方法
    NVelocity系列:Getting Start With NVelocity
    Castle.ActiveRecord分页示例
    NVelocity系列:NVelocity配置详解
  • 原文地址:https://www.cnblogs.com/miss123/p/5520082.html
Copyright © 2011-2022 走看看