Home  >  Article  >  Java  >  How to implement multiple inheritance of inner classes in Java

How to implement multiple inheritance of inner classes in Java

WBOY
WBOYforward
2023-05-13 15:58:06824browse

Explanation

1. Each internal class can be inherited by an (interface), so whether the external category has inherited an (interface) or not, it has no effect on the internal category. .

2. Without the capabilities provided by internal classes, you can inherit multiple concrete or abstract classes, and some design and programming problems will be difficult to solve.

The interface solves some problems. A class can implement multiple interfaces, and internal classes are allowed to inherit multiple non-interface types (categories or abstract classes).

Example

//类一
public class ClassA {
   public String name(){
       return "liutao";
   }
   public String doSomeThing(){
    // doSomeThing
   }
}
//类二
public class ClassB {
    public int age(){
        return 25;
    }
}
 
//类三
public class MainExample{
   private class Test1 extends ClassA{
        public String name(){
          return super.name();
        }
    }
    private class Test2 extends ClassB{
       public int age(){
         return super.age();
       }
    }
   public String name(){
    return new Test1().name();
   }
   public int age(){
       return new Test2().age();
   }
   public static void main(String args[]){
       MainExample mi=new MainExample();
       System.out.println("姓名:"+mi.name());
       System.out.println("年龄:"+mi.age());
   }
}

The above is the detailed content of How to implement multiple inheritance of inner classes in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete