Understanding of Abstract, static, final of

Abstract, static, final understanding

A. Abstract Abstract

1> Modified class

  • After a class is modified Abstract, you can not target a new, modified parent

  • Abstract modified description is not a specific type, a category, a parent should not be concrete
  • Description This class is not complete, is not specific enough, the abstract class can not exist independently
  • Subclass common output

作用:
    1> 可以被子类继承,提供共性属性和方法
    2> 可声明为引用,强制使用多态
    3> 抽象父类可以是子类的组成部分,依附于子类对象的存在
    4> 由父类共性+子类独有组成完整的子类对象
    5> 抽象类也是有构造方法的,可以由子类来调用,构建子类对象时调用父类构造方法
    6> 和普通类的区别,就是不能new,其他和普通的类都一样

2> modification methods

  • Abstract method: only a statement, did not materialize, incomplete

  • When necessary declarations of methods, but issued realize is redundant, since implementation of the parent class is unable to meet the needs of the subclass, the subclass will finally be rewritten
  • Abstract class containing the method is an abstract class must
  • There is not necessarily an abstract class abstract method
  • Subclass must override all abstract methods of the parent class

//如果父类是个抽象
//那么子类必须重写父类的所有的抽象方法
//抽象类中不一定有抽象方法
abstract class Animal{//含有不完整的方法,那么这个类也是不完整的
    String bread;
    int age;
    
    public Animal(){
        System.out.println("这是抽象类的无参构造函数..");    
    }
    
    //这是一个抽象方法
    //含有抽象方法的类必须是个抽象类
    public abstract void eat();//只有声明,没有实现,意为不完整
    
    //还可以拥有普通的方法
    public  void slep(){};
}


//一个抽象类的子类
class Dog extends Animal{
    
    //必须重写父类所有的抽象方法
    public void eat(){
        
    }
    
}

//子类不想实现父类的抽象方法
//那么这个类也必须是个抽象类
//同时继承父类所有的未实现的抽象方法
abstract class Cat extends Animal{
    
    
}

Two. Static static

When the class is loaded. In the process area, the information will be stored a class object MyClass, MyClass.class

  • Ordinary instance variables are created object in memory when you create an object

  • Static method is to create objects in memory when the class is loaded

  • Static method allows direct access to static members

  • Static methods can not directly access non-static member

  • Static method does not allow this and super keywords

  • Static methods can be inherited and can not rewrite, no polymorphism

    • To the dynamic run-time execution is clear is which, because it can not determine whether there is a subclass covering him rewrite
    • When static methods do not have to run, compile time will know. Immutable runtime

    Call java method is divided into two

    1> Static assignment: the static method, the parameter list of the same name allows different methods, static methods refers to reload between

    2> Dynamic assignment: In the case of inheritance relationship, instance method is invoked, up from a low (first check the sub-categories, find the parent class, has been up to find) find an available version of the method, refers to an instance method of covering

    jvm method call instructions:

    1> invokespacial call the private methods and constructors

    2> invokeinteface call interface method

    3> invokestatic call the static method

    4> invokevirtual virtual method calls (parent class definition, quilts cover future method)

    5> invokedynamic calling the dynamic link method

    public class TestStatic{
        public static void main(String[] args){
    
            A.staticMethod();
            B.staticMethod();//继承了父类的静态方法,但是不能重写,所以不存在多态
    
            A a = new B();
    
            //也是可以掉调用的,即使在子类中也有一个同样的静态方法
            //下面的方法调用的依然是父类中的静态方法
            //因为静态方法不能被覆盖 
            a.staticMethod();
    
    
    
        }
    }
    
    class A{
    
        static int find;
        public static void staticMethod(){
            System.out.println("----" + find);
        }
    }
    
    class B extends A{
    
        //假如B中也存在一个,也是不行的,不能覆盖
        public static void staticMethod(){
            System.out.println("----" + find);
        }
    }

