zoukankan      html  css  js  c++  java
  • Java继承_接口练习题

    1、创建一个名称为StaticDemo的类,并声明一个静态变量和一个普通变量。对变量分别赋予105的初始值。在main()方法中输出变量值。

    编写代码如下:

     1 package org.hanqi.practise;
     2 
     3 public class StaticDemo {
     4     
     5      private static int i = 10;
     6      private int j = 5;
     7      public String a()
     8      {
     9          return "i="+i+" j="+j;
    10      }   
    11     
    12     public static void main(String[] args) {
    13         
    14         StaticDemo b = new StaticDemo();
    15         System.out.println(b.a());
    16     }
    17 }

    运行结果为:

    2、建立一个汽车Auto类,包括轮胎个数,汽车颜色,车身重量、速度等成员变量。并通过不同的构造方法创建实例。至少要求:  汽车能够加速,减速,停车。 再定义一个小汽车类Car,继承Auto,并添加空调、CD等成员变量,覆盖加速,减速的方法

    编写代码如下:

    创建Auto类:

     1 package org.hanqi.practise;
     2 
     3 public class Auto {
     4     
     5     private int tyre;
     6     private String color;
     7     private double weight;
     8     private double speed;
     9     public Auto(int tyre, String color, double weight, double speed) {
    10         super();
    11         this.tyre = tyre;
    12         this.color = color;
    13         this.weight = weight;
    14         this.speed = speed;
    15     }
    16     public void accelerate()
    17     {
    18         System.out.println("Auto加速");
    19     }
    20     public void deceleration()
    21     {
    22         System.out.println("Auto减速");
    23     }
    24     public void stop()
    25     {
    26         System.out.println("Auto停车");
    27     }
    28 }

    创建Car类:

     1 package org.hanqi.practise;
     2 
     3 public class Car extends Auto {
     4     
     5     
     6     public Car(int tyre, String color, double weight, double speed) {
     7         super(tyre, color, weight, speed);
     8         
     9     }
    10     private String airconditioner;
    11     private String CD;
    12     public void accelerate()
    13     {
    14         System.out.println("Car加速");
    15     }
    16     public void deceleration()
    17     {
    18         System.out.println("Car加速");
    19     }
    20 }

    3、创建一个名称为Vehicle的接口,在接口中添加两个带有一个参数的方法start()stop()。在两个名称分别为BikeBus的类中实现Vehicle接口。创建一个名称为interfaceDemo的类,在interfaceDemomain()方法中创建BikeBus对象,并访问start()stop()方法。

    编写代码如下:

    创建Vehicle接口:

    1 package org.hanqi.practise;
    2 
    3 public interface Vehicle {
    4     
    5     public void start();
    6     public void stop();
    7 }

    创建Bike类:

     1 package org.hanqi.practise;
     2 
     3 public class Bike implements Vehicle {
     4 
     5     @Override
     6     public void start() {
     7         
     8 
     9     }
    10 
    11     @Override
    12     public void stop() {
    13         
    14 
    15     }
    16 }

    创建Bus类:

     1 package org.hanqi.practise;
     2 
     3 public class Bus implements Vehicle {
     4 
     5     @Override
     6     public void start() {        
     7 
     8     }
     9 
    10     @Override
    11     public void stop() {        
    12 
    13     }
    14 }

    创建InterfaceDemo类:

     1 package org.hanqi.practise;
     2 
     3 public class InterfaceDemo {
     4 
     5     public static void main(String[] args) {
     6         
     7         Bike bike = new Bike();
     8         bike.start();
     9         bike.stop();
    10         System.out.println();
    11         Bus bus = new Bus();        
    12         bus.start();
    13         bus.stop();
    14     }
    15 }
  • 相关阅读:
    java 多线程4: java线程的优先级
    Thread.currentThread().getName() ,对象实例.getName() 和 this.getName()区别
    go http
    go redis
    go tcp
    go 单元测试
    go 定时器
    go channel
    go goroutine
    go 错误处理
  • 原文地址:https://www.cnblogs.com/hanazawalove/p/5263200.html
Copyright © 2011-2022 走看看