CGBTN2110-DAY09 summary review

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
 }
  1. Try catch structures can be nested if there are multiple exception types that need special handling
  2. 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{ }
  1. 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
  2. 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

  1. The method modified by abstract is an abstract method, which has no method body
  2. Once a class has an abstract method, the class must be declared as an abstract class
  3. 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
  4. Abstract classes cannot be instantiated
  5. Abstract classes have constructors, but not for their own use. They are called for subclass super()
  6. Abstract classes can define member variables / member constants
  7. Abstract classes can define all general / all pumping / half general and half pumping
  8. If a class does not want to be instantiated by the outside world, it can be declared as an abstract class

2 abstract method

  1. The method modified by the keyword abstract is an abstract method
  2. 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:

  1. 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 ()

  1. Define the child Pigeon class Pigeon:

It can fly and lay eggs

  1. 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

  1. 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

  1. Define the child Ant class:

It can fly and lay eggs

  1. Define the child Bee class:

It can not only fly, but also lay eggs. It also has its own unique function to make honey

Keywords: Java

Added by matt2012 on Wed, 10 Nov 2021 14:44:43 +0200