1> static properties (class attributes)

  • Static class members are full members of all shared objects

  • Only one, not because create multiple objects to produce multiple copies of the whole class

  • Instead of creating objects, can be directly accessed by the class name

a. Common properties
public class TestStatic{
    public static void main(String[] args){
        MyClass mc1 = new MyClass();
        m1.a = 10;
        
        //每个对象都拥有自己单独的a
        //每个对象都在堆中用一个自己的a空间,指向栈中的mc2引用
        MyClass mc2 = new MyClass();
        m2.a = 20;
        
        System.out.println(m1.a + "--" + m2.a);//输出结果: 10  --  20
        
    }
}

class MyClass{
    //每个对象都持有一例
    int a; //实例属性
}
b. Static property
public class TestStatic{
    public static void main(String[] args){
        
        //m1.b和m2.b访问的都是方法区中的b,是同一个
        //每个类的实例都是使用的这一个b
        MyClass mc1 = new MyClass();
        MyClass mc2 = new MyClass();
        
        m1.a = 10;
        m2.a = 20;
        
        m1.b = 100;
        m2.b = 202;//覆盖了前面的m1赋值的100//自动转换 m2.b --> MyClass.b
        
        
        //不用通过对象名访问静态属性
        //直接使用类名访问
        MyClass.b = 200;
        
        System.out.println(m1.a + "--" + m2.a);//输出结果: 10  --  20
        System.out.println(m1.b + "--" + m2.b);//输出结果: 200  --  200

    }
}

class MyClass{
    
    //实例属性
    int a;
    
    //在这个类加载的时候,这个static属性就放到方法区了\
    //整个类的共享的.只有单独的一份
    static int b; //静态属性
}

2> static method

class Student{
    
    //静态方法
    //类的所有对象公用
    public static void method(){
        
    }
    
    
    public void fan(){
        //同一个类的其他方法使用本类的静态方法,直接使用方法名
        method();
    }
    
}

class Test{
     public static void main(String[] args){
        //类外使用静态方法
         //类名.方法名
         Student.method(); 
     }
}

3> static Advanced Applications

1- classloading
  • When JVM first use of a class, is the need to find .class file that requires the use of classes by CLASSPATH

  • The .class file description information of the class is loaded into memory, save (for example: information package name, class name, property, method, constructor, etc.)

  • Load time
    • Create Object
    • Create a subclass object
    • Accessing static property
    • Call the static method
    • Class.forName ( "fully qualified name") :( active loading a class, but does not receive a return value, loaded into memory but is currently not used)
class TestClassLoaded{
    public static void main(String[] args){
        new Super();//首次用到,创建了一个类的对象,就需要加载这个类
        
        Super.sMethod();//或者这个类中有静态方法,那么在首次调用这个静态方法的时候,也会加载这个类
        Super.sname;//同上
        
        Sub.sMethod();//调用父类的静态方法,首次使用也会加载super类,
        
    }
    
}

class Super{
    
    String name;
    
    static String sname;//静态属性
    
    //静态方法
    public static void sMethod(
        {
            
        }           
}
        
class Sub extends Super{
    
}
2- static code block
  • Static block of code is loaded by the class-triggered (only once)
  • It may be static property assignment, or the necessary initial behavior
  • First, static properties and static loads and executes code block
  • Again loading the common attributes and dynamic code blocks and construction methods
public class Test{
    public static void main(String[] args){
        
        //静态代码块是由类加载触发的
        
        new MyClass;
        
        System.out.print(MyClass.sname);//这两个都会执行静态代码块
        
    }
}

class MyClass{
     
    //静态属性
    static int sname = 88;
    
    int name = 99;
    
    //静态代码块
    static{
        System.out.print("这诗集静态代码块中得到输出");
        
        //使用了静态的属性
        //执行静态代码块和静态属性的时候,根据书写顺序加载
        //如这里使用了sname静态属性,那么这个属性必须定义到静态代码块前面
        System.out.print(sname);
        }
    
