zoukankan      html  css  js  c++  java
  • 匿名内部类的简单使用

    1.  new 一个抽象的类:

    package com.xuzhiwen.test1;
    
    abstract class Person {
        abstract void eat();
    }
    package com.xuzhiwen.test1;
    
    public class TestPerson {
        public static void main(String[] args) {
            Person p = new Person() {
                @Override
                void eat() {
                    System.out.println("i can eat");
                }
            };
            p.eat();
        }
    }

    2. new 一个接口:

    package com.xuzhiwen.test1;
    
    public interface Animal {
        public void run();
    }
    package com.xuzhiwen.test1;
    
    public class Dog {
        public static void main(String[] args) {
            new Animal() {
                
                @Override
                public void run() {
                    System.out.println("run()..");
                }
                
                public void getName(){
                    System.out.println("getName()...");
                }
            }.getName();
        }
    }
  • 相关阅读:
    给出字符串
    Closest
    最短路计数(spfa)
    Cleaning Shifts
    Mr. Young's Picture Permutations
    Georgia and Bob
    GCD
    Cutting Game
    SP4191 MSKYCODE
    [CQOI2013]新Nim游戏
  • 原文地址:https://www.cnblogs.com/beibidewomen/p/7287674.html
Copyright © 2011-2022 走看看