Java程序设计:引用类型参数传值及对象的组合

目录

1 实验名称

2 实验目的

3 实验源代码

4 实验运行结果图

5 实验总结


1 实验名称

       引用类型参数传值及对象的组合

2 实验目的

       掌握引用类型的参数传值的过程及内存中的变化情况,理解对象的组合,熟练代码调试。

3 实验源代码

Application.java(源文件名字)

package lxd.iot.cn.edu.nuc;

import lxd.iot.cn.edu.nuc.*;//*号是通配符

public class Application {
  //  public static void main(String[] args) {
        //创建梯形的对象
//          Ladder ladder = new Ladder();//创建扫描器对象
//        Scanner scanner = new Scanner(System.in);
//        System.out.println("请输入梯形的上底,输完回车");
//       ladder.upperBase = scanner.nextDouble();
//        ladder.setUpperBase(scanner.nextDouble());
//        System.out.println("请输入梯形的下底,输完回车");
//        ladder.setBottom(scanner.nextDouble());
//        System.out.println("请输入梯形的高,输完回车");
//        ladder.setHeight(scanner.nextDouble());
//        System.out.println("梯形的面积是:"+ladder.getArea());
//
//
//        Ladder ladder1 = new Ladder(4,5,8);
//        System.out.println("ladder的面积是:"+ladder1.getArea());
//
//
//        ladder1 = ladder;
//        System.out.println("lader1的面积是:"+ladder1.getArea());






  //  }

    public static void main(String[] args) {
        Cylinder cylinder = new Cylinder();//创建圆柱对象
        cylinder.setHeight(10);

        Circle bottom = new Circle(2);//创建底圆对象
        cylinder.setBottom(bottom);//将bottom对象设置为圆柱对象的底圆

        double volume = cylinder.getVolume();
        System.out.println("圆柱的体积是:"+volume);

//        Circle bottom1 = new Circle(3.0);
//        cylinder.setBottom(bottom1);
//        System.out.println("圆柱的体积是:"+cylinder);


        CircularCone circularCone = new CircularCone();
        circularCone.setHeight(10);

        circularCone.setBottom(bottom);
        System.out.println("圆锥的体积是:"+circularCone.getVolume());


    }


}

Ladder.java

ublic class Ladder {
    //定义梯形的基本属性--成员变量
    double upperBase;//上底
    double bottom;//下底
    static double height = 6.0;//高--静态类成员变量

    //定义无参构造方法
    Ladder(){

    }


    //定义带参数的构造方法

    Ladder(double upperBase,double bottom,double height){
        this.upperBase = upperBase;
        this.bottom = bottom;
        this.height = height;
    }

    public double getUpperBase() {
        return upperBase;
    }

    public void setUpperBase(double upperBase) {
        this.upperBase = upperBase;
    }

    public double getBottom() {
        return bottom;
    }

    public void setBottom(double bottom) {
        this.bottom = bottom;
    }

    public double getHeight() {
        return height;
    }

//    public static void setHeight(double height) {//定义静态类方法
//        static.height = height;
//    }

    //定义梯形的行为(功能)
    //求梯形的面积
    public double getArea(){
        double area;//局部变量
        area=(upperBase+bottom)*height/2;
        return area;
    }

}

Cylinder.java

package lxd.iot.cn.edu.nuc;

import lxd.iot.cn.edu.nuc.Circle;

public class Cylinder {
    double height;
    Circle bottom;//底圆

    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;
    }

    public Circle getBottom() {
        return bottom;
    }

    public void setBottom(Circle bottom) {
        this.bottom = bottom;
    }

    public double getVolume(){
       return bottom.getArea()*height;
    }
}

Circle.java

package lxd.iot.cn.edu.nuc;

public class Circle {
    double radius;
    public Circle(){

    }
    public Circle(double radius){
        this.radius = radius;
    }

    public double getRadius() {
        return radius;
    }

    public void setRadius(double radius) {
        this.radius = radius;
    }
    public double getArea(){
        return Math.PI*radius*radius;
    }

    public double getGirth(){
        return 2*Math.PI*radius;
    }
}

CircularCone.java

package lxd.iot.cn.edu.nuc;

public class CircularCone {
    public double height;
    private Circle bottom;

    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;
    }

    public Circle getBottom() {
        return bottom;
    }

    public void setBottom(Circle bottom) {
        this.bottom = bottom;
    }
    public double getVolume(){
        return bottom.getArea()*height/3;
    }
}

4 实验运行结果图

5 实验总结

在这次实验中,通过以下步骤完成了任务:
   (1)创建对象:首先需要创建一个对象,这可以通过new操作符和构造函数来实现。在本次实验中,创建了不同的对象,并使用构造函数为其赋值。
   (2)引用类型参数传值:Java中,对象作为方法的参数时,传递的是对象的引用而不是副本。因此,在方法内部修改对象数据会影响到原始对象。在本次实验中,定义了一个方法,将创建的对象作为参数传递给该方法,并在方法内部对对象进行了修改。
   (3)对象的组合方式。Java中,可以将对象作为成员变量嵌套在另一个类中,形成对象的组合。在本次实验中,创建了两个类,并在其中一个类中嵌套了另一个类的对象。

  • 13
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

茜茜西西CeCe

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值