    MyClass{
        System.out.print("类中音译的属性值为: " + name);
        System.out.print("这是构造方法中得到输出");
    }
    
    public void m1(){
        int a = 10;
    }
}
3- execution order between the various modules
package com.huqi.demo1;

public class TestLoadSort{
    public static void main(String[] args) throws Exception{
        
        //new Super();
        new Sub();//创建一个子类的对象,查看加载各个属性和代码块的顺序
        
        //主动加载一个类,只执行静态代码的部分
        Class.forName("com.huqi.demo1.Sub");//里面的必须带包名
        /*
        如果只执行这一行代码,则会输出下面的语句,这个是类级别的,从这里下面的代码都是对象级别的
            这是父类的静态方法---
            这是父类的静态代码块---
            这是子类的静态方法---
            是子类的静态代码块---
        */
        
    }
}

class Super{

    static String staticField = "这是父类的静态方法---";
    
    static{
        System.out.println(staticField);
        System.out.println("这是父类的静态代码块---");
    }
    
    String instanceField = "这是父类的成员属性---";
    
    {
        System.out.println(instanceField);
        System.out.println("这是父类的动态代码块---");
    }
    
    public Super(){
        System.out.println("这是父类的构造方法---");
    }
    
}

class Sub extends Super{

    static String staticField2 = "这是子类的静态方法---";
    
    static{
        System.out.println(staticField2);
        System.out.println("这是子类的静态代码块---");
    }
    
    String instanceField2 = "这是子类的成员属性---";
    
    {
        System.out.println(instanceField2);
        System.out.println("这是子类的动态代码块---");
    }
    
    public Sub(){
        System.out.println("这是子类的构造方法---");
    }
}
当new Sub时的结果
    这是父类的静态方法---
    这是父类的静态代码块---
    这是子类的静态方法---
    这是子类的静态代码块---   //从这里分割,前面的是类几级别的,类加载时运行,后面的都是创建对象时执行
    这是父类的成员属性---
    这是父类的动态代码块---
    这是父类的构造方法---
    这是子类的成员属性---
    这是子类的动态代码块---
    这是子类的构造方法---

当new Super时的结果:
    这是父类的静态方法---
    这是父类的静态代码块---
    这是父类的成员属性---
    这是父类的动态代码块---
    这是父类的构造方法---

Three .final

  • final modification class: This class can not be inherited
  • final modification method: This method can not be overridden
  • final modification of variables: This variable value can not be altered (no initial value allows only assigned once.)
    • Local variables: display initialization
    • Examples of variables: initialization display, dynamic code block constructor
    • Static constant: the display initialize, static code block
    • Constant basic data types: value is unchanged
    • Application data type constant: Address immutable

1> final class

  • final modification class: This class can not be inherited.
    • String.math.System are final modified class can not be inherited
  • The final modification methods can not be overridden
public class testFinal{
    
}

//final不能和abstract一起使用,是冲突的两个
final class Super{
    
}
//发生错误,不能继承被final修饰的类
final Sub extends Super{
    
}

2> final variable

  • final modified variables, it becomes a constant

  • With a final modified property has no default value
  • Value can only be assigned once

public class testFinal{
    main(){
        //用final修饰的是常量
        //先赋值再使用
        final double PI = 3.1415;
    }
}

//final不能和abstract一起使用,是冲突的两个
final class Super{
    
    //没有默认值,必须要初始化
    //在对象初始化完成之前完成初始化的操作
    final int field = 10;
    
    final int FIELD;
    
    public Super(){
        //对常量进行初始化
        FIELD = 20;
    }
    
    //静态常量
    final static void STATIC_FIELD = 20;
    
    static{
        //可以给STATIC_FIELD赋值
        //STATIC_FIELD = 20;
    }
    
}

Guess you like

Origin www.cnblogs.com/huqisue/p/12384896.html