⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 multiinhert.java

📁 180个针对Java初学者的简单实例180个针对Java初学者的简单实例180个针对Java初学者的简单实例
💻 JAVA
字号:
class Person {
    public String name;
    public char sex;
    public int age;
    //设置默认值
    Person(){
    	name = "wangliangliang";
    	sex  = 'm';
    	age  = 18;
    }
    //对象作为构造函数的参数
    Person(Person p){
    	name = p.name;
    	sex = p.sex;
    	age =p.age;
    }
    //指定值初始化对象
    Person(String name,char sex,int age){
    	this.name = name;
    	this.sex = sex;
    	this.age = age;
    }
    //输出person的基本信息
    public void info(){
    	System.out.println("The Person "+name+" is a "+sex+", aged "+age);
    }
}
 //实现所有超类的构造函数
class Country extends Person{
	public String nation;
	 //设置默认值
	Country(){
		super();
		nation = "China";
	}
   //对象作为构造函数的参数
	Country(Country ch){
		super(ch);
		nation = ch.nation;
	}
   //指定值初始化类Chinese的对象
	Country(String n,char s,int a,String na){
		super(n,s,a);
		nation = na;
	}
}
class City extends Country{
   public	String city;
   //设置默认值
   City(){
	   super();
	   city="beijing";
   }
   //对象作为构造函数的参数
   City(City c){
	  super(c);
	  city=c.city;  
   }
   //指定值初始化类Chinese的对象
   City(String n,char s,int a,String cou,String c){
	   super(n,s,a,cou);
	   city=c;
   }   
}
public class MultiInhert {
	public static void main(String []args){
	   City c1 = new City("zhanghua",'m',22,"China","shanghai");
	   City c2 = new City("chenyan",'f',21,"China","guangzhou");
	   City c3 = new City();
	   City c4 = new City(c1);
	   
	   System.out.print("The base info of the person is:");
	   c1.info();
	   System.out.println("The nationality of the person is:"+c1.nation);
	   System.out.println("The city of the person is:"+c1.city);
	   
	   System.out.print("The base info of the person is");
	   c2.info();
	   System.out.println("The nationality of the person is:"+c2.nation);
	   System.out.println("The city of the person is:"+c2.city);
	   
	   System.out.print("The base info of the person is:");
	   c3.info();
	   System.out.println("The nationality of the person is:"+c3.nation);
	   System.out.println("The city of the person is:"+c3.city);
		
	   System.out.print("The base info of the person is");
	   c4.info();
	   System.out.println("The nationality of the person is:"+c2.nation);
	   System.out.println("The city of the person is:"+c4.city);
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -