一.(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));
}
}