📄 demosuper.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;
}
}
public class DemoSuper {
public static void main(String []args){
Country cou1 = new Country("zhanghua",'m',22,"China");
Country cou2 = new Country("chenyan",'f',21,"China");
Country cou3 = new Country();
Country cou4 = new Country(cou1);
System.out.print("The base info of the person is:");
cou1.info();
System.out.println("The nationality of the person is:"+cou1.nation);
System.out.print("The base info of the person is");
cou2.info();
System.out.println("The nationality of the person is:"+cou2.nation);
System.out.print("The base info of the person is:");
cou3.info();
System.out.println("The nationality of the person is:"+cou3.nation);
System.out.print("The base info of the person is");
cou4.info();
System.out.println("The nationality of the person is:"+cou2.nation);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -