代码改变世界

java继承示例

2011-03-18 11:35  Rollen Holt  阅读(271)  评论(0编辑  收藏  举报
  1: /**
  2:  * 
  3:  * This program is used to test Inheritance
  4:  * @author Rollen  Holt
  5:  */
  6: 
  7:  class SuperClass
  8: {
  9: 	int number1;
 10: 	int number2;
 11: 	SuperClass(){
 12: 		number1=number2=1;
 13: 	}
 14: 	SuperClass(int temp){
 15: 		number1=number2=temp;
 16: 	}			
 17: 	SuperClass(int temp1, int temp2 ){
 18: 		number1=temp1;
 19: 		number2=temp2;
 20: 	}
 21: 	void Show(){
 22: 		System.out.println("SupceClass");
 23: 		System.out.println(number1);
 24: 		System.out.println(number2);
 25: 	}
 26: }
 27: 
 28: public class Inheritance extends SuperClass {
 29: 	int number3;
 30: 	Inheritance(){
 31: 		super();
 32: 		number3=1;
 33: 	}
 34: 	Inheritance(int temp){
 35: 		super(temp);
 36: 		number3=temp;
 37: 	}
 38: 	Inheritance(int temp1, int temp2, int temp3){
 39: 		super(temp1,temp2);
 40: 		number3=temp3;
 41: 	}
 42: 	
 43: 	void Show(){
 44: 		super.Show();
 45: 		System.out.println(number3);
 46: 	}
 47: 	
 48: 	public static void main(String[] args) {
 49: 		System.out.println("This program is used to test Inheritance");
 50: 		
 51: 		SuperClass superNumber1 = new SuperClass();
 52: 		superNumber1.Show();
 53: 		
 54: 		SuperClass superNumber2 = new SuperClass(2);
 55: 		superNumber2.Show();
 56: 		
 57: 		SuperClass superNumber3 = new SuperClass(2,2);
 58: 		superNumber3.Show();
 59: 		
 60: 		System.out.println("Inheritance Class");
 61: 		
 62: 		Inheritance inher1 = new Inheritance();
 63: 		inher1.Show();
 64: 		
 65: 		Inheritance inher2 = new Inheritance(2);
 66: 		inher2.Show();
 67: 		
 68: 		Inheritance inher3 = new Inheritance(1,2,3);
 69: 		inher3.Show();
 70: 
 71: 	}
 72: 
 73: }
 74: