测试java实现继承接口案例

‍定义一个抽象类Person,其中有一个公共的抽象方法showInfo()。然后定义此抽象类的一个子类Student,包括的成员变量有姓名、学号、班级和年龄,且此类中包括两个构造方法。

首先定义一个Student类,用来时实现Student中的一些成员属性,在Student类定义一个方法,用来输出成员变量中的一些信息,代码如下所示:

class Student {
    String stuName;
    String stuID;
    String stuClass;
    int age;

    public Student(String stuName, String stuID) {
        this.stuName = stuName;
        this.stuID = stuID;
    }

    public Student(String stuName, String stuID, String stuClass, int age) {
        this.stuName = stuName;
        this.stuID = stuID;
        this.stuClass = stuClass;
        this.age = age;
    }

    public void showInfo() {
        System.out.println("姓名:" + stuName + "\t" + "学号:" + stuID + "\t" + "班级:" + stuClass);
    }
}

编写一个测试类,用来测试代码编写是否可以正常运行,通过测试类来测试输出学生的一些信息,如下图代码所示:

abstract class Persion {
    public static void main(String args[]) {
        Student student = new Student("张三", "2022140267", "计算机科学与技术", 18);
        student.showInfo();
    }
}

运行结果:

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值