this part
Very important , The understanding of class and class relation , The call of the relationship between classes , This thing doesn't write thousands of lines of code. You can't play around, but it's a very happy process , What could be happier than creating , I hope you can define yourself like this , You're not Minoan , You are the creators , And programming is just one of the tools you create . For one of them , Programming shouldn't be all you have , So programming like this loses its original fun , Learning with the attitude of falling in love with it , believe me , ceaseless coding Can make you fall in love with it

Let's see the business first :

business 1: Simulate the continuous passing of vehicles on the road , There's a speedometer monitoring these cars all the time , If there's a car speeding , Just call the police and tell the police car to chase him
There are three classes of analysis : vehicle , Tachometer , police car

There must be an array of cars in memory, because it's going on and on , Here I use a class called garage management to manage the storage and departure of the car through the speedometer , The speedometer has its own speed standard. If it's speeding, call the police car , At this time, the police car should have a garage
The knowledge points used in this business are comprehensive : inherit , Interface , Default Adapter ,static Method block , polymorphic , Encapsulation idea .
package Catchcar; public interface Car { // Define a car Interface , After all, whether it's a road car or a police car, it can run , You can't fly
public abstract void running(int speed); public abstract void running(String
carNum,String carBrand,int speed);// Method overload public abstract int getSpeed(); }
package Catchcar; public class RunningCar implements Car { private String
carBrand;// Automobile brand private String carNum;// private int speed; public RunningCar(
String carBrand,String carNum,int speed){ this.carBrand=carBrand; this.carNum=
carNum; this.speed=speed; } @Override public void running(int speed) {
// No problem ,,, The interface has such a disadvantage , I just want to override one method of the interface , But the internal mechanism of the interface has to be rewritten by me , It's really inconvenient
// Solution : Create an abstract class , Implementation interface , Override classes that you don't want subclasses to override , Then the class just inherits the middle abstract class , //
Just inherit a few of the classes you want to override , This is called the default adapter mode , Basically any framework any java The encapsulated data structures all have this pattern } @Override public void
running(String carNum,String carBrand,int speed) {// rewrite Car Interface method System.out.println
(" The number is "+carNum+carBrand+" Brand car "+" with "+speed+" Speed driving "); } @Override public int getSpeed(
) { return this.speed; } } package Catchcar; public class PoliceCar implements
Car{ // Speed of police car --- attribute , And his construction method // The way to catch up ---- Dependency , Car is the object of dependence // Get car speed , Judge the result according to the speed private
int speed; public PoliceCar(){} public PoliceCar(int speed){ this.speed=speed; }
public void catchCar(RunningCar runningCar){ int carspeed=runningCar.getSpeed();
System.out.println(" Starting to catch up with the current police car speed is "+this.getSpeed()); if(this.speed>carspeed){ int
time=(this.speed-carspeed); System.out.println(time+" I got it "); }else{ System.out.
println(" I didn't catch it "); } } @Override public void running(int speed) { } @Override
public void running(String carNum, String carBrand, int speed) { } @Override
public int getSpeed() { return this.speed; } } package Catchcar; public class
CarBase { // Here I build a Car The array of type is right. This array can be used for both police cars and ordinary cars ( This is the charm of polymorphism ) // private static Car
runningCarbase[]=new Car[5]; private static Car policeCarbase[]=new Car[5];
static { // Garage initialization // A static block is the first one executed in a subclass , To understand the loading order of classes for(int i=0;i<runningCarbase.
length;i++){ // Do you understand why I use static array , Static elements cannot access non static elements // Why can't I visit you runningCarbase[i]=new
RunningCar("bentain",""+Math.random(),(int)(Math.random()*10)); //
Random a number and turn it into string // math.random The method is to 01 Find random decimal between , I put it *10 In force to become int You can go out at random 0-10 Between
// It's amazing , This is the charm of polymorphism } // Last for In the cycle i Variables go out of the loop and disappear , Later, I can define variables with the same name without conflict for(int i=0;i<
policeCarbase.length;i++){ policeCarbase[i]=new PoliceCar(8); } } public static
PoliceCargetPoliceCar(){ // Walk through the garage , If there's a car, go back and cut to change the position into null PoliceCar policeCar=null;
for(int i=0;i<policeCarbase.length;i++){ if(policeCarbase[i]!=null){ policeCar=(
PoliceCar) policeCarbase[i];// The car in the reporter's garage is still Car Of type , //
It's just pointing policeCar Subclass can call subclass properties and subclass override methods . // I didn't have to choose before. Now I want to be a good man , You need to force a subclass policeCarbase[i]=null;
break; } } return policeCar; } public static RunningCar getRunningCar(){
RunningCar runningCar=null; for(int i=0;i<runningCarbase.length;i++){ if(
runningCarbase[i]!=null){ runningCar =(RunningCar)runningCarbase[i];
runningCarbase[i]=null; // Stop cycling when you get the car break; } } return runningCar; } } package
Catchcar; public class TextMachine { // attribute Define a standard time // Method of measuring car
// speeding ? Call the construction method of police chasing car private int stander=5; public TextMachine(){} public void
textCar(RunningCar runningcar){ int carSpeed=runningcar.getSpeed(); if(carSpeed>
5){ System.out.println(" The speed of the car is "+carSpeed+" Send out a police car to catch up "); PoliceCar PC =CarBase.
getPoliceCar(); PC.catchCar(runningcar); }else { System.out.println(" Caution! "); } }
} package Catchcar; public class TextMaiN { public static void main (String[]
args){ TextMachine textMachine=new TextMachine(); for(int i=0;i<5;i++){
textMachine.textCar(CarBase.getRunningCar()); } } }

©2019-2020 Toolsou All rights reserved,
C Language programming to find a student's grade java Realize the function of grabbing red packets Random forest R Language implementation QCustomPlot series (5)- Real time dynamic curve Software testing BUG describe use C++ I want to talk to you “ Prototype mode ” ( copy / copy constructor )JS How to operate China's longest high speed rail officially opened ! The fastest way to finish the race 30.5 hour Zimi apologizes :33W Gan charger delayed due to force majeure 2021 year 2 Chinese programming language ranking