学习java第30天

1.动态绑定

*在程序运行过程中,根据具体的实例对象才能具体确定是哪个方法

Father ft=new Son(); ft.say(); Son继承自Father,重写了say()

2.静态变量

在变量前家static关键字

public class SyncThread implements Runnable {

    private static int count;

    @Override

    public  void run() {

        synchronized(this) {

            for (int i = 0; i < 5; i++) {

                try {

                    System.out.println(Thread.currentThread().getName() + ":" + (count++));

                    Thread.sleep(100);

                } catch (InterruptedException e) {

                    e.printStackTrace();

                }

            }

        }

    }

    public static void main(String[] args) {

       SyncThread s1 = new SyncThread();

        SyncThread s2 = new SyncThread();

        Thread t1 = new Thread(s1);

        Thread t2 = new Thread(s2);

        t1.start();

        t2.start();

    }

}

3.静态方法

在方法前面加上static关键字,引用静态方法时,可以用类名.方法名或者对象名.方法名的形式

public class TestStatic {

    public static void main(String[]args){

        System.out.println(S.getStatic());                       //类名加前缀访问静态方法

        S s=new S();

        System.out.println(s.getStatic());                        //使用实例化对象名访问静态方法

        System.out.println(s.get());

    }

    public static class S

    {

        private static int a;

        private int t=0;

        static{

            a=10;

        }       

        public static int getStatic()

        {

            return a;     

        public int getT()

        {

            return t;

        }  

        public int get()

        {

            getT();

            getStatic();

            t=a;

            return t;

        }

    }

}

4.静态代码块

static代码块也叫静态代码块,可以有多个

public class StaticBlockDemo {
 static{
  System.out.println("静态代码块");
 }
 public StaticBlockDemo(){
  System.out.println("构造方法");
 }
  public static void main(String[] args) {
  StaticBlockDemo d = new StaticBlockDemo();
  StaticBlockDemo d2 = new StaticBlockDemo();
 }
}

静态代码块优先于构造方法执行,且在加载时只执行一次

5.明天学习内容:Static关键字,Final关键字,Abstract关键字

.

.

原文地址:https://www.cnblogs.com/SirNie/p/13434598.html