DAY09 review
1 abnormal
1. Abnormal inheritance structure
The root in the exception hierarchy is Throwable
Error: at present, we can't solve the problem of coding
Exception: exception
Compilation exception: an error is reported without running the code. It is mandatory to handle it
Runtime exception: an error is reported only when the code is running. It can be compiled without mandatory processing
Throwable:Top level parent of exception --Error:error,The program cannot process --Exception:abnormal,We can fix it by coding -- Compilation exception:It hasn't run yet. It has reported an error -- Runtime exception:Can be compiled,An error is reported as soon as it runs
2. Abnormal solutions
1. Try catch – solve it yourself
Format of exception capture processing:
try{ Code that may throw exceptions }catch(Type of exception name of exception){ In case an exception is caught, the solution is to handle it }
- Try catch structures can be nested if there are multiple exception types that need special handling
- Using the idea of polymorphism, no matter what the child Exception is, it is uniformly regarded as the parent type Exception
Make a more general solution, or even just write this one
2. Throw throws up – leave it to others to solve
Format of exception thrown:
Between the braces and braces of the method, write the: throws exception type
If there are multiple exceptions, use commas to separate them
private static void method3() throws ArithmeticException,InputMismatchException,Exception{ } private static void method3() throws Exception{ }
- If a method throws an exception, whoever calls the method needs to handle the exception. There are also two solutions: capture and solve or continue to throw upward
- We can't directly throw the exception to main(), because calling main() is a JVM and no one solves it. The error is also reported, so we usually solve the exception before calling main()
3. Abstraction
1. Abstract class
- The method modified by abstract is an abstract method, which has no method body
- Once a class has an abstract method, the class must be declared as an abstract class
- If a subclass inherits an abstract parent class, there are two solutions:
1) Abstract subclass: do not implement / implement some abstract methods in the abstract parent class
2) Normal subclass: implement all abstract methods in the abstract parent class - Abstract classes cannot be instantiated
- Abstract classes have constructors, but not for their own use. They are called for subclass super()
- Abstract classes can define member variables / member constants
- Abstract classes can define all general / all pumping / half general and half pumping
- If a class does not want to be instantiated by the outside world, it can be declared as an abstract class
2 abstract method
- The method modified by the keyword abstract is an abstract method
- The abstract method has no method body {}, and ends directly with a semicolon
4. Exercise case 1:
package cn.tedu.exec; /*This class is used for object-oriented comprehensive design cases*/ public class DesignTeacher { } //Extract the common functions of all classes and form the parent class Teacher upward abstract class Teacher{ //Since the method in the parent class must be overridden by the child class, you can declare the method in the parent class as an abstract method //Abstract the parent class to make rules, and hand over the actual work to the child class public abstract void ready(); public abstract void teach(); } //1. Create excellent CGB teacher class //Business: focusing on Internet architecture and micro services class CGBTeacher extends Teacher{ @Override public void ready() { System.out.println("Preparing lessons on Internet technology"); } @Override public void teach() { System.out.println("Teaching internet technology"); } } //2. Create an expert class teacher class //Focus: strengthening the basic framework and strengthening high and new technology class ACTTeacher extends Teacher{ @Override public void ready() { System.out.println("The foundation of lesson preparation is being strengthened"); } @Override public void teach() { System.out.println("The teaching foundation is being strengthened"); } } //3. Create an abstract subclass to implement only one abstract method in the abstract parent class abstract class SCDTeacher extends Teacher{ @Override public void ready() { System.out.println("Under development..."); } }
5 exercise case 2:
- Define the parent Bird class:
The number of leg legNumbers is 2
Egg numbers can be customized
It has the function of flying () and laying eggs ()
- Define the child Pigeon class Pigeon:
It can fly and lay eggs
- Define the sub Swallow class Swallow:
It can fly and lay eggs, and has its own unique function of nesting makeNest()
package cn.tedu.exec; /*This class is used as a comprehensive OOP case*/ public class TestAnimal { public static void main(String[] args) { //11. Create pigeon class object Pigeon p = new Pigeon(); System.out.println(p.LEG_NUMBERS);//2 System.out.println(p.eggNumbers);//0, or the default value of the parent class p.layEggs();//The number of eggs laid by pigeons: 2 p.fly();//Birds fly and fly~ //12. Create the object of swallow class Swallow s = new Swallow(); System.out.println(s.LEG_NUMBERS);//2 System.out.println(s.eggNumbers);//6 s.fly();//Inherited method s.layEggs();//The abstract method is implemented s.makeNest();//Own unique method } } //1. Define parent class abstract class Bird{ //2. Define the member constant in the bird class. The number of legs is 2 final int LEG_NUMBERS = 2; //3. Define the member variables in the bird class and the number of eggs laid int eggNumbers; //4. Define common methods in bird classes public void fly(){ System.out.println("Fly birdie fly ~"); } //5. Define the abstract methods in the bird class public abstract void layEggs(); } //6. Create subclasses class Pigeon extends Bird{ //7. Implement methods not implemented in the abstract parent class @Override public void layEggs() { System.out.println("The number of eggs laid by pigeons is:2 individual"); } } //8. Create the second subclass swallows class Swallow extends Bird{ //7.1 define subclass's own member variables int eggNumbers = 6; //7.2 implement methods not implemented in the abstract parent class @Override public void layEggs() { System.out.println("Number of eggs laid by swallows:"+eggNumbers); } //8. Define subclass specific methods public void makeNest(){ System.out.println("Swallows build nests~"); } }
5 exercise case 2: animal design code 2
- Define the parent Insect class Insect:
The number of legs is 6. The number of eggs can be customized. It has the function of flying and spawning spawn
- Define the child Ant class:
It can fly and lay eggs
- Define the child Bee class:
It can not only fly, but also lay eggs. It also has its own unique function to make honey