关于java:static代码块多态异常

1.static的用法

在一个class类中能够存在被static润饰的成员变量和成员办法,被static润饰的成员属性和成员办法能够通过两种办法实现执行。

  1. 第一种是通过类名.动态属性/类名.静态方法执行;
  2. 第二种是通过实例化对象的形式拜访,对象名.动态属性和对象名.静态方法。

在静态方法中只能拜访动态资源,不能拜访非动态资源
在一般办法中能够拜访静态方法,也能够拜访一般办法

被static润饰的成员变量仅此一份,所有对象所共享。(全局惟一,全局共享)

动态资源随着类的加载而加载,优先于对象加载

只加载一次,就会始终存在,不再开拓新空间

static不能和this或者super共用,因为有static时可能还没有对象

public class Test1_Static {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Person.test();//静态方法能够通过类名.办法名间接调用
        System.out.println(Person.age);//动态属性能够通过类名.属性名间接调用
        Person person=new Person();
        System.out.println(person.name);
        person.show();
        System.out.println(person.age);//动态属性能够通过对象名.动态属性调用拜访
        person.test();//静态方法也能够通过对象名.静态方法调用
        Person person2=new Person();
        Person person3=new Person();
        person2.age=10;
        System.out.println(person3.age);//被static润饰的成员属性供所有对象应用

    }

}
class Person{
    static int age;
    public static void test() {
        System.out.println("test()....");
//        show();//在jdk8中,静态方法不能够拜访一般办法
    }
    String name;
    public void show() {
        System.out.println("show()....");
        test();//在一般办法中能够拜访静态方法
    }
}

2.动态代码块,结构代码块,部分代码块

2.1动态代码块

动态代码块的作用是:实现我的项目的初始化
动态代码块随着类的加载而加载,只会加载一次,储存在内存中,随着类的隐没而隐没。
动态代码块存在的地位是类里,成员地位。
触发节点 : 类加载的时候

2.2结构代码块

结构代码块的作用是:提取构造方法中的个性 触发节点:创建对象时

2.3部分代码块

部分代码块存在的地位是,成员办法里
作用是:管制局部变量的作用范畴
触发节点:执行指标办法时

2.4执行程序

动态代码块>结构代码块>构造方法>部分代码块

public class Test2_Block {

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

}
class BlockDemo{
    //动态代码块随着类的加载而加载(只会加载一次),加载在内存,所有对象所共享,随着类的隐没而隐没。触发节点 :加载类的时候
    static {
        System.out.println("我是动态代码块");
    }
    //结构代码块 提取构造方法中的共性,优先于构造方法执行 触发工夫点 创建对象时
    {
        System.out.println("我是结构代码块");
    }
    public BlockDemo() {
        
        System.out.println("我是构造方法");
    }
    public void test() {
        //部分代码块 控制变量的作用范畴 触发节点 执行办法时
        {
            System.out.println("我是部分代码块");
        }
    }
}

3.final的用法

  1. 被final润饰的类不容许被继承
  2. 被final润饰的办法,子类能够拜访但不能重写
  3. 被final润饰的变量称之为常量,子类能够拜访
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);
    }
    

}
//被final润饰的类不容许被继承
class Animal{
    //被final润饰的办法能够被继承,子类能够拜访,然而不能被子类重写,
    final public void run() {
        System.out.println("run().....");
    }
    //被final润饰的变量,,子类能够拜访,然而值不能被批改
    final int age=1000;
}
class Cat extends Animal{
    
}

4.多态

多态:同一个对象的多种状态
多态的益处

--1,进步了程序的灵活性和扩展性
--2,多态中,基本不关怀具体的子类的类型,能够屏蔽子类间的不 同,把子类都当做父类看待
--3,代码更加通用,做出对立的编程

多态的条件:

--1产生了继承关系,子类重写了父类的办法
--2父类援用指向子类的实例,编译看右边,运行看左边
public class Test4_Multi {
    public static void main(String[] args) {
        Person people=new Chinese();
        people.speak();
    }
}
class Person{
    public void speak() {
        System.out.println("说什么");
    }
}
class Chinese extends Person{
    @Override
    public void speak() {
        System.out.println("说汉语");
    }
}

5.多态的应用

多态的实现条件是

产生了继承关系+子类重写了父类的办法

父类援用指向子类对象,编译看右边运行看左边

父类中的静态方法,子类能够拜访,然而不能够重写

多态中应用的成员变量指的是父类的成员变量,

多态指的是父类援用子类对象,子类展示重写父类的办法

public class Test5_UseMulti {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Father f=new Son();//父类援用指向子类对象
        f.study();//编译看右边,运行看左边指的是子类重写父类的办法
        f.show();//静态方法不能重写
        Father.show();
        System.out.println(f.sum);//多态中应用的成员变量是父类的成员变量
    }

}
class Father{
    int sum=10;
    public void study() {
        System.out.println("学习养生");
    }
    public static void show() {
        System.out.println("Father...show()");
    }
}
class Son extends Father{
    int sum=20;
    @Override
    public void study() {
        System.out.println("学习Java");
    }
    public static void show() {
        System.out.println("Son...show()");
    }
}

6.异样

异样是指程序中呈现的Bug.
try–catch 捕捉可能呈现的异样,catch对呈现的异样进行解决

`   try{
        代码
    }catch(异样类型1 异样名){
        给出解决方案1
    }catch(异样类型2 异样名){
        给出解决方案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("除数不能为0");
        }
    }
    public static void method1() {
        int a=5;
        int b=0;
        System.out.println(a/b);
    }
}

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理