Using an arbitrarily defined method of an anonymous interface

前端 未结 4 941
萌比男神i
萌比男神i 2020-12-05 23:56

Consider the following code:

public static void main(String[] args) {
    File file = new File(\"C:\\\\someFile.txt\") {
        public void doStuff() {
             


        
4条回答
  •  时光取名叫无心
    2020-12-06 00:40

    It is possible to access that method. But I don't know why you would ever want to do this.

    public static void main(String[] args) {
       new File("C:\\someFile.txt") {
          public void doStuff() {
              // Do some stuff
          }   
       }.doStuff();
    }
    

提交回复
热议问题