java练习册(010)--类(2)

上传人:飞*** 文档编号:44969203 上传时间:2018-06-14 格式:DOC 页数:2 大小:49.50KB
返回 下载 相关 举报
java练习册(010)--类(2)_第1页
第1页 / 共2页
java练习册(010)--类(2)_第2页
第2页 / 共2页
亲,该文档总共2页,全部预览完了,如果喜欢就下载吧!
资源描述

《java练习册(010)--类(2)》由会员分享,可在线阅读,更多相关《java练习册(010)--类(2)(2页珍藏版)》请在金锄头文库上搜索。

1、java 课程实践指导书课程实践指导书文件编号:YM/WI-19 文件版本: A 版 文件页码:page of JAVAJAVA 篇篇JAVAJAVA 语言中的类(语言中的类(2 2)一、练习目标: 1. 类的声明以及属性,方法的定义。 2. 类,属性,方法的访问权限。 3. final 的使用。 4. this 的使用。 5. Static 的使用。 二、练习时间与作业要求: 本次练习一共需要 180 分钟。翻译练习写在 JAVA 笔记本上,书面作业写在 JAVA 家庭作业本上。后面的书面作业不包括在这个时间范围内。三、翻译练习: 将下面要求翻译的内容,写在记事本上ClassClass ac

2、cessaccessIn Java, the access specifiers can also be used to determine which classes within a library will be available to the users of that library. If you want a class to be available to a client programmer, you use the publicpublic keyword on the entire class definition. This controls whether the

3、 client programmer can even create an object of the class. To control the access of a class, the specifier must appear before the keyword classclass. Thus you can say:public class Widget Now if the name of your library is mylibmylib, any client programmer can access WidgetWidget by sayingimport myli

4、b.Widget;orimport mylib.*;However, theres an extra set of constraints: 1. There can be only one publicpublic class per compilation unit (file). The idea is that each compilation unit has a single public interface represented by that publicpublic class. It can have as many supporting java 课程实践指导书课程实践

5、指导书文件编号:YM/WI-19 文件版本: A 版 文件页码:page of package-access classes as you want. If you have more than one publicpublic class inside a compilation unit, the compiler will give you an error message. 2. The name of the publicpublic class must exactly match the name of the file containing the compilation un

6、it, including capitalization. So for WidgetWidget, the name of the file must be Widget.javaWidget.java, not widget.javawidget.java or WIDGET.javaWIDGET.java. Again, youll get a compile-time error if they dont agree. 3. It is possible, though not typical, to have a compilation unit with no publicpubl

7、ic class at all. In this case, you can name the file whatever you like. 四、上机练习: 1. 将圆周率定义为常量,编写一个求圆面积的方法,调用此方法来求圆的面积。 2. 定义一个学生类,包含三个属性(学号,姓名,成绩)均为私有的,分别给这三 个属性定义两个方法,一个设置它的值,另一个获得它的值。然后在一个测试类里 试着调用。 3、创建一个汽车类(Car),为其定义两个属性:颜色和型号,为该类创建两个构造 方法: 第一个为无形参的构造方法,利用其中方法将颜色和型号设置为红色、轿车 第二个为带参构造方法,利用构造方法将颜色和型

8、号设置为黑色、轿车,另外为 该类创建两个方法,分别用来显示颜色和型号。 4修改上次写的人类,使它有多个构造函数,可以用姓名构造一个人,也可以用姓名, 性别构造一个人。练习各种访问权限的访问范围.和 static 关键字的使用。 五、书面作业: 1、什么是构造函数?其书写格式是什么? 构造函数是一种特殊的对象方法,方法名和类名相同,没有返回值,声明的时候不能声 明成返回类型为 void ,这个函数只在创建对象的时候调用,用来初始化对象属性变量2、如果一个类不写构造函数,它有构造函数吗? 如果没有写这个构造函数,系统会自动产生一个默认的无参数的空构造函数.3、如果一个类中只有带参的构造函数,它有没有不带参数的构造函数吗? 可以定义不带参数的构造函数,可以同名,但不可同参数

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号