首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

面向对象的编程-Application 4

Previously on OOP:

In Calculation class, it is required to store two integer numbers as attributes. The value of these numbers can be either passed by constructor, or inserted in system standard input. Furthermore, the computation with these two numbers are addition and subtraction. From programming perspective, the most noticeable issue is that we have to use Scanner class to implement input from system standard input.

现在我们可以在Hello类中,调用Calculation类中已经编写好的methods。在理论部分我们曾经学过,一个类想要调用别的类中的方法,有两种方法:

(1)创建一个其他类的实例,然后用object reference + dotted notation + method name。

(2)在其他类的函数头中加入关键字“static”,使之成为静态函数。用class name + dotted notation + method name来调用。这种方法是非常不推荐的,理由是因为在Java Thread中讲过的multiple entities accessing the same resource may result in racing condition。

如果在调用method的前面没有dotted notation,这说明了这个method就定义在这个类中,而不是在其他类中。非要统一格式的话,在前面可以加上“this.”。

所以,我们在Hello类中要先新建一个Calculation类的实例,再调用函数,如下所示:

第一段代码,创建Calculation类的实例,object reference的名称是calObject;并且第一个数据是4,第二个数据是5。用calObject调用addition()函数,两个数据会相加,得到的结果是9;再调用subtraction()函数,两个数据相减,得到的结果是-1。

这是第二段代码,两个数据的值不再是constructor中的参数,而是用户从system standard input中输入的。接着会调用addition() and subtraction()函数把两个数据加加减减。

以上就是HelloWorld project中的所有代码。在这个非常简单的例子中,我们验证了Eclipse编译器的配置,学会了输出输入字符串,创建类的实例并且调用其中的函数。

下面,我们回归到FirstExample project,现在里面空空如也,没有任何的类被创建。第一步是要有一个main函数,否则程序会不知道该从哪一个函数开始运行。为了巩固一下学习成果,请各位亲们自己在default package中创建一个类,名字叫做First,功能是打印随便一个字符串。

参考答案如下:

在面向对象的开发中,除了“整个Workspace至少要有一个main函数”这个要求之外,每个project中一定要有且仅有一个“主要的类”,用英语来讲可以是main facade(源于法语,发音比较特别,必须重点记忆)/ the most important class / dominate class / etc。

在主要的类中,一般不包含main函数。它是最直接被main函数控制的那一个类,main函数中一定有创建这个类的实例。主要的类负责控制所有其他的类,或者说其他的类都是主要的类的辅助。从编码的角度上来讲,它里面会存放很多数据,使用不同的数据格式,考试题目的绝大部分程序都要在这个类中编写。

现在,让我们先暂且忘记default package and main facade这两个事情,因为both Hello project and FirstExample project并不是软件开发的项目,仅仅是例子。

我们再新建一个名字叫做“Person”的类。To observe good practice, it is recommended to separate this class into three parts: attributes, constructors, and methods, just as the previous example we made.

What attributes does a Person have? A social security number (identity number), first name and last name. Thus, the attributes of Person class incorporating these three fields is reasonable.如下所示:

上面这段程序中,声明了三个attributes,名称分别是:SSN,name, andsurname。然后constructor的代码会在Person实例被创建的时候给三个attributes赋值。

与C语言不同的是,Java中声明的变量不需要立即初始化,即写作SSN= NULL;name= NULL;surname= NULL;这是因为编译器比较友善,can automatically initiates all created variables。然而,C语言必须要初始化!初始化!初始化!重要的事情说三遍。否则存放的数据就不是NULL,而是什么都有可能,可能是乱码,可能是正确的,可能是错误的。

当然,C语言中的变量也不是都一定要初始化的,比如global variable。为了保险起见,还是全部初始化一遍为好。一个非常重要的问题就被引出了:什么是local variable?Global variable?这些变量和attributes又有什么关系呢?请在Person class的constructor中添加以下代码:

变量“i”声明在constructor中,而attributes全部都在Person class中,所以,i只在constructor中存在,而attributes在整个Person class都存在。When the execution of constructor is done, variable“i”is lost. Nevertheless, the value of attributes are preserved.我们把像i这样的,仅仅存在于变量声明的那一组大括号中的变量称作为local variable,会随着大括号程序段的执行的结束而结束。

第二,global variable指的是所有类都能访问到的变量。我们的SSN,name, andsurname显然不行,因为前面写的是“private”关键字,即仅能在Person类中存在。

第三,attributes是一个OOP的概念,是类的成员变变量。比local variable存在的范围稍微大一些。以上就是local variable, global variable and attributes的简单说明。

欲知Person class的methods,且听下回分解。

欢迎使用本黄鸭编写的小程序~

微信公众号二维码:

  • 发表于:
  • 原文链接https://kuaibao.qq.com/s/20180921G1GYFV00?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券