infoperson.java
来自「压缩包内是近180多个针对Java初学者编写的简单实例」· Java 代码 · 共 53 行
JAVA
53 行
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);
}
}
//类Chinese继承类Person
class Country extends Person{
public String nation;
//指定值初始化类Chinese的对象
Country(String n,char s,int a,String na){
name = n;
sex = s;
age = a;
nation = na;
}
}
public class InfoPerson{
public static void main(String []args){
Country cou1 = new Country("zhanghua",'m',22,"China");
Country cou2 = new Country("chenyan",'f',21,"China");
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);
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?