实验七:继承与多态练习
1.本次实验的目的和要求
1)练习面向对象编程技术中的继承和多态编程技术。
2)练习extends和protect修饰符的使用
3)练习super的使用
4)理解Java多态的特点和应用环境。
2.实验内容或原理
设计一个抽象类Shape(图形)包含求面积和周长的area()方法和perimeter()方法以及设置颜色的方法SetColor(),并利用Java多态技术设计其子类Circle(圆形)类和Triangle(三角形)类,并分别实现相应的求面积和求周长的方法。
public class Test
{
public static void main(String[] args) {
Circle circle = new
Circle(2);
circle.setColor("yellow");
System.out.println("A circle
created on: " + circle.getDate()
+ "\nCircle color: " + circle.getColor());
System.out.println("Circle
area is: " + circle.area() +
"\nCircle perimeter is: " + circle.perimeter());
System.out.println("\n");
Triangel tri = new
Triangel(3,4,
本文通过一个Java实验展示了继承与多态的概念,设计了抽象类Shape及其子类Circle和Triangle,实现了求面积和周长的方法。通过创建对象并调用相应方法,演示了如何运用继承、多态特性和protected修饰符。
最低0.47元/天 解锁文章

3458

被折叠的 条评论
为什么被折叠?



