• 第六周&java实验报告四


    Java实验报告四

     

    实验目的:

    (1)掌握类的继承方法;
    (2)变量的继承和覆盖,方法的继示、重载和覆盖实现;

    实验内容

    (1)根据下面的要求实现圆类Circle.
    ①圆类Cirle的成员变量: radius 表示圆的半径。
    ②圆类Cirdle的方法成员:
    Circle () :构造方法,将半径置0
    Circle (doubler) :构造方法:
    创建Circle对象时将半径初始化为r
    double getRadius () :获得圆的半径值
    double getPerimeter () :获得圆的周长
    void disp O :将圆的半径、圆的周长和圆的面积输出到屏幕
    (2)维承第(1)题中的圆Circle类,派生圆柱体类Cylinder。要求如下:
    ①圆柱体类Cylinder的成员变量: height 表示圆柱体的高。
    ②圆柱体类Cylinder的成员方法:Cylinder (doubler, double h)构造方法,创建Cylinder对象时将圆半径初始化为r,圆柱高初始化为h

     

    实验源码;

    (1)

     1 import java.util.*;
     2 class Circle{
     3     private double radius;
     4     public  Circle(double r) {
     5         setRadius(r);
     6     }
     7     public double getPerimeter() {
     8         return (2*Math.PI*getRadius());
     9     }
    10     public void disp() {
    11         System.out.println("圆的半径:"+radius+" 圆的周长:"+getPerimeter()+" 圆的面积:"+2*(Math.PI*Math.pow(radius, 2)));
    12     }
    13     public double getRadius() {
    14         return radius;
    15     }
    16     public void setRadius(double radius) {
    17         this.radius = radius;
    18     }
    19     
    20 }
    21 public class Text1{
    22     public static void main(String[] args){
    23         Circle cr=new Circle(3);
    24         cr.disp();
    25     }
    26 }

    实验结果:

    实验源码:

    (2),(3)

     1 import java.util.*;
     2 class Circle{
     3     private double radius;
     4     public Circle() {
     5         
     6     }
     7     public  Circle(double r) {
     8         setRadius(r);
     9     }
    10     public double getPerimeter() {
    11         return (2*Math.PI*getRadius());
    12     }
    13     public void disp() {
    14         System.out.println("圆的半径:"+radius+" 圆的周长:"+getPerimeter()+" 圆的面积:"+2*(Math.PI*Math.pow(radius, 2)));
    15     }
    16     public double getRadius() {
    17         return radius;
    18     }
    19     public void setRadius(double radius) {
    20         this.radius = radius;
    21     }
    22     
    23 }
    24 class Cylinder extends Circle{
    25     private double height;
    26     
    27     public Cylinder(double r,double h){
    28         setRadius(r);
    29         setHeight(h);
    30     }
    31     public double getVol() {
    32         return 2*(Math.PI*Math.pow(getRadius(), 2))*height;
    33     }
    34     public void dispVol() {
    35         System.out.println("圆柱体的体积:"+getVol());
    36     }
    37     public double getHeight() {
    38         return height;
    39     }
    40     public void setHeight(double height) {
    41         this.height = height;
    42     }
    43     
    44 }
    45 public class Text1{
    46     public static void main(String[] args){
    47         Cylinder cr=new Cylinder(3,2);
    48         cr.disp();
    49         cr.dispVol();
    50     }
    51 }

     实验结果:

    这次实验难度不高,都是上课讲过的知识点,但我在实现Cylinder类时出现了一个问题:我在声明Cylinder的构造方法时系统报错了,如下:

    我在向括号中添加两个参数后报错依旧没有停止,如下:

     

    最后发现是在Cricle中没有声明无参的构造方法(因为是问题解决后截的图,所以用注释的方式将其消除了)。

    第六周学习总结

    主要学习了接口的相关知识:

    接口是一种特殊的类,里面由全局常量和公共的抽象方法组成。

    (1)接口的定义格式:

    1 Interface 接口名称{
    2 
    3        全局变量:
    4 
    5        抽象方法;
    6 
    7 }

    (2)实现接口

    接口的使用必须通过子类(或称为实现类,以下称为实现类),子类通过implements关键字实现接口。

    1 class 实现类implements接口A,接口B,…… {
    2 
    3 }

    一个实现类可以同时实现多个接口,同时实现类也必须覆写完两个两个接口中的所有抽象方法。接口可以继承多个接口,但接口不能被多个实现类继承,接口也不能继承抽象类。

  • 相关阅读:
    Java实现“睡排序”——线程池Executors的使用
    浅谈HashMap与线程安全 (JDK1.8)
    Ubuntu 16 Java Develop环境快速搭建
    Spring Boot在反序列化过程中:jackson.databind.exc.InvalidDefinitionException cannot deserialize from Object value
    Java 8 – Map排序
    vue指令优化网络图片加载速度
    如何实现小于12px的字体效果
    两种以上方式实现已知或者未知宽度的垂直水平居中
    C# winform窗体间传值(使用委托或事件)
    C#栈Stack的使用
  • 原文地址:https://www.cnblogs.com/noacgnnolife/p/11625068.html
Copyright © 2020-2023  润新知