Java实验报告(四)|继承和多态

题目一

(一)程序一
源程序:

public class Leaf {
    int i = 0;
    Leaf increment(){
        i++;
        return this;
    }
    void print(){
        System.out.println("i="+i);
    }
    public static void main(String[] args) {
        Leaf x= new Leaf();
        x.increment().increment().increment().print();
    }
}

运行结果截图:
在这里插入图片描述

题目二

(二)程序二
源程序:

public class ThisTest1 {
    int x;
    public ThisTest1(int x) {
        this.x = x + 1;
    }
    public static void main(String[] args) {
        ThisTest1 t = new ThisTest1(5);
        System.out.println(t.x);
    }
}

运行结果截图:
在这里插入图片描述

题目三

(三)程序三

public class ThisTest2 {
    int x, y;
    public ThisTest2(int x, int y) {
        this.x = x;
        this.y = y;
    }
    public ThisTest2(int x) {
        this(x, 3);
    }
    public static void main(String[] args) {
        ThisTest2 t = new ThisTest2(1);
        System.out.println("t.x=" + t.x);
        System.out.println("t.y=" + t.y);
    }
}

运行结果截图:
在这里插入图片描述

题目四

(四)程序四
源程序:

class SuperClass1 {
    int a = 3;
    void f() {
        a += 1;
    }
}
public class SubClass1 extends SuperClass1 {
    int a = 6;
    void f() {
        super.f();
        a = a + super.a - 3;
    }
    public static void main(String[] args) {
        SubClass1 my = new SubClass1();
        my.f();
        System.out.println("a=" + my.a);
    }
}

运行结果截图:
在这里插入图片描述

题目五

(五)程序五

class SuperClass2 {
    private int i;
    public SuperClass2(int x) {
        i = x + 1;
    }
    public int f() {
        return i;
    }
}
public class SubClass2 extends SuperClass2 {
    public SubClass2(int m) {
        super(m);
    }
    public static void main(String[] args) {
        SubClass2 t = new SubClass2(9);
        System.out.println(t.f());
    }
}

运行结果截图:
在这里插入图片描述

题目六

(六)程序六
源程序:

class ElectricalEquipement {
    ElectricalEquipement() {
        System.out.println("ElectricalEquipement()");
    }
}
class Computer extends ElectricalEquipement {
    Computer() {
        System.out.println("Computer()");
    }
}
public class PortableComputer extends Computer {
    PortableComputer() {
        System.out.println("PortableComputer()");
    }
    public static void main(String[] args) {
        PortableComputer myPc = new PortableComputer();
    }
}

运行结果截图:
在这里插入图片描述

题目七

(七)程序七
源程序:

class Shape {
    void print() {
        System.out.println("Shape");
    }
}
class Circle extends Shape {
    void print() {
        System.out.println("Circle");
    }
}
class Rectangle extends Shape {
    void print() {
        System.out.println("Rectangle");
    }
}
class Square extends Shape {
    void print() {
        System.out.println("Square");
    }
}
public class DrawShape {
    static void draw(Shape s) {
        s.print();
    }
    public static void main(String[] args) {
        Shape s = new Shape();
        draw(s);
        s = new Circle();
        draw(s);
        s = new Rectangle();
        draw(s);
        s = new Square();
        draw(s);
    }
}

运行结果截图:
在这里插入图片描述

实验小结:
(一)this的用法总结:
1、this是一个关键字,是一个引用,保存内存地址指向自身。
2、this只能使用在实例方法中。谁调用这个实例方法,this就是谁。
3、this出现在实例方法中其实代表的是当前对象。
4、this不能使用在静态方法中。
5、this.大部分情况下是可以省略的,但是用来区分局部变量和实例变量的时候不能省略。
6、this() 这种语法只能出现在构造方法第一行,表示当前构造方法调用本类其他的构造方法,目的是代码复用。
(二)super的用法总结
1、super只能出现在实例方法和构造方法中。
2、super不能出现在静态方法中。
3、super.大部分情况下可省略。
4、父类和子类中有同名的属性,或者说有同样的方法,若想在子类中访问父类的,super.不能省略。
5、super()同this()一样只能出现在构造方法的第一行,通过当前的构造方法去调用“父类中的构造方法”。
(三)继承的好处
子类继承父类,可以实现代码的复用,降低代码的冗余度。
(三)多态的好处
降低程序的耦合度,提高程序的扩展力。
(四)写出你在此次试验中遇到的问题、解决方法及你的收获。
问题:多态向上/向下转型失败
解决:多态转型的时候,并非都可以进行向上转型,有时候需要用instanceof进行判断,解决转型失败的问题。

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值