JAVA学习笔记20210412_对象和类

该博客定义了一个名为Pump的类,用于描述泵的主要属性,包括转速(PumpRev)、压力(OutputPressure)、重量(PumpWeight)、排量(PumpDisplacement)和脉动(PumpPulsation)。类中包含一个构造方法,接收泵的类型作为参数,并提供了设置这些属性值的方法。测试代码创建了两个Pump实例并设置了相应的属性值。
摘要由CSDN通过智能技术生成

创建描述泵的属性(转速,压力,重量,排量,脉动)的类
定义描述泵类型的构造方法,含有唯一参数:泵的类型字符串

/**
* this is a class definition discribing the properties of a pump
* @author XYM_
* @date 2021-4-12
* @version 1.0
*/

import java.io.*
public class Pump{
	
	//PumpRev rotate speed of pump
	int PumpRev;

	//OutputPressure output pressure of pump
	double OutputPressure;

	//PumpWeight weight of pump
	double PumpWeight;

	//PumpDisplacement displacement of pump
	double PumpDisplacement;

	//PumpPulsation pressure pulsation of pump
	double PumpPulsation;
	
	/**
	* this is a constructor of pump
	* @param name type name of pump
	*/
	public Pump(String name){
		// print the name
		System.out.println("Type of pump:" + name);
	}
	
	/**
	* set the value of PumpRev
	* @param n rotate speed of pump
	* @author XYM_
	* @return value of PumpRev
	* @version 1.0
	*/
	public int setN( int n ){
		PumpRev = n;
		// print the rotate speed
		System.out.println("Rotate speed:" + PumpRev + "rpm");
		return PumpRev;
	}

	/**
	* set the value of OutputPressure
	* @param p output pressure of pump
	* @author XYM_
	* @return value of OutputPressure
	* @version 1.0
	*/
	public double setP( double p ){
		OutputPressure = p;
		// print the output pressure
		System.out.println("Output pressure:" + OutputPressure + "MPa");
		return OutputPressure;
	}
	
	/**
	* set the value of PumpWeight
	* @param w weight of pump
	* @author XYM_
	* @return value of PumpWeight
	* @version 1.0
	*/
	public double setW( double w ){
		PumpWeight = w;
		// print the weight
		System.out.println("Weught:" + PumpWeight + "kg");
		return PumpWeight;
	}
	
	/**
	* set the value of PumpDisplacement
	* @param q displacement of pump
	* @author XYM_
	* @return value of PumpDisplacement
	* @version 1.0
	*/
	public double setQ( double q ){
		PumpDisplacement = q;
		// print the displacement
		System.out.println("Displacement:" + PumpDisplacement + "ml/rev");
		return PumpDisplacement;
	}
	
	/**
	* set the value of PumpPulsation
	* @param tau pressure pulsation of pump
	* @author XYM_
	* @return value of PumpPulsation
	* @version 1.0
	*/
	public double setTau( double tau ){
		PumpPulsation = tau;
		// print the pressure pulsation
		System.out.println("Pulsation:" + PumpPulsation + "%");
		return PumpPulsation;
	}
}

测试代码为:

/**
* this is a test class
* @author XYM_
* @date 2021-4-12
* @version 1.0
*/

import java.io.*;
public class PumpTest{

	public static void main(String[] args){
	
		Pump pump01 = new Pump("swashplate pump");
		pump01.setN(4200);
		pump01.setP(21.5d);
		pump01.setW(9.8d);
		pump01.setQ(19.5d);
		pump01.setTau(4d);
		
		Pump pump02 = new Pump("swashplate pump");	
		pump02.setN(4000);
		pump02.setP(28d);
		pump02.setW(15.3d);
		pump02.setQ(75d);
		pump02.setTau(4d);
			
	}
}

运行结果为:

Type of pump:swashplate pump
Rotate speed:4200rpm
Output pressure:21.5MPa
Weught:9.8kg
Displacement:19.5ml/rev
Pulsation:4.0%

Type of pump:swashplate pump
Rotate speed:4000rpm
Output pressure:28.0MPa
Weught:15.3kg
Displacement:75.0ml/rev
Pulsation:4.0%
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值