其它多继承实现(不完备)

12、内部类(多继承)

文章分类:Java编程

首先,要清楚继承的目的是为了复用。Java只能继承一个类,不支持多继承。即没有extends Class1,Class2的语句形式。但我们可以通过内部类现模拟这一实现。

java的非静态内部类可以使用外部类的所有成员方法和变量。这给继承多个类的同名成员并共享带来可能。同时非匿名内部类可以继承一个父类和实现多个接口,因此外部类想要多继承的类可以分别由内部类继承,并进行Override或者直接复用。然后外部类通过创建内部类的对象来使用该内部对象的方法和成员,从而达到复用的目的,这样外部内就具有多个父类的所有特征。这里的多继承可以说是外部类继承自一个内部类对象,而不是类,内部类 is in a 外部类,外部内的所有行为都是通过内部类对象动态获得的。
下面是采用组合多个内部类的方式模拟多继承的实例(基于对象层面,即组合多个内部类对象):

Java代码
//手机   
  1. abstract class Mobile {   
  2.     public abstract void call();   
  3. }   
  4.   
  5. // MP3播放器   
  6. abstract class Mp3Palyer {   
  7.     public abstract void play();   
  8. }   
  9.   
  10. // 智能手机   
  11. class SmartPhone {   
  12.     private Mobile mb = new SmartMobile();   
  13.     private Mp3Palyer mp3 = new PhoneMp3();   
  14.   
  15.     public Mobile getMobile() {   
  16.         return mb;   
  17.     }   
  18.   
  19.     public Mp3Palyer getMp3() {   
  20.         return mp3;   
  21.     }   
  22.   
  23.     public class SmartMobile extends Mobile {   
  24.         @Override  
  25.         // 不同的智能机有的call方式   
  26.         public void call() {   
  27.             System.out.println("Call phone!");   
  28.         }   
  29.     }   
  30.   
  31.     public class PhoneMp3 extends Mp3Palyer {   
  32.         @Override  
  33.         // 不同的智能机有的play方式   
  34.         public void play() {   
  35.             System.out.println("Play music!");   
  36.         }   
  37.     }   
  38. }   
  39.   
  40. public class MutiImpTest1 {   
  41.     static void call(Mobile m) {   
  42.         m.call();   
  43.     }   
  44.   
  45.     static void play(Mp3Palyer p) {   
  46.         p.play();   
  47.     }   
  48.   
  49.     public static void main(String[] args) {   
  50.         SmartPhone sp = new SmartPhone();   
  51.         call(sp.getMobile());// 智能手机具有手机功能   
  52.         play(sp.getMp3());// 又具有Mp3的功能   
  53.     }   
  54. }  
//手机
abstract class Mobile {
	public abstract void call();
}

// MP3播放器
abstract class Mp3Palyer {
	public abstract void play();
}

// 智能手机
class SmartPhone {
	private Mobile mb = new SmartMobile();
	private Mp3Palyer mp3 = new PhoneMp3();

	public Mobile getMobile() {
		return mb;
	}

	public Mp3Palyer getMp3() {
		return mp3;
	}

	public class SmartMobile extends Mobile {
		@Override
		// 不同的智能机有的call方式
		public void call() {
			System.out.println("Call phone!");
		}
	}

	public class PhoneMp3 extends Mp3Palyer {
		@Override
		// 不同的智能机有的play方式
		public void play() {
			System.out.println("Play music!");
		}
	}
}

public class MutiImpTest1 {
	static void call(Mobile m) {
		m.call();
	}

	static void play(Mp3Palyer p) {
		p.play();
	}

	public static void main(String[] args) {
		SmartPhone sp = new SmartPhone();
		call(sp.getMobile());// 智能手机具有手机功能
		play(sp.getMp3());// 又具有Mp3的功能
	}
}

 

下面采用继承与匿名内部类的方式模拟(一个是类层面的,一个是对象层面,即外部类首先继承一个类,然后通过引用内部类对象来继承另外一个类):

Java代码
  1. //手机   
  2. abstract class Mobile {   
  3.     public abstract void call();   
  4. }   
  5.   
  6. // MP3播放器   
  7. abstract class Mp3Palyer {   
  8.     public abstract void play();   
  9. }   
  10.   
  11. // 智能手机,继承自Mobile   
  12. class SmartMobile extends Mobile {   
  13.     @Override  
  14.     // 不同的智能机有的call方式   
  15.     public void call() {   
  16.         System.out.println("Call phone!");   
  17.     }   
  18.   
  19.     // 智能手机也有播放音乐的功能   
  20.     public Mp3Palyer getMp3() {   
  21.         return new Mp3Palyer() {   
  22.             @Override  
  23.             // 不同的智能机有的play方式   
  24.             public void play() {   
  25.                 System.out.println("Play music!");   
  26.             }   
  27.         };   
  28.     }   
  29. }   
  30.   
  31. public class MutiImpTest2 {   
  32.     static void call(Mobile m) {   
  33.         m.call();   
  34.     }   
  35.   
  36.     static void play(Mp3Palyer p) {   
  37.         p.play();   
  38.     }   
  39.   
  40.     public static void main(String[] args) {   
  41.         // 现在智能手机即是手机类型,又具有Mp3的功能   
  42.         SmartMobile sp = new SmartMobile();   
  43.         call(sp);// 智能手机即是手机类型   
  44.         play(sp.getMp3());// 又具有Mp3的功能   
  45.     }   
  46. }  
