2020.8.14

一、今日学习内容

   1、研究生继承

 1 public class Student2 {
 2     protected int num;
 3     protected String name;
 4     protected char sex;
 5     Student2(){}
 6     public void setValue(int n,String str,char c) {
 7         num=n;
 8         name=str;
 9         sex=c;
10     }
11     public void display() {
12         System.out.println("学号:"+num+"	 姓名:"+name+"	 性别:"+sex);
13     }
14 }
15 public class Postgraduent1 extends Student2 {
16     private String advisor;
17     Postgraduent1(){}
18     public void setAdvisor(String str) {
19         advisor=str;
20     }
21     public static void main(String[] args) {
22         Postgraduent1 xq=new Postgraduent1();
23         xq.setValue(1122, "Xiao Qiang", 'M');
24         xq.setAdvisor("Prof.Zhu");
25         xq.display();
26         System.out.println("Advisor:"+xq.advisor);
27     }
28 }

     

   2、多边形继承

 1 //父类:
 2 public class CPolygon {
 3     protected int width,height;
 4     public void setValues(int a,int b)
 5     {
 6         width=a;
 7         height=b;
 8     }
 9 }
10 //子类1:长方形
11 public class CRectangle extends CPolygon {
12     public int area() {
13         return width*height;
14     }
15 }
16 //子类2:三角形
17 public class CTriangle extends CPolygon {
18     public int area() {
19         return (width*height)/2;
20     }
21 }
22 //实现类:
23 public class main1 {
24     public static void main(String[] args) {
25         CRectangle rect=new CRectangle();
26         CTriangle trgl=new CTriangle();
27         rect.setValues(4, 5);
28         trgl.setValues(4, 5);
29         System.out.println("长方形面积:"+rect.area());
30         System.out.println("三角形面积:"+trgl.area());
31     }
32 }

   

  3、日期时间类

 1 //接口1:日期
 2 public interface Date {
 3     public void setDate(int y,int mo,int d);
 4     public void displayDate();
 5 }
 6 //接口2:时间
 7 public interface Time {
 8     public void setTime(int h,int mi,int s);
 9     public void displayTime();
10 }
11 //实现类:
12 public class Datetime implements Date,Time {
13     protected int year,month,day,hour,minute,second;
14     public void setDate(int y,int mo,int d) {
15         year=y;
16         month=mo;
17         day=d;
18     }
19     public void displayDate() {
20         System.out.println(year+"-"+month+"-"+day);
21     }
22     public void setTime(int h,int mi,int s) {
23         hour=h;
24         minute=mi;
25         second=s;
26     }
27     public void displayTime() {
28         System.out.println(hour+":"+minute+":"+second);
29     }
30     public static void main(String[] args) {
31         Datetime dt=new Datetime();
32         dt.setDate(2020, 5, 20);
33         dt.setTime(20, 13,14);
34         dt.displayDate();
35         dt.displayTime();
36     }
37 }

   

  4、研究生继承2

 1 //父类:学生类
 2 public class Student2 {
 3     protected int num;
 4     protected String name;
 5     protected char sex;
 6     Student2(){}
 7     Student2(int n,String str,char c) {
 8         num=n;
 9         name=str;
10         sex=c;
11     }
12     public void display() {
13         System.out.println("学号:"+num+"	姓名:"+name+"	 性别:"+sex);
14     }
15 }
16 //子类:研究生类
17 public class Postgraduent1 extends Student2 {
18     private String advisor;
19     Postgraduent1(){}
20     Postgraduent1(int n,String s,char c,String str){
21         super(n,s,c);
22         advisor=str;
23     }
24     public static void main(String[] args) {
25         Postgraduent1 xq=new Postgraduent1(10001, "小明", 'M',"Prof.Zhu");
26         xq.display();
27         System.out.println("Advisor:"+xq.advisor);
28     }
29 }

   

二、遇到的问题

    对接口在可以实现多继承方面不太了解,同时对继承的子类的构造函数不会写

三、明日计划

   继续完成相关例题

原文地址:https://www.cnblogs.com/wmdww/p/13503225.html