<<--B站..........欢迎来到DGX的博客..........GitHub-->>

我的B站

Java第三次实验(类)

方法1:主类中第一行代码是

Cylinder cylinder=new Cylinder();

package work02;

class Cylinder{
	double r;  //r为圆柱的底面半径
	double h;  //h为圆柱的高	
	void area(){
		double area;
		area = 2*Math.PI*r*r+2*Math.PI*r*h;
		System.out.println("圆柱的面积为:"+area);		
	}
	void volume(){
		double volume;
		volume = Math.PI*r*r*h;
		System.out.println("圆柱的体积为:"+volume);
	}
	
}
public class Leitest {
	public static void main(String args[]){
		Cylinder yuanzhu = new Cylinder();
		yuanzhu.r = 1;
		yuanzhu.h = 2;
		yuanzhu.area();
		yuanzhu.volume();
	}
}

方法2:主类中第一行代码是

Cylinder cylinder=new Cylinder(a,b); //a是输入的参数半径radius,b是输入的高height.

package work02;

class Cylinder02{
	double r;double h;
	Cylinder02(double a, double b){
		r = a;h =b;	
	}
	void area(){
		double area;
		area =2*Math.PI*r*r+2*Math.PI*r*h;
		System.out.println("圆柱的面积为:"+area);
	}
	void volume(){
		double volume;
		volume = Math.PI*r*r*h;
		System.out.println("圆柱的体积为:"+volume);
	}
}

public class Leitest02 {
	public static void main(String args[]){
		Cylinder02 yuanzhu = new Cylinder02(1,2);
		yuanzhu.area();
		yuanzhu.volume();
	}

}

总结比较有用的地方



实现代码:


作者|daiguoxi
时间|2021/3/17
邮箱|daiguoxi@gmail.com
公众号|DGX杂学

posted @ 2021-03-17 16:45  DG息  阅读(107)  评论(0)    收藏  举报