我是靠谱客的博主 瘦瘦雪糕,最近开发中收集的这篇文章主要介绍12.24作业,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

(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 homework7;



    public interface Learning
    {
         abstract void prelearn();
         abstract void lessons();
         abstract void review();
    }



package homework7;


    public interface Drinking
    {
        abstract void drink();
        abstract void throwUp();
        abstract void playMad();
    }
package homework7;

public abstract class Person
{
    private String name;
    private String sex;
    private int age;

    public Person()
    {

    }
    Person(String name,String sex,int age)
    {
        super();
        this.name=name;
        this.sex=sex;
        this.age=age;
    }
    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;
    }
    public abstract void love();

}
package homework7;

public class Student extends Person implements Learning
{
    private String school;

    Student()
    {}
    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;
    }
    @Override
    public void love() {
        System.out.println("students' loving");

    }
    public void prelearn() {
        System.out.println("students'prelearning");

    }
    public void lessons() {
        System.out.println("students'lessoning");

    }
    public void review() {
        System.out.println("students'reviewing");
    }
    public void chatting() {
        System.out.println("students'chatting");
    }

    public String toString() {
        return "姓名:"+getName()+",性别:"+getSex()+",年龄:"+getAge()+",学校:"+getSchool();
    }

    @Override
    public boolean equals(Object object) 
    {
        if(object==null)
        {
            return false;
        }else if(object==this)
        {
            return true;
        }else if(!(object instanceof Student))
        {
            return false;
        }
        Student student=(Student)object;
        if(getName().equals(student.getName()) && getSex().equals(student.getSex()) && getAge()==student.getAge() && school.equals(student.school)){
            return true;
        }
        return false;
    }
}


package homework7;
public class Officer extends Person implements Drinking{

    @Override
    public void drink() {
        System.out.println("喝酒中");
    }

    @Override
    public void throwUp() {
    System.out.println("喝多了,扔酒瓶!");
    }

    @Override
    public void playMad() {
        System.out.println("发酒疯。。。。");
    }

    @Override
    public void love() {

    }
    public void meeting(){
        System.out.println("officer is having meetings.....");
    }
}
package homework7;

public class Client
{
    public void method(Person person)
    {
        if(person==null)
        {
        System.out.println("输入无效,请重新输入~~~~~~");   
        }
        else if(person instanceof Student)
        {
            Student student=(Student)person;
            student.chatting();
        }
        else if(person instanceof Programmer)
        {
            Programmer programmer=(Programmer)person;
            programmer.coding();
            programmer.debuging();
        }
        else if(person instanceof Officer)
        {
            Officer officer=new Officer();
            officer.meeting();
        }
    }
}
package homework7;

public class Demo 
{
    public static void main(String[] args){
        Student student=new Student();
        Person programmer=new Programmer();
        Officer officer=new Officer();
        Client client=new Client();

        client.method(student);
        client.method(programmer);
        client.method(officer);
        System.out.println("                   ");
        Student student1=new Student("张三","男",20,"北大");
        System.out.println("学生1:");
        System.out.println(student1);
        Student student2=new Student("李四","女",20,"北大");
        System.out.println("学生2:");
        System.out.println(student2);
        System.out.println("                       ");
        //三目运算符:
        System.out.println(student1.equals(student2)?"是":"否");

    }

}

这里写图片描述

最后

以上就是瘦瘦雪糕为你收集整理的12.24作业的全部内容,希望文章能够帮你解决12.24作业所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(45)

评论列表共有 0 条评论

立即
投稿
返回
顶部