一.(1)定义以下这些接口和类,并完成相关属性和方法的声明和调用.数据类型,参数列表,返回类型等请根据题目需要自行定义.
学习接口Learning(包含方法: 预习preLearn,上课lessons,复习reveiw)
喝酒接口Drinking(包含方法: 喝酒drink, 吐throwup,耍酒疯playMad)
抽象类Person(包含属性:姓名,性别,年龄; 抽象方法:谈恋爱love)
学生Student是人,得会学习,但不能喝酒(因为会使大脑变笨);他们还有自己的学校(school),还欢和朋友聊微信(chatting).
公务员Officer是人,不用学习,但经常需要喝酒应酬;他们还得经常开一些无聊的会议(meeting).
程序猿Programmer,是人,必须经常学习,较少社交所以不喝酒;他们特别喜欢写代码(coding),修bug(debuging).
(2)在场景类Client中定义一个方法method1,在形参和实参上体现对象的多态性,在方法中进行调用,如果对象的实际类型是学生,就和朋友聊微信;如果是公务员,就去开会;如果是程序猿,就去写代码和修bug.
(3)直接打印一个学生对象,就能以下面格式来输出:学生信息: 姓名:张三,性别:男,年龄:20,学校:北大.
(4)如果两个学生的姓名、性别、年龄、学校一样,则认为这两个学生“相等”。
package text4;

/*

  • 学习接口Learning(包含方法: 预习preLearn,上课lessons,复习reveiw)
  • */
    public interface Learning {
    public void preLearn(); // 预习方法

    public void lessons(); // 上课方法

    public void reveiw(); // 复习方法
    }
    package text4;

/*

  • 喝酒接口Drinking(包含方法: 喝酒drink, 吐throwup,耍酒疯playMad)
  • */
    public interface Drinking {
    public void drink(); // 喝酒方法

    public void throwup(); // 吐方法

    public void playMad(); // 耍酒疯方法
    }
    package text4;

/*

  • 抽象类Person(包含属性:姓名,性别,年龄; 抽象方法:谈恋爱love)
  • */
    public abstract class Person {

    private String name;
    private String sex;
    private int age;

    public Person() {

    }

    public Person(String name, String sex, int age) {
    this.name = name;
    this.sex = sex;
    this.age = age;
    }

    public abstract void love();

    public String getName() {
    return name;
    }

    public void setName(String name) {
    this.name = name;
    }

    public String getSex() {
    return sex;
    }

    public void setSex(String sex) {
    this.sex = sex;
    }

    public int getAge() {
    return age;
    }

    public void setAge(int age) {
    this.age = age;
    }

}
package text4;
/*

  • 学生Student是人,得会学习,但不能喝酒(因为会使大脑变笨);他们还有自己的学校(school),还喜欢和朋友聊微信(chatting).
  • */
    public class Student extends Person implements Learning{
    private String school;

    public Student(){

    }

    public Student(String name, String sex, int age,String school) {
    super(name,sex,age);
    this.school = school;
    }

    public String getSchool() {
    return school;
    }

    public void setSchool(String school) {
    this.school = school;
    }

    public void preLearn() {
    System.out.println(getName()+"学生预习...");

    }

    public void lessons() {
    System.out.println(getName()+"学生上课...");

    }

    public void reveiw() {
    System.out.println(getName()+"学生复习...");

    }

    public boolean equals(Object obj){
    if(this == obj){
    return true;
    }
    if(obj instanceof Student){
    Student objNew = (Student) obj;
    if(this.getName() == objNew.getName() && this.getSex() == objNew.getSex()
    && this.getSchool() == objNew.getSchool() && this.getAge() == objNew.getAge()){
    return true;
    }
    }
    return false;
    }
    public void love() {
    System.out.println(getName()+"学生谈恋爱...");

    }

    public void chatting(){
    System.out.println(getName()+"和朋友聊微信...");
    }
    public String toString(){
    return "姓名:"+getName()+"性别:"+getSex()+"年龄:"+getAge()+"学校:"+getSchool();
    }
    }
    package text4;
    /*

  • 公务员Officer是人,不用学习,但经常需要喝酒应酬;他们还得经常开一些无聊的会议(meeting).
  • */
    public class Officer extends Person implements Drinking{

    public Officer() {

    }

    public Officer(String name, String sex, int age) {
    super(name, sex, age);
    }

    public void drink() {
    System.out.println(getName()+"公务员喝酒");

    }

    public void throwup() {
    System.out.println(getName()+"公务员吐");

    }

    public void playMad() {
    System.out.println(getName()+"公务员耍酒疯");

    }

    public void love() {
    System.out.println(getName()+"公务员谈恋爱...");

    }
    public void meeting(){
    System.out.println(getName()+"公务员开无聊的会议...");
    }
    }
    package text4;
    /*

  • 程序猿Programmer,是人,必须经常学习,较少社交所以不喝酒;他们特别喜欢写代码(coding),和修bug(debuging).
  • */
    public class Programmer extends Person implements Learning{

    public Programmer() {

    }

    public Programmer(String name, String sex, int age) {
    super(name, sex, age);

    }

    public void preLearn() {
    System.out.println(getName()+"程序猿预习...");

    }

    public void lessons() {
    System.out.println(getName()+"程序猿上课...");

    }

    public void reveiw() {
    System.out.println(getName()+"程序猿复习...");

    }

    public void love() {
    System.out.println(getName()+"程序猿谈恋爱...");

    }

    public void coding(){
    System.out.println(getName()+"程序员特别喜欢写代码...");
    }
    public void debuging(){
    System.out.println(getName()+"程序员修bug...");
    }

}
package text4;
/*

  • 在场景类Client中定义一个方法method1,在形参和实参上体现对象的多态性,在方法中进行调用,
  • */
    public class Client {

    public void method1(Person per){
    if(per==null){
    System.out.println("不能直接传递null!");
    return;
    }
    if(per instanceof Student){
    Student stu=(Student)per;
    stu.chatting();
    }
    if(per instanceof Officer){
    Officer off=(Officer)per;
    off.meeting();
    }
    if(per instanceof Programmer){
    Programmer pro=(Programmer)per;
    pro.coding();
    pro.debuging();
    }
    }
    }
    package text4;

public class TestPerson {

/**
 *  学生Student
    公务员Officer
    程序猿Programmer
 */
public static void main(String[] args) {
    Student stu = new Student("张三","男",20,"北大");
    Officer off = new Officer("李四","男",22);
    Programmer pro = new Programmer("王五","女",24);
    Client cli=new Client();    //为Client实例化一个cli对象
    cli.method1(stu);
    cli.method1(off);
    cli.method1(pro);
    System.out.println(stu);
    Student stu1 = new Student("张三","男",20,"北大");
    System.out.println( stu.equals(stu1));

}

}