java任务--Volume

– 定义一个圆类(Circle),其所在的包为bzu.info.software;定义一个圆柱类Cylinder,其所在的包为bzu.info.com;定义一个主类A,其所在的包也为bzu.info.com,在A中生成一个Cylinder对象,并输出其体积。编译并运行该类

– 试着改变求体积方法的访问权限,查看并分析编译和运行结果

– 把Cylinder类和A类置于不同的包中,通过对求体积方法设置不同的访问权限,查看并分析编译和运行结果

Circle类:

package bzu.info.software;

public class Circle {
	int radius;
	double s;
	public Circle(int r){
		radius=r;
	}
	public void setRadius(int r){
		radius=r;   //用于对已有对象改变变量
	}
	 public int getRadius(){
		return radius;   //用于得到已有对象变量
	}
	 public double Sarea(){   //定义一个方法求圆的面积
		s=3.14*radius*radius;
		return s;
		
	}

}
Cylinder类:

package bzu.info.com;

import bzu.info.software.Circle;

public class Cylinder {
	double height;
	Circle bottom;
	Cylinder(int h,Circle c){
		height=h;
		bottom=c;  //地址的传递
	}
	void setBottomRadius(int r){
		bottom.setRadius(r);
	}
	int getBottomRadius(){
		return bottom.getRadius();                            
	}
	double Volume(){    //定义一个方法求圆柱体的体积
		return bottom.Sarea()*height;   //调用圆的Sarea()方法与高相乘并返回
		
	}

}
主类A:

package bzu.info.com;

import bzu.info.software.Circle;

public class Cylinder {
	double height;
	Circle bottom;
	Cylinder(int h,Circle c){
		height=h;
		bottom=c;//地址的传递
	}
	void setBottomRadius(int r){  //用于修改圆柱体的底面半径
		bottom.setRadius(r);   //调用圆类的set方法修改圆的半径赋给圆柱体的底面半径
	}
	int getBottomRadius(){   //用于得到圆柱体的底面半径
		return bottom.getRadius();   //调用圆类的get方法得到圆的半径并返回                          
	}
	double Volume(){   //定义一个方法求圆柱体的体积
		return bottom.Sarea()*height;  //调用圆的Sarea()方法与高相乘并返回
		
	}

}
测试结果:





程序分析:用到很多的set方法和get方法用于方便修改已有对象的变量并得到,关于访问权限的问题,如果在另外一个包中的Circle类不加public,则另外包的Cylinder类和主类A将没办法使用Circle类中的方法,要想使用必须导入import那个包,如果将某类中的变量改成private,则变成私有,意思是只能在本类内部使用,而默认的是default,是包权限,在包内可以任意使用。

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Core Java Volume I–Fundamentals, 1 (11th Edition) By 作者: Cay S. Horstmann ISBN-10 书号: 0135166306 ISBN-13 书号: 9780135166307 Edition 版本: 11 出版日期: 2018-09-06 pages 页数: 928 The #1 Java Guide for Serious Programmers: Fully Updated for Java SE 9, 10 & 11 For serious programmers, Core Java, Volume I—Fundamentals, Eleventh Edition, is the definitive guide to writing robust, maintainable code. Whether you’re using Java SE 9, 10, or 11, it will help you achieve a deep and practical understanding of the language and API, and its hundreds of realistic examples reveal the most powerful and effective ways to get the job done. Cay Horstmann’s updated examples reflect Java’s long-awaited modularization, showing how to write code that’s easier to manage and evolve. You’ll learn how to use JShell’s new Read-Eval-Print Loop (REPL) for more rapid and exploratory development, and apply key improvements to the Process API, contended locking, logging, and compilation. In this first of two volumes, Horstmann offers in-depth coverage of fundamental Java and UI programming, including objects, generics, collections, lambda expressions, Swing design, concurrency, and functional programming. If you’re an experienced programmer moving to Java SE 9, 10, or 11, there’s no better source for expert insight, solutions, and code. Master foundational techniques, idioms, and best practices for writing superior Java code Leverage the power of interfaces, lambda expressions, and inner classes Harden programs through effective exception handling and debugging Write safer, more reusable code with generic programming Improve performance and efficiency with Java’s standard collections Build cross-platform GUIs with the Swing toolkit Fully utilize multicore processors with Java’s improved concurrency

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值