定义类Parents,Father, Mother, 其中Parents为父类,Father, Mother为子类, 测试其特性。

【问题描述】
定义类Parents,Father, Mother, 其中Parents为父类,Father, Mother为子类, 测试其特性。

【输入形式】
请输入你父亲的名字:Jack请输入你母亲的名字:Rose请输入你父亲的年龄:30请输入你母亲的年龄:32
【输出形式】
Please input your father’s name:
Please input your mother’s name:
Please input your father’s age:
Please input your mother’s age:

【样例输入】
JackRose3032

【样例输出】

I am the father.
my name is: Jackmy age is: 30my sex is: MI am the mother.my name is: Rosemy age is: 32my sex is: F

import  java.util.Scanner;

class  Parents  {

        private  String  name;

        private  int  age;

        private  String  sex;

        

        public  String  getName()  {

                return  name;

        }

        

        public  void  setName(String  name)  {

                this.name  =  name;

        }

        

        public  int  getAge()  {

                return  age;

        }

        

        public  void  setAge(int  age)  {

                this.age  =  age;

        }

        

        public  String  getSex()  {

                return  sex;

        }

        

        public  void  setSex(String  sex)  {

                this.sex  =  sex;

        }

        

        public  void  getInfo()  {

                System.out.println("my  name  is:  "  +  getName());

                System.out.println("my  age  is:  "  +  getAge());

                System.out.println("my  sex  is:  "  +  getSex());

        }  

}

class  Father  extends  Parents  {

        public void message() {
		System.out.println("I am the father.");
	}


}

class  Mother  extends  Parents  {

        public void message() {
		System.out.println("I am the mother.");
	}

}

        

public  class  P4T1  {

        public  static  void  main(String[]  args)  {

                

                Father  f  =  new  Father();

                Mother  m  =  new  Mother();

                

                String  name;

                int  age;

                

                Scanner  s  =  new  Scanner(System.in);

                

                System.out.println("Please  input  your  father's  name:");

                f.setName(s.nextLine());

                System.out.println("Please  input  your  mother's  name:");

                m.setName(s.nextLine());

                

                System.out.println("Please  input  your  father's  age:");

                f.setAge(s.nextInt());

                System.out.println("Please  input  your  mother's  age:");

                m.setAge(s.nextInt());

                

                f.setSex("M");

                f.message();

                f.getInfo();

        

                

                m.setSex("F");

                m.message();

                m.getInfo();

        }

}
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值