已知有一个类:person,请再添加其两个子类:men和women类;在主类main方法里建立两个子类的对象,分别为各属性附值并输出相应的属性。
class person {
public String name;
String sex;
protected int weight;
private int age;
public void setName(String name)/*当前方法中的形参名与成员变量名相同,则采用this关键字来区别。*/
{ this.name=name; }
protected void setWeight(int weight)
{ this.weight=weight; }
protected void setAge(int age)
{ this.age=age; }
public int getAge( )
{ return age; }
}
举例:
class men extends person
{ men( ){
sex="male"; } }
class person {
public String name;
String sex;
protected int weight;
private int age;
public void setName(String name)/*当前方法中的形参名与成员变量名相同,则采用this关键字来区别。*/
{ this.name=name; }
protected void setWeight(int weight)
{ this.weight=weight; }
protected void setAge(int age)
{ this.age=age; }
public int getAge( )
{ return age; }
}
举例:
class men extends person
{ men( ){
sex="male"; } }
public class Person {
public String name;
String sex;
protected int weight;
private int age;
public void setName(String name)/*当前方法中的形参名与成员变量名相同,则采用this关键字来区别。*/
{ this.name=name; }
public String getName(){
return this.name;
}
public void setSex(String sex)/*当前方法中的形参名与成员变量名相同,则采用this关键字来区别。*/
{ this.sex=sex; }
public String getSex(){
return this.sex;
}
protected void setWeight(int weight)
{ this.weight=weight; }
public String getWeight(){
return this.weight;
}
protected void setAge(int age)
{ this.age=age; }
public int getAge( )
{ return age; }
//主函数,运行入口
public static void main(String args[]){
men boy = new men();
boy.name = 'zhangsan';
boy.sex = '男';
boy.weight = '70kg';
boy.age ='21';
System.out.println("姓名:"+boy.name+"性别:"+boy.sex+"体重:"+boy.weight+"年龄:"+boy.age);
women girl = new women();
// women对象创建类似men
}
//这里采用内部类的方式定义men,注:这里最好把类名写成大写
class men extends Person {
//构造器
public men (){
super();
}
}
//women类
class women extends Person {
//构造器
public women (){
super();
}
} }
//主类比如是Person,注:文件名需和public类一致
class Person {
public String name;
String sex;
protected int weight;
protected int age;
public void setName(String name)/*当前方法中的形参名与成员变量名相同,则采用this关键字来区别。*/
{ this.name=name; }
public String getName(){
return this.name;
}
public void setSex(String sex)/*当前方法中的形参名与成员变量名相同,则采用this关键字来区别。*/
{ this.sex=sex; }
public String getSex(){
return this.sex;
}
protected void setWeight(int weight)
{ this.weight=weight; }
public int getWeight(){
return weight;
}
protected void setAge(int age)
{ this.age=age; }
public int getAge( )
{ return age; }
//主函数,运行入口
public static void main(String args[]){
men boy = new men();
boy.name ="zhangsan";
boy.sex = "男";
boy.weight = 70;
boy.age=21;
System.out.println("姓名:"+boy.name+"性别:"+boy.sex+"体重:"+boy.weight+"年龄:"+boy.age);
women girl = new women();
// women对象创建类似men} }
//这里采用内部类的方式定义men,注:这里最好把类名写成大写
class men extends Person {
//构造器
public men (){
super();
}
}
//women类
class women extends Person {
//构造器
public women (){
super();
}
}
class Person
{
public String name;
char sex;
protected int weight;
protected int age;
public void setName(String name)
{ this.name=name; }
public String getName()
{
return this.name;
}
public void setSex(char sex)
{ this.sex=sex;}
public char getSex()
{
return this.sex;
}
protected void setWeight(int weight)
{ this.weight=weight; }
public int getWeight()
{
return this.weight;
}
protected void setAge(int age)
{ this.age=age; }
public int getAge( )
{ return age; }
class men extends Person
{
public men ()
{
super();
}
}
//女的类似
public static class Test3
{
public static void main(String[] args)
{
men boy = new men();
boy.name = "zhangsan";
boy.sex = '男';
boy.weight = 70;
boy.age =21;
System.out.println("姓名:"+boy.name+"性别:"+boy.sex+"体重:"+boy.weight+"年龄:"+boy.age);
//女的类似
}
} }