概述
(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 lixaofeng;
public interface Drinking {
void drink();//喝酒
void throwup();//吐
void playMad();//耍酒疯
}
package lixaofeng;
public interface Learning {
void preLearn();// 预习
void lessons();//上课
void reveiw();//复习
}
package lixaofeng;
public abstract class Person {
private String name;
private String sex;
private int age;
public Person()
{
}
public Person(String name,String sex,int age)
{
super();
this.name=name;
this.sex=sex;
this.age=age;
}
public abstract void love(String name);
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 lixaofeng;
public class Client
{
public static void main(String[] args)
{
Student stu1=new Student("04151028","西安邮电大学","王茜","女",20);
Student stu2=new Student("04151029","西安邮电大学","雨林枫","男",20);
Student stu3=new Student("04151028","西安邮电大学","王茜","女",20);
Officer off=new Officer("国务院总理","清雨","男",59);
Programmer pro=new Programmer(1000,"依若","女",22);
System.out.println("ttttt---------学生模块---------");
stu1.preLearn();
stu1.lessons();
stu1.reveiw();
stu1.chatting();
stu1.love("张伟");
if(stu1.equals(stu1)==true)
System.out.println("这两个学生(stu1和stu2)是同一个人");
else
System.out.println("这两个学生(stu1和stu2)不是同一个人");
if(stu1.equals(stu3)==true)
System.out.println("这两个学生(stu1和stu3)是同一个人");
else
System.out.println("这两个学生(stu1和stu2)不是同一个人");
System.out.println("学生stu2的具体信息为:"+stu2);
System.out.println("ttttt---------公务员模块---------");
off.meeting();
off.love("靳美美");
off.drink();
off.throwup();
off.playMad();
System.out.println("该公务员的具体信息为:"+off);
System.out.println("ttttt---------程序员模块---------");
pro.preLearn();
pro.lessons();
pro.reveiw();
pro.coding();
pro.bug();
System.out.println("该程序员的具体信息为:"+pro);
System.out.println("ttttt--------测试对象实际类型模块---------");
method1(stu1);
method1(off);
method1(pro);
}
public static void method1(Person p)
{
if(p==null)
{
System.out.println("不能直接传递null!");
return;//结束方法
}
if(p instanceof Student)
{
System.out.println("该对象是Student类型");
Student stu=(Student)p;
stu.chatting();
}
if(p instanceof Officer)
{
System.out.println("该对象是Officer类型");
Officer off=(Officer)p;
off.meeting();
}
if(p instanceof Programmer)
{
System.out.println("该对象是Programmer类型");
Programmer pro=(Programmer)p;
pro.coding();
pro.bug();
}
}
}
package lixaofeng;
public class Officer extends Person implements Drinking
{
private String grade;//公务员级别
@Override
public void love(String name)
{
System.out.println(this.getName()+"和"+name+"正在谈恋爱!");
}
@Override
public void drink()//喝酒
{
System.out.println(this.getName()+"公务员外出去喝酒!");
}
public String getGrade()
{
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
@Override
public void throwup()//吐
{
System.out.println(" 喝酒喝多了,"+this.getName()+"公务员在吐!");
}
@Override
public void playMad()//耍酒疯
{
System.out.println(" 吐完之后,"+this.getName()+"公务员在耍酒疯!");
}
public void meeting()
{
System.out.println("级别为"+this.grade+" ,名字为"+this.getName()+"的公务员在开会!");
}
public Officer()
{
}
public Officer(String grade,String name,String sex,int age)
{
super(name, sex, age);
this.grade = grade;
}
public String toString()
{
return "姓名: "+getName()+",性别: "+getSex()+",年龄: "+getAge()+",级别为: "+grade;
}
}
package lixaofeng;
public class Programmer extends Person implements Learning
{
private double salary;
public double getSalary()
{
return salary;
}
public void setSalary(double salary)
{
this.salary = salary;
}
public Programmer()
{
super();
}
public Programmer(double salary,String name,String sex,int age)
{
super(name, sex, age);
this.salary = salary;
}
@Override
public void love(String name)//谈恋爱
{
System.out.println("程序猿"+this.getName()+"和"+name+"正在谈恋爱!");
}
@Override
public void preLearn()//预习
{
System.out.println(" 名字为"+this.getName()+"的程序员正在预习!");
}
@Override
public void lessons()//上课
{
System.out.println("预习完了,"+this.getName()+"程序员正在上课!");
}
@Override
public void reveiw()//复习
{
System.out.println("上课完了,"+this.getName()+"程序员正在复习!");
}
public void coding()
{
System.out.println(this.getName()+"程序员正在写带代码!");
}
public void bug()
{
System.out.println("代码敲完之后,"+this.getName()+"程序员正在修改bug!");
}
@Override
public String toString()
{
return "姓名: "+getName()+",性别: "+getSex()+",年龄: "+getAge()+",薪水为: "+salary;
}
}
package lixaofeng;
public class Student extends Person implements Learning
{
private String number;//学号
private String schoolname ;//学校
@Override
public void love(String name)//谈恋爱
{
System.out.println(this.getName()+"在和"+name+"正在谈恋爱!");
}
@Override
public void preLearn()//预习
{
System.out.println("学号为"+this.number+" ,名字为"+this.getName()+"的同学正在预习!");
}
public String getNumber() //Number访问器
{
return number;
}
public void setNumber(String number) //Number修改器
{
this.number = number;
}
@Override
public void lessons()//上课
{
System.out.println("学号为"+this.number+",名字为"+this.getName()+"的同学正在上课!");
}
@Override
public void reveiw()//复习
{
System.out.println("上完课后"+this.getName()+"同学正在复习!");
}
public void school()//学校
{
System.out.println(this.getName()+"同学在"+this.schoolname+"上学!");
}
public void chatting()
{
System.out.println("复习完了,"+this.getName()+"同学正在和朋友聊天!");
}
Student()
{
}
public Student(String number, String schoolname,String name,String sex,int age)
{
super(name, sex, age);
this.number = number;
this.schoolname = schoolname;
}
@Override
public String toString()
{
return "姓名: "+getName()+",性别: "+getSex()+",年龄: "+getAge()+",学校:"+schoolname+",学号"+number;
}
public boolean equals(Student obj)
{
if(obj==null)
{
System.out.println("该对象不存在,无法比较!");
return false;
}
else if((obj instanceof Student)==false)
{
System.out.println("传入对象类型错误!");
return false;
}
else if((obj.getName().equals(this.getName())==true)&&(obj.getAge()==this.getAge())&&(obj.getSex().equals(this.getSex())==true)&&(obj.schoolname.equals(this.schoolname)==true))
return true;
else
return false;
}
}
最后
以上就是威武指甲油为你收集整理的包装类和多态性代码的全部内容,希望文章能够帮你解决包装类和多态性代码所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复