当前位置:网站首页>Static + code block + polymorphism + exception

Static + code block + polymorphism + exception

2020-11-07 20:55:57 Roy01

1.static Usage of

In a class Class can exist by static Decorated member variables and member methods , By static Decorated member properties and member methods can be implemented in two ways .

  1. The first is through the class name . Static attribute / Class name . Static method execution ;
  2. The second is to access by instantiating objects , Object name . Static properties and object names . Static methods .

Only static resources can be accessed in static methods , Cannot access non static resources
Static methods can be accessed in normal methods , You can also access common methods

By static The modified member variable is only one part , Shared by all objects .( Globally unique , Global Shared )

Static resources are loaded with classes , Takes precedence over object loading

Load only once , There will always be , No more new space

static Unable to join this perhaps super share , Because there is static There may be no object yet

public class Test1_Static {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Person.test();// Static methods can be defined by the class name . Method name calls directly 
        System.out.println(Person.age);// Static properties can be defined by the class name . Property name calls directly 
        Person person=new Person();
        System.out.println(person.name);
        person.show();
        System.out.println(person.age);// Static properties can be defined by the object name . Static property call access 
        person.test();// Static methods can also be used by object names . Static method calls 
        Person person2=new Person();
        Person person3=new Person();
        person2.age=10;
        System.out.println(person3.age);// By static Decorated member properties are used by all objects 

    }

}
class Person{
    static int age;
    public static void test() {
        System.out.println("test()....");
//        show();// stay jdk8 in , Static methods cannot access normal methods 
    }
    String name;
    public void show() {
        System.out.println("show()....");
        test();// Static methods can be accessed in normal methods 
    }
}

2. Static code block , Building blocks of code , Local code block

2.1 Static code block

The function of static code block is : Complete the initialization of the project
Static code blocks load as classes load , Will only load once , Stored in memory , Disappear with the class .
Static code blocks exist in the class , Member position .
Trigger node : Class loading time

2.2 Building blocks of code

The function of building code blocks is : Extract features from the construction method Trigger node : When you create an object

2.3 Local code block

The location of the local code block is , In the member method
Role is : Control the scope of local variables
Trigger node : When executing the target method

2.4 Execution order

Static code block > Building blocks of code > Construction method > Local code block

public class Test2_Block {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new BlockDemo().test();
    }

}
class BlockDemo{
    // Static code blocks load as classes load ( Will only load once ), Load in memory , Shared by all objects , Disappear with the class . Trigger node  : When loading a class 
    static {
        System.out.println(" I'm a static block of code ");
    }
    // Building blocks of code   Extracting commonness in construction methods , Takes precedence over constructor execution   Trigger time point   When you create an object 
    {
        System.out.println(" I'm building blocks of code ");
    }
    public BlockDemo() {
        
        System.out.println(" I'm the construction method ");
    }
    public void test() {
        // Local code block   The control range of the variable   Trigger node   When executing methods 
        {
            System.out.println(" I'm a local code block ");
        }
    }
}

3.final Usage of

  1. By final Decorated classes are not allowed to be inherited
  2. By final The method of decoration , Subclasses can be accessed but not overridden
  3. By final Modified variables are called constants , Subclasses can access
public class Test3_Final {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Cat cat=new Cat();
        cat.run();
        System.out.println(cat.age);
    }
    

}
// By final Decorated classes are not allowed to be inherited 
class Animal{
    // By final The decorated method can be inherited , Subclasses can access , But it can't be overridden by subclasses ,
    final public void run() {
        System.out.println("run().....");
    }
    // By final Decorated variable ,, Subclasses can access , But the value cannot be changed 
    final int age=1000;
}
class Cat extends Animal{
    
}

4. polymorphic

polymorphic : Multiple forms of the same object
The benefits of polymorphism

--1, It improves the flexibility and expansibility of the program 
--2, In polymorphism , It doesn't care about the types of specific subclasses , You can block the non between subclasses   Same as , Treat all subclasses as parents 
--3, The code is more generic , Make unified programming 

The condition of polymorphism :

--1 There's an inheritance relationship , The subclass overrides the method of the parent class 
--2 The parent reference points to an instance of a subclass , Compile to the left , Run to the right 
public class Test4_Multi {
    public static void main(String[] args) {
        Person people=new Chinese();
        people.speak();
    }
}
class Person{
    public void speak() {
        System.out.println(" what did you say? ");
    }
}
class Chinese extends Person{
    @Override
    public void speak() {
        System.out.println(" Speak Chinese ");
    }
}

5. The use of polymorphism

The realization condition of polymorphism is

There's an inheritance relationship + The subclass overrides the method of the parent class

The parent class reference points to the subclass object , Compile and run on the left and look at the right

Static methods in the parent class , Subclasses can access , But it can't be rewritten

The member variable used in polymorphism refers to the member variable of the parent class ,

Polymorphism refers to that the parent class refers to the child class object , Subclasses show methods that override the parent class

public class Test5_UseMulti {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Father f=new Son();// The parent class reference points to the subclass object 
        f.study();// Compile to the left , Run to see the right side refers to the method of subclass overriding the parent class 
        f.show();// Static methods cannot override 
        Father.show();
        System.out.println(f.sum);// The member variables used in polymorphism are the member variables of the parent class 
    }

}
class Father{
    int sum=10;
    public void study() {
        System.out.println(" Learn to keep fit ");
    }
    public static void show() {
        System.out.println("Father...show()");
    }
}
class Son extends Father{
    int sum=20;
    @Override
    public void study() {
        System.out.println(" Study Java");
    }
    public static void show() {
        System.out.println("Son...show()");
    }
}

6. abnormal

An exception is something that appears in a program Bug.
try--catch Capture possible exceptions ,catch Handle the exception that appears

`   try{
         Code 
    }catch( Exception types 1  Exception names ){
         Give solutions 1
    }catch( Exception types 2  Exception names ){
         Give solutions 2
    }
      

`

public class Test6_Exception {

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

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