当前位置:网站首页>Abnormal + Abstract

Abnormal + Abstract

2020-11-07 23:07:51 Roy01

1.throws

Throw an exception , Who calls who deals with
Declare an exception after a method Method name () throws Exception types

public class Test1_Exception {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
//        method();// Abnormal exposure 
        try {
            method2();
        } catch (ArithmeticException e) {
            // TODO Auto-generated catch block
            System.out.println(" The divisor is not 0");
        }
        
    }
    public static void method2 () throws ArithmeticException{
        int a=5;
        int b=0;
        System.out.println(a/b);
    }
    public static void method() {
        // TODO Auto-generated method stub
        int a=5;
        int b=0;
        System.out.println(a/b);
    }

}

2.abstract

By abstract The decorated method is called abstract method
Abstract methods can have no method bodies ;
There are abstract methods in a class , It's an abstract class
Abstract classes do not allow instantiation
Subclasses inherit Abstract parents , Or implement all the abstract methods of the parent class , Either it is defined as an abstract class

public class test2_Abstract {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Animal cat=new Cat();
        cat.eat();
        cat.sleep();
//        Animal b=new Dog();// Abstract class cannot be instantiated 
    }

}
// When there are abstract methods in a class , So this class is an abstract class 
abstract class Animal{
    // Abstract methods can have no method bodies 
    public abstract void eat();
    public void sleep() {
        System.out.println(" Go to sleep ");
    }
}
// When a class inherits an abstract class , Or rewrite the abstract method of the abstract class , Or it's also an abstract class 
abstract class dog extends Animal{
    
}
class Cat extends Animal{

    @Override
    public void eat() {
        // TODO Auto-generated method stub
        System.out.println(" Cats eat fish ");
    }
    public void sleep() {
        System.out.println(" Go to sleep +++++");
    }
    
}

3.abstract Usage of

There can be construction methods in abstract classes , Abstract classes cannot be instantiated themselves , But its subclass instantiation , A parameterless construct of the parent class is required

ublic class Test3_UseAbstract {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Aniaml2 a=new dog2();
        
    }

}
abstract class Aniaml2{
    public Aniaml2() {
        System.out.println(" The construction method of the parent class ");
    }
}
class dog2 extends Aniaml2{

    public dog2() {
        super();
        System.out.println(" Subclass construction method ");
        // TODO Auto-generated constructor stub
    }
    
}

Variables and constants can exist in abstract classes , Subclass inherits abstract class , You can modify variables , You can't modify constants

public class Test4_UseAbstract2 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Animal3 dog3=new Dog3();
        dog3.name=" Poodle ";
        System.out.println(dog3.name);
//        dog3.MAX_VLAUE=18;
        System.out.println(dog3.MAX_VLAUE);
    }

}
abstract class Animal3{
    String name=" rhubarb ";
    public static final byte MAX_VLAUE=127;
    
}
class Dog3 extends Animal3{
    
}

Abstract classes can all be ordinary methods , Abstract classes cannot be instantiated
Abstract classes can have ordinary methods and abstract methods
Subclass inherits abstract class , Or rewrite all the abstract methods of the abstract class , Or defined as an abstract class . When a child class does not override the normal method of the parent class, it accesses the normal method of the parent class , After being overridden, the subclass's rewriting method is accessed .

public class Test5_UseAbstract3 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Animal4 a=new Dog4();
        a.sleep();// Common method -- Using the parent class before rewriting , After rewriting, subclasses are used 
        a.eat();// Abstract method -- Using the method of the parent class to declare , Method body of subclass 
        a.game();// Abstract method -- Using the method of the parent class to declare , Method body of subclass 
    }

}
abstract class Animal4{
    public void sleep() {
        System.out.println("sleep()...");
    }
    public abstract void eat();
    public abstract void game();
}
class Dog4 extends Animal4{

    @Override
    public void eat() {
        // TODO Auto-generated method stub
        System.out.println("eat()...");
    }

    @Override
    public void game() {
        // TODO Auto-generated method stub
        System.out.println("game()...");
    }
    
}
abstract class Cat4 extends Animal4{
    @Override
    public void eat() {
        // TODO Auto-generated method stub
        System.out.println("eat222()...");
    }


}

abstact example

-- demand :

     Concrete things : Teacher of Peiyou class , Master class teacher 
     Generality : Prepare lessons  /  lecture 
     test : Creating polymorphic object tests 
public class Test6_Design {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Teacher cGBTeacher=new CGBTeacher();
        Teacher actTeacher=new ActTeacher();
        cGBTeacher.ready();
        cGBTeacher.teach();
        actTeacher.ready();
        actTeacher.teach();
    }

}
abstract class Teacher{
    public abstract void ready();
    public abstract void teach();
}
class CGBTeacher extends Teacher{

    @Override
    public void ready() {
        // TODO Auto-generated method stub
        System.out.println(" Teacher of Peiyou class   Get ready   E-commerce projects ");
    }

    @Override
    public void teach() {
        // TODO Auto-generated method stub
        System.out.println(" Teacher of Peiyou class   speak   E-commerce projects ");
    }
    
}
class ActTeacher extends Teacher{

    @Override
    public void ready() {
        // TODO Auto-generated method stub
        System.out.println(" Master class teacher   Prepare the handwriting frame ");
    }

    @Override
    public void teach() {
        // TODO Auto-generated method stub
        System.out.println(" Master class teacher   Talk about the strengthening part "); 
    }
    
}

版权声明
本文为[Roy01]所创,转载请带上原文链接,感谢