继承作业

案列分析01

 1 package com.test02;
 2 
 3 public class Exercise01 {
 4     public static void main(String[] args) {
 5         B b = new B();
 6     }
 7 }
 8 //A 类
 9 class A{
10     public  A(){
11         System.out.println("a");//第一输出的结果
12     }
13     public  A(String name){
14         System.out.println("a name");
15     }
16 }
17 //创建B类
18 class  B extends A{
19     //无参构造器
20     public  B(){//这个无参构造器中第一行是this,所有没有super
21         this("abc");
22         System.out.println("b");//第三个输出结果
23     }
24     public  B(String name){//在这个方法体内有super()
25         //super();默认时隐藏的,所指的是父类A中的无参构造器
26         System.out.println("b name");//第二个输出结果
27     }
28 }

案列分析02:

 1 package com.use;
 2 public class Exercise02 {
 3     public static void main(String[] args) {
 4         //main方法中的调用对象
 5         C c = new C();
 6         /**
 7          * 输出的结果分别是
 8          * 1."我是A类"
 9          * 2.我是B类的有参构造器
10          * 3."我是C类有参构造"
11          * 4."我是C类无参构造"
12          */
13     }
14 }
15 class  A{
16     public A(){//这个无参构造里面也隐含了super
17         //super();指代父类(Object)中的无参构造
18         System.out.println("我是A类");//第一个输出的结果
19     }
20 }
21 class B extends A{
22     public B(){
23         System.out.println("我是B类的无参构造器");
24     }
25     public B(String name){//隐含了super
26         //super();这个super指代的是父类(A)中的无参构造
27         System.out.println("我是B类的有参构造器");//第二个输出的结果
28     }
29 }
30 class C extends B{
31     public C(){
32       this("hello");//这个this指代的是c类中的有参构造
33         System.out.println("我是C类无参构造");//第四个输出的结果
34     }
35     public C (String name){
36         super("hahaha");//super指代的是父类(B)中的有参构造
37         System.out.println("我是C类有参构造");//第三个输出的结果
38     }
39 }

案例3 ExtendsExercise03.java

编写Computer类,包含CPU、内存、硬盘等属性,getDetails方法用于返回
Computer的详细信息
编写PC子类,继承Computer类,添加特有属性【品牌brand】
编写NotePad子类,继承Computer类,添加特有属性【演示color】
编写Test类,在main方法中创建PC和NotePad对象,分别给对象中特有的属性
赋值,以及从Computer类继承的属性赋值,并使用方法并打印输出信息。

 1 package com.use;
 2 public class ExtendsExercise03 {
 3     public static void main(String[] args) {
 4         PC pc = new PC("AMD",32,1024,"IBM");
 5         pc.printInfo();
 6 
 7         NotePad notePad = new NotePad("Inter",16,512,"白色");
 8         notePad.printInfo();
 9     }
10 }
11 /**
12  * 编写Computer类,包含CPU、内存、硬盘等属性,getDetails方法用于返回
13  * Computer的详细信息
14  * 编写PC子类,继承Computer类,添加特有属性【品牌brand】
15  * 编写NotePad子类,继承Computer类,添加特有属性【演示color】
16  * 编写Test类,在main方法中创建PC和NotePad对象,分别给对象中特有的属性
17  * 赋值,以及从Computer类继承的属性赋值,并使用方法并打印输出信息。
18  */
19 class Computer{
20     private  String cpu;
21     private int Memory;
22     private  int disk;
23 //创建形式参数构造器
24     public Computer(String cpu, int memory, int disk) {
25         this.cpu = cpu;
26         Memory = memory;
27         this.disk = disk;
28     }
29 
30     // getDetails方法,用于返回Computer的详细信息
31     public String getDetails(){
32         return "cpu="+cpu+"\t"+"Memory="+Memory+"\t"+"disk="+disk;
33     }
34 
35     public String getCpu() {
36         return cpu;
37     }
38     public void setCpu(String cpu) {
39         this.cpu = cpu;
40     }
41     public int getMemory() {
42         return Memory;
43     }
44     public void setMemory(int memory) {
45         Memory = memory;
46     }
47     public int getDisk() {
48         return disk;
49     }
50     public void setDisk(int disk) {
51         this.disk = disk;
52     }
53 }
54 
55 
56 //编写PC子类,继承Computer类,添加特有属性【品牌brand】
57 class PC extends Computer{
58     private  String brand;
59      //根据继承规则,这个形参构造器直接去调用父类的形参构造器
60     public PC(String cpu, int memory, int disk, String brand) {
61         super(cpu, memory, disk);
62         this.brand = brand;
63     }
64 //设置特有属性
65     public String getBrand() {
66         return brand;
67     }
68     public void setBrand(String brand) {
69         this.brand = brand;
70     }
71     //调用父类中的getDetails()方法,得到相关属性
72     public void printInfo(){
73         System.out.println(getDetails()+"\t"+"brand="+brand);
74     }
75 }
76 //创建类,添加特有颜色的属性
77 class  NotePad extends  Computer{
78     private String color;
79 
80     public NotePad(String cpu, int memory, int disk, String color) {
81         super(cpu, memory, disk);
82         this.color = color;
83     }
84     //调用方法用于打印
85     public void printInfo(){
86         System.out.println(getDetails()+"\t"+"color="+color);
87     }
88 }

 

 

posted @ 2022-03-06 16:55  捞月亮的渔夫  阅读(76)  评论(0)    收藏  举报