Java学习笔记---类、构造函数的创建,以及类的调用练习

总结:
1、每一个Java文件只能有一个公共类
2、默认访问属性的类只能被本包的成员访问
3、类的三大特性:封装性、继承性、多态性
4、Java的访问权限有4种,修饰符分别为:public、private、protected、不带访问权限修饰符。
      (1)public:访问限制最宽的修饰符,被其修饰的类、属性、方法可以被跨类访问以及跨包访问
      (2)默认访问权限:即不带访问权限的修饰符,默认访问权限只允许同一个包中进行访问
      (3)protected:被其修饰的类、属性、方法只能被类本身的方法以及子类访问。注:即使子类在不同的包中也可以访问
      (4)private:访问限制最窄的修饰符,被其修饰的类、属性、方法只能被该类的对象访问,其子类不能访问,也不能跨包访问

练习:

package mypackage01;

import java.util.Scanner;

public class mydemo
{

	public static void main(String[] args)
	{
		System.out.print("——————执行程序——————\n");
		Scanner scanner = new Scanner(System.in);
		while (true)
		{
			System.out.print("——功能介绍——\n");
			System.out.print("1.长方体的计算功能\t\t2.圆柱体的计算功能\t\t3.球体的计算功能\n");
			System.out.print("使用功能请输入对应的数字编号:\t\t结束程序请输入:0\n");
			final int choice = scanner.nextInt();
			if (choice == 1)
			{
				cuboid();
				continue;
			}
			if (choice == 2)
			{
				cylinder();
				continue;
			}
			if (choice == 3)
			{
				sphere();
				continue;
			}
			if (choice == 0)
			{
				break;
			}
			else
			{
				System.out.print("!!!您的输入有误请重新输入!!!\n");
				continue;
			}
		}
		scanner.close();
		System.out.print("——————程序结束——————\n");
	}
	private static void cuboid()
	{
		System.out.print("——已进入长方体的计算功能——\n");
		Scanner scanner = new Scanner(System.in);
		System.out.print("请输入长方体的长:\n");
		final double l = scanner.nextDouble();
		System.out.print("请输入长方体的宽:\n");
		final double w = scanner.nextDouble();
		System.out.print("请输入长方体的高:\n");
		final double h = scanner.nextDouble();
		System.out.print("计算长方体的表面积请输入:1\t\t计算长方体的体积请输入:2\n");
		final int choice = scanner.nextInt();
		if (choice == 1)
		{
			System.out.printf("长方体表面积为: %.2f\n", Stero.cuboidArea(l, w, h));
		}
		if (choice == 2)
		{
			System.out.printf("长方体体积为: %.2f\n", Stero.cuboidVolume(l, w, h));
		}
		else
		{
			System.out.print("!!!您的输入有误!!!\n");
		}
	}
	private static void cylinder()
	{
		System.out.print("——已进入圆柱体的计算功能——\n");
		Scanner scanner = new Scanner(System.in);
		System.out.print("请输入圆柱体的底面半径:\n");
		final double r = scanner.nextDouble();
		System.out.print("请输入圆柱体的高:\n");
		final double h = scanner.nextDouble();
		System.out.print("计算圆柱体的表面积请输入:1\t\t计算圆柱体的体积请输入:2\n");
		final int choice = scanner.nextInt();
		if (choice == 1)
		{
			System.out.printf("圆柱体表面积为: %.2f\n", Stero.cylinderArea(r, h));
		}
		if (choice == 2)
		{
			System.out.printf("圆柱体体积为: %.2f\n", Stero.cylinderVolume(r, h));
		}
		else
		{
			System.out.print("!!!您的输入有误!!!\n");
		}
	}
	private static void sphere()
	{
		System.out.print("——已进入球体的计算功能——\n");
		Scanner scanner = new Scanner(System.in);
		System.out.print("请输入球体的半径:\n");
		final double r = scanner.nextDouble();
		System.out.print("计算球体的表面积请输入:1\t\t计算球体的体积请输入:2\n");
		final int choice = scanner.nextInt();
		if (choice == 1)
		{
			System.out.printf("圆柱体表面积为: %.2f\n", Stero.sphereArea(r));
		}
		if (choice == 2)
		{
			System.out.printf("圆柱体体积为: %.2f\n", Stero.sphereVolume(r));
		}
		else
		{
			System.out.print("!!!您的输入有误!!!\n");
		}
	}
}
class Stero
{
	//定义长方体表面积的计算方法
	public  static double cuboidArea(double l, double w, double h)
	{
		System.out.print("!!!调用了长方体表面积的计算方法!!!\n");
		if (l > 0 && w > 0 && h > 0)
		{
			Cuboid cuboid = new Cuboid(l, w, h);
			return cuboid.area();
		}
		else
		{
			return -1.0;
		}
	}
	//定义长方体体积的计算方法
	public static double cuboidVolume(double l, double w, double h)
	{
		System.out.print("!!!调用了长方体体积的计算方法!!!\n");
		if (l > 0 && w > 0 && h > 0)
		{
			Cuboid cuboid = new Cuboid(l, w, h);
			return cuboid.volume();
		}
		else
		{
			return -1.0;
		}
	}
	//定义圆柱体表面积的计算方法
	public static double cylinderArea(double r, double h)
	{
		System.out.print("!!!调用了圆柱体表面积的计算方法!!!\n");
		if (r > 0 && h > 0)
		{
			Cylinder cylinder = new Cylinder(r, h);
			return cylinder.area();
		}
		else
		{
			return -1.0;
		}
	}
	//定义圆柱体体积的计算方法
	public static double cylinderVolume(double r, double h)
	{
		System.out.print("!!!调用了圆柱体体积的计算方法!!!\n");
		if (r > 0 && h > 0)
		{
			Cylinder cylinder = new Cylinder(r, h);
			return cylinder.volume();
		}
		else
		{
			return -1.0;
		}
	}
	//定义球体表面积的计算方法
	public static double sphereArea(double r)
	{
		System.out.print("!!!调用了球体表面积的计算方法!!!\n");
		if (r > 0)
		{
			Sphere sphere = new Sphere(r);
			return sphere.area();
		}
		else
		{
			return -1.0;
		}
	}
	//定义球体体积的计算方法
	public static double sphereVolume(double r)
	{
		System.out.print("!!!调用了球体体积的计算方法!!!\n");
		if (r > 0)
		{
			Sphere sphere = new Sphere(r);
			return sphere.volume();
		}
		else
		{
			return -1.0;
		}
	}
}
class Cuboid extends Stero
{
	private double length;//定义长方体的长
	private double width;//定义长方体的宽
	private double high;//定义长方体的高
	//构造一个长宽高都是1.0的长方体
	public Cuboid()
	{
		this.setLength(1.0);
		this.setWidth(1.0);
		this.setHigh(1.0);
	}
	//构造一个有长宽高参数的长方体
	public Cuboid(double length, double width, double high)
	{
		this.setLength(length);
		this.setWidth(width);
		this.setHigh(high);
	}
	//获取长方体的长
	public double getLength()
	{
		return length;
	}
	//获取长方体的宽
	public double getWidth()
	{
		return width;
	}
	//获取长方体的高
	public double getHigh()
	{
		return high;
	}
	//定义长方体的长
	public void setLength(double length)
	{
		this.length = length;
	}
	//定义长方体的宽
	public void setWidth(double width)
	{
		this.width = width;
	}
	//定义长方体的高
	public void setHigh(double high)
	{
		this.high = high;
	}
	//定义长方体表面积的计算方法
	public double area()
	{
		return 2 * (this.length * this.width +this.width * this.high + this.high * this.length);
	}
	//定义长方体体积的计算方法
	public double volume()
	{
		return this.length * this.width * this.high;
	}
}
class Cylinder extends Stero
{
	private double radius;
	private double high;
	//构造一个地面半径和高都是1.0的圆柱体
	public Cylinder()
	{
		this.radius = 1.0;
		this.high = 1.0;
	}
	//构造一个底面半径和高都有参数的圆柱体
	public Cylinder(double radius, double high)
	{
		this.radius = radius;
		this.high = high;
	}
	//获取圆柱体的底面半径
	public double getRadius()
	{
		return this.radius;
	}
	//获取圆柱体的高
	public double getHigh()
	{
		return this.high;
	}
	//定义圆柱体的底面半径
	public void setRadius(double radius)
	{
		this.radius = radius;
	}
	//定义圆柱体的高
	public void setHigh(double high)
	{
		this.high = high;
	}
	//定义圆柱体表面积的计算方法
	public double area()
	{
		return 2 * Math.PI * Math.pow(this.radius, 2) + 2 * Math.PI * this.radius * this.high;
	}
	//定义圆柱体体积的计算方法
	public double volume()
	{
		return Math.PI * Math.pow(this.radius, 2) * this.high;
	}
}
class Sphere extends Stero
{
	private double radius;
	//构造一个半径为1.0的球体
	public Sphere()
	{
		this.radius = 1.0;
	}
	//构造一个半径为带参的球体
	public Sphere(double radius)
	{
		this.radius = radius;
	}
	//获取球体的半径
	public double getRadius()
	{
		return this.radius;
	}
	//定义球体的半径
	public void setRadius(double radius)
	{
		this.radius = radius;
	}
	//定义球体表面积的计算方法
	public double area()
	{
		return 4 * Math.PI * Math.pow(this.radius, 2);
	}
	//定义球体体积的计算方法
	public double volume()
	{
		return (4 * Math.PI * Math.pow(this.radius, 3))/3;
	}
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值