//手机
abstract class Mobile {
	public abstract void call();
}

// MP3播放器
abstract class Mp3Palyer {
	public abstract void play();
}

// 智能手机,继承自Mobile
class SmartMobile extends Mobile {
	@Override
	// 不同的智能机有的call方式
	public void call() {
		System.out.println("Call phone!");
	}

	// 智能手机也有播放音乐的功能
	public Mp3Palyer getMp3() {
		return new Mp3Palyer() {
			@Override
			// 不同的智能机有的play方式
			public void play() {
				System.out.println("Play music!");
			}
		};
	}
}

public class MutiImpTest2 {
	static void call(Mobile m) {
		m.call();
	}

	static void play(Mp3Palyer p) {
		p.play();
	}

	public static void main(String[] args) {
		// 现在智能手机即是手机类型,又具有Mp3的功能
		SmartMobile sp = new SmartMobile();
		call(sp);// 智能手机即是手机类型
		play(sp.getMp3());// 又具有Mp3的功能
	}
}

 

上面的多继承是站在外部类的角度来看的,即它们是通过外部类引用内部类来达到多态与复用的目的。反过来,内部类继承了一个类,同时拥有了外部类的所有成员方法和属性,我们是否可以认为内部类集成了两个类呢?——一个是类层面的,一个是对象层面的(因为非静态内部类使用前一定有外部类的对象来创建它,它持有外部类某个对象的引用)。如果外部类还继承了其他类呢?内部类还是可以访问其他类的方法与属性。现在从内部类的角度来模拟多继承:

Java代码
  1. //智能机抽象类   
  2. abstract class SmartMobile {   
  3.     public abstract void call();   
  4.   
  5.     public void play() {   
  6.         System.out.println("Play music!");   
  7.     }   
  8. }   
  9.   
  10. // 手机   
  11. class Mobile {   
  12.     /*  
  13.      * 该方法是私有的,与SmartMobile类中方法同名,所以  
  14.      * Mobile不能直接继承自SmartMobile,因为重写时不能  
  15.      * 缩小访问权限,所以只能使用一个内部类来重写。  
  16.      */  
  17.     private void call() {   
  18.         System.out.println("Call phone!");   
  19.     }   
  20.   
  21.     public SmartMobile getSmartMobileImp() {   
  22.         // 智能机的实现,好比多继承(继承外部类与SmartMobile)   
  23.         return new SmartMobile() {   
  24.             // 调用“继承”自外部类的相应方法来实现call   
  25.             public void call() {   
  26.                 // 回调外部类真真的实现   
  27.                 Mobile.this.call();   
  28.             }   
  29.         };   
  30.     }   
  31. }   
  32.   
  33. public class MutiImpTest3 {   
  34.     static void call(SmartMobile m) {   
  35.         m.call();   
  36.     }   
  37.   
  38.     static void play(SmartMobile p) {   
  39.         p.play();   
  40.     }   
  41.   
  42.     public static void main(String[] args) {   
  43.         // 智能机即是手机也是Mp3播放器   
  44.         SmartMobile sp = new Mobile().getSmartMobileImp();   
  45.         call(sp);// 智能机即是手机   
  46.         play(sp);// 也是是Mp3播放器   
  47.     }   
  48. }  
//智能机抽象类
abstract class SmartMobile {
	public abstract void call();

	public void play() {
		System.out.println("Play music!");
	}
}

// 手机
class Mobile {
	/*
	 * 该方法是私有的,与SmartMobile类中方法同名,所以
	 * Mobile不能直接继承自SmartMobile,因为重写时不能
	 * 缩小访问权限,所以只能使用一个内部类来重写。
	 */
	private void call() {
		System.out.println("Call phone!");
	}

	public SmartMobile getSmartMobileImp() {
		// 智能机的实现,好比多继承(继承外部类与SmartMobile)
		return new SmartMobile() {
			// 调用“继承”自外部类的相应方法来实现call
			public void call() {
				// 回调外部类真真的实现
				Mobile.this.call();
			}
		};
	}
}

public class MutiImpTest3 {
	static void call(SmartMobile m) {
		m.call();
	}

	static void play(SmartMobile p) {
		p.play();
	}

	public static void main(String[] args) {
		// 智能机即是手机也是Mp3播放器
		SmartMobile sp = new Mobile().getSmartMobileImp();
		call(sp);// 智能机即是手机
		play(sp);// 也是是Mp3播放器
	}
}

另外,从上面程序可看出内部类的另一作用:如果你想继承一个类或实现一个接口,但是这个接口或类中的一个方法和你构想的这个类中的一个方法的名称,参数相同,但访问权限缩小了或者是在继承时不想覆盖签名相同但功能不同的方法,所以你不能直接继承与实现它,你应该怎么办?这时候,你可以建一个内部类继承这个类或实现这个接口(当然你可以修改访问权限是可以的)。由于内部类对外部类的所有内容都是可访问的,内部类可以通过调用外部类的这个方法来重写那个类或接口。上面的Mobile类中的call方法就是这种情况,所以你不能直接让Mobile去继承SmartMobile,固只能采用内部类来达到重写的目的。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值