學校中有老師和學生兩類人,而在職研究生既是老師又是學生,對學生的管理和對教師的管理在他們身上都有體現。


1)設計兩個信息管理接口StudentManageInterface和TeacherManageInterface。其中,StudentInterface接口包括setFee()方法和getFee()方法,分別用於設置和獲取學生的學費;

TeacherInterface接口包括setPay()方法和getPay()方法,分別用於設置和獲取教師的工資

2) 定義一個研究生類Graduate,實現StudentInterface接口和TeacherInterface接口,它定義的成員變量有name(姓名)、sex(性別)、age(年齡)、fee(每學期學費)、pay(月工資)。

3) 創建一個姓名為“zhangsan”的研究生,統計他的年收入和學費,如果收入減去學費不足2000元,則輸出“provide a loan”(需要貸款)信息。

提示:

1)定義兩個接口,分別在其中申明兩個方法。

2)定義主類Graduate,實現這兩個接口。

3)定義主類的成員變量,和構造方法。

4)給出四個接口方法的實現。

5)給出一個計算是否需要貸款的方法,在里面統計年收入和學費,並輸出是否需要貸款的信息。

6)main方法。在其中創建一個姓名為“zhangsan”的研究生,調用計算是否需要貸款的方法。

 

接口:只有抽象方法的類

以下具體代碼具體分析:

 1 interface StudentManageInterface{    //interface 接口名
 2 public abstract void setFree(double fee);  //抽象方法
 3 public abstract double getFree();
 4 }
 5 interface TeacherManageInterface{
 6 public abstract double getPay();
 7 public abstract void setPay(double pay);
 8 }
 9 class Graduate implements StudentManageInterface,TeacherManageInterface{  //接口的實現implements
10 private String name,sex;
11 private int age;
12 private double fee,pay;
13 Graduate(){    
14 }
15 Graduate(String name,String sex,int age,double fee,double pay){
16 this.name=name;
17 this.sex=sex;
18 this.age=age;
19 this.fee=fee;
20 this.pay=pay;
21 }
22 public String getName(){
23 return name;
24 }
25 public String getSex(){
26 return sex;
27 }
28 public int getAge(){
29 return age;
30 }
31 public void setFree(double fee){
32 this.fee=fee;
33 }
34 public double getFree(){
35 return fee;
36 }
37 public void setPay(double pay){
38 this.pay=pay;
39 }
40 public double getPay(){        //對接口的抽象方法必須重寫
41 return pay;
42 }    
43 }
44 public class Test3_4 {
45 public static void main(String[] args){
46 Graduate gr=new Graduate("zhangsan","男",25,8000,3000);
47 judgeLoan(gr);
48 }
49 public static void judgeLoan(Graduate gr){  //對象作形參
50 if(gr.getPay()*12-gr.getFree()*2<2000){
51 System.out.println("provide a loan");
52 }
53 else 
54 System.out.println("don't need a loan");
55 }
56 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM