2020年8月17日Java学习日记

学习内容

 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+"\t 姓名:"+name+"\t 性别:"+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.研究生继承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+"\t姓名:"+name+"\t 性别:"+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 }
复制代码
复制代码

   

posted @ 2020-08-17 00:42  Zwyooo  阅读(57)  评论(0编辑  收藏  举报