zoukankan      html  css  js  c++  java
  • Java的两个特性

    第一个例子:

    import java.lang.*;
    public class Test {

        public static void main(String[] str)
        {        
            Fun(new MyInterface(){public void Say()
            {            
                System.out.println("this is temp interface");
            }});
            Fun(new People(){public void Say()
            {            
                System.out.println("this is temp class");
            }});
        }
        
        static void Fun(MyInterface p)
        {
            p.Say();        
        }
    }

    interface MyInterface
    {
        public void Say();
    }

    class People implements MyInterface
    {
        public void Say()
        {
            
            System.out.println("this is People");
        }
    }


    程序输出:

    this is temp interface
    this is temp class

    第二个例子:

    import java.lang.*;
    public class Test {

        public static void main(String[] str)
        {        
            new Test().TestFun();
        }
        
        static void Fun(MyInterface p)
        {
            
            p.Say();        
        }
        
        void Why()
        {
            System.out.println("Why");
        
        }
        
        void TestFun()
        {        
            Fun(new MyInterface(){public void Say()
            {    
                Test.this.Why();
                Why();    
                
                //如果加这一句,会进入死循环 this.Say();
                //总结,在这个方法里this代表一个MyInterface类型,而可直接使用Test的方法。
            }});
        }
    }

    interface MyInterface
    {
        public void Say();
    }
     
    程序输出:
    Why
    Why

    第三个例子

    public class Test {
        static    int Age=123;
        public static void main(String[] str) {       
            System.out.println(myInterface.getClass());
            Fun(myInterface);
            Age+=1;
            Fun(myInterface);
        }

        static void Fun(MyInterface p) {
            p.Say();
        }
        
        static MyInterface myInterface =new MyInterface() {        
            @Override
            public void Say() {
                System.out.println(Age);            
            }
        };
    }
    interface MyInterface
    {
        public void Say();
    }
     

    程序输出:

    class Test$1

    123

    124

    第四个例子:

    public class Test {
        public static void main(String[] str) {

            B.Test();
        }

        public void Run() {
            System.out.println("Test.Run");

        }
    }

    class B {
        public static void Test() {
            Test t = new Test(){
                public void Run() {
                    System.out.println(this.getClass());
                }
            };
            t.Run();
            Test t2=new Test();
            System.out.println(t2.getClass());
        }
    }
    程序输出:
    class B$1
    class Test

    第五个例子:

    class Test {
        public static void main(String[] args)
        {
            A a=new A(){
                @Override
                void Eat() {                
                    super.Eat();
                    this.Say();
                    System.out.println("a.eat");
                }
            };
            a.Eat();
            System.out.println((a instanceof A));
            System.out.println((a.getClass()));
            
        }
    }

    class A {
        void Say() {
            System.out.println("A.Say");
        }

        void Eat() {
            System.out.println("A.Eat");
        }
    }
     

    程序输出:

    A.Eat
    A.Say
    a.eat
    true
    class Test$1
     

  • 相关阅读:
    windows计数器和瓶颈
    SQL Server 2005的Resource数据库
    Android学习笔记 json数据的解析
    android的消息处理机制(图+源码分析)——Looper,Handler,Message
    JS刷新验证码
    Java取得Unix的timestamp
    (转载)DBMS_SQL package 学习
    关于Java中Http下载的一些整理
    (转载)sqlserver内存释放
    关于EL表达式
  • 原文地址:https://www.cnblogs.com/mxw09/p/1961999.html
Copyright © 2011-2022 走看看