import java.util.Scanner;
class Man extends Person
{
Man()
{
System.out.println("Man的构造方法");
}
public void Show()
{
System.out.println(name+" "+add);
}
}
class Woman extends Person
{
Woman()
{
System.out.println("Woman的构造方法");
}
public void Show()
{
System.out.println(name+" "+add+" "+sex);
}
}
class Person
{
String name;
String add;
String sex;
void work()
{
System.out.println("我要挣钱");
}
}
class Demo1
{
public static void main(String[] args)
{
//Woman w = new Woman();
//Man m = new Man();
System.out.println("数量");
Scanner sc=new Scanner(System.in);
int count=sc.nextInt();
Person[] arrP=new Person[count];
int x=0;
int y=0;
for (int i=0;i<count;i++ )
{
Person p=new Person();
System.out.print("姓名");
p.name=sc.next();
System.out.print("地址");
p.add=sc.next();
System.out.print("性别");
p.sex=sc.next();
switch (p.sex)
{
case "男":
{
x++;
break;
}
case "女":
{
y++;
break;
}
}
arrP[i]=p;
}
Man[] m=new Man[x];
Woman[] w=new Woman[y];
int a=0;
int b=0;
for (int i=0;i<count;i++)
{
Man m1=new Man();//实例化对象
Woman w1=new Woman();
switch (arrP[i].sex)
{
case "男":
{
m1.name=arrP[i].name;//m只是定义了一个Man类型的数组 要想拥有这三个属性必须是Man类的对象
m1.add=arrP[i].add;
m1.sex=arrP[i].sex;
m[a]=m1;//存到数组中
a++;
break;
}
case "女":
{
w1.name=arrP[i].name;//w只是定义了一个Woman类型的数组 要想拥有这三个属性必须是Woman类的对象
w1.add=arrP[i].add;
w1.sex=arrP[i].sex;
w[b]=w1;//存到数组中
b++;
break;
}
}
}
for (int i=0;i<b;i++)//i<=b数组下标越界
{
System.out.println(w[i].name);
}
}
}
|
|