zoukankan      html  css  js  c++  java
  • Java中的多重继承

    1. Java子类只能继承一个基类,但可以实现多个接口。

    package com.test.wx;
    
    public class Test {
        interface CanFight{
            void fight();
        }
        interface CanSwim{
            void swim();
        }
        interface CanFly{
            void fly();
        }
        class Action{
            public void fight(){ System.out.println("Action");}
        }
    
        class Hero extends Action implements CanFight,CanSwim,CanFly
        {
    
            @Override
            public void fly() {
                // TODO Auto-generated method stub
                System.out.println("fly");
            }
    
            @Override
            public void swim() {
                // TODO Auto-generated method stub
                System.out.println("swim");
            }
            
        }
        
        public static void t(CanFight x){x.fight();}
        public static void u(CanSwim x){x.swim();}
        public static void v(CanFly x){x.fly();}
        public static void w(Action x){x.fight();}
        public static void main(String[] args) {
            Hero h= new Test().new Hero(); //内部类对象的创建需要使用.new
            t(h);
            u(h);
            v(h);
            w(h);
            
        }
    }
    View Code

    执行结果:

    Action
    swim
    fly
    Action
    View Code

    2. 接口的多继承。

    package com.test.wx;
    
    public class Test {
        interface A{
            void fa();        
        }
        interface B{
            void fb();
        }
        interface C extends A,B{
            void fc();
        }
        
        class DDD implements C{
    
            @Override
            public void fa() {
                // TODO Auto-generated method stub
            }
    
            @Override
            public void fb() {
                // TODO Auto-generated method stub
                
            }
    
            @Override
            public void fc() {
                // TODO Auto-generated method stub
                
            }
            
        }
    
    }
    View Code
  • 相关阅读:
    win32-LPCSTR->String
    win32-UpdateLayeredWindow
    win32-CreateDIBSection的使用
    win32-StretchDIBits
    hdu1978 简单记忆化搜索
    hdu1978 简单记忆化搜索
    hdu4530 水题
    hdu4530 水题
    hdu3006 状态压缩+位运算+hash(小想法题)
    hdu3006 状态压缩+位运算+hash(小想法题)
  • 原文地址:https://www.cnblogs.com/wxlovewx/p/5195895.html
Copyright © 2011-2022 走看看