A small JAVA program
Methods (方法)
A method is a group of programming statements that is given a name.
即C语言中的的 function(函数)。
定义与调用:(与C语言一样, 但方法需定义在class里面)
Java is Object-Oriented Paradigm of Programming(OOP)
面向过程(procedure-oriented):当需要实现某一功能时,每个具体步骤都亲力亲为,详细处理每一个细节。
面向对象:当需要实现一个功能时,直接用已经具有的功能,而不用关心具体步骤。
对比
Three main concepts:
Encapsulation (封装) (object)
• provides security for data
• improves code maintenance
• code is easier to understand
Objects should be encapsulated: it protects and manages its own data
• Imagine if anyone could just go into your house and rearrange your furniture!
• Special methods are used to access and modify an object’s data
• They act as a lock – restricting access to the data stored within the object
• Inheritance (继承)(class)
• improve code sharing and re-usability
• hierarchical code organisation based on commonality
in your source code: same data and/or operations
Classes can be created from existing classes using inheritance
• This is software reuse, exploiting similarities among classes
• One class is used to derive several new classes – and those classes
can be used to derive even more classes
• In this way, we create a class hierarchy in which derived children
classes inherit attributes and methods from their parent classes
• Polymorphism means that you can write source code which computes
over multiple types of related objects.
• This leads to elegant and reusable solutions
• Polymorphism
• simplifies code understanding
• standardises method naming
类 class
一组相关属性和行为的集合。
属性:该事物的状态信息。
行为:该事物能做什么。
举例: 猫
属性:名字、体重、颜色等。
行为:跑、跳、叫。
对象 object
是类的一个实例(也具备该类事物的属性和行为)
类是抽象的描述,而对象是该类事物的实例
举例:
In the first example, we have printed out a string.
System is a class, and out is an obejct.
println is a method.
So..
Besides, we can also declare a string variable:
When we ouput the variables with string we should use the +.
The diference between the println & print:
String concatenation
![](https://i-blog.csdnimg.cn/blog_migrate/595376dc7954220f7c897c2a9817f6e1.png)
![](https://i-blog.csdnimg.cn/blog_migrate/b00927ddd8123517c2d760634dbd6655.png)
Concatenation vs addition operators
So when we want to output the variables with string or somthing, we should use the + .