栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

类&方法的结构

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

类&方法的结构

Class 类 Basic Class Structure

The inner contents of a class:

  • Fields
  • Constructors
  • Methods

The name of a class always starts with a capital letter.

If the name is a compound name, like ticket machine, it should be written as TicketMachine.

类名: 无空格, 大驼峰.

Fields 变量
  • Fields store values for an object. 变量为对象存储数据
  • They are also know as instance variables. 它也叫实例变量
  • Fields define the state of an object. 从实例变量中能够看出一个对象的属性
  • Some values change often. 有些数据并不固定
  • Some change rarely (or not at all). 有些数据比较固定
public class TicketMachine
{
    private int price;
    private int balance;
    private int total;
    
    // Further detailds omitted.
}

visibility modifier (private) + type (int) + variable name (price)

Constructors 构造器
public TicketMachine (int cost)
{
    price = cost;
    balance = 0;
    total = 0;
}
  • Initialise an object. 初始化一个对象
  • Have the same name as their class. 构造器的名字一定和类名一样
  • Have close association with the fields. 构造器与实例变量关系密切
    1. Initial values stored into the fields. 往下读你就知道了
    2. Parameter values often used for these. 你信我的, 你往下读

”=“ : assigns a value to variable

java 中的 “=” 与数学中的不同, 在 Java, “=” 为变量赋值, 并不表示相等的关系

Passing data via parameters

以 BlueJ 为例:

如果我在源代码中做了一些更改,最简单的,我在某空白处敲了下回车键

这时我再回来一看就会发现,类的显示上多了很多 “//”, 它表示 该类没有被编译. 意味着 不能运行.

其实,我们的电脑并不能够直接运行 Java code, 我们的电脑运行的是一种机器代码 (machine code). 可是人类并不会写, 也不认识 machine code, 我们的电脑也不认识 Java code, 怎么办呢?

compiler可以将Java code转换成machine code, 这个转换的过程叫做编译

当我们点击图中红圈内的Compile键时, compiler将Java code 编译成了machine code. 类中的斜杠就会消失, 这个类就可以运行了

注意:每次更改源代码都要 “compile” 一次

Parameters (形式参数) are another sort of variable.

左边的对话窗是在新建对象:

new TicketMachine(500)

(A): 输入数据500,数据被复制到class(类)的constructor(构造器)中的parameters(形式参数)里. (这些在引用构造器时会自动完成)

public TicketMachine(int cost)
{
    price = cost;
    balance = 0;
    total = 0;
}

(B): 看一下构造器的内部, 形式参数 cost (500)被存储到了变量 price中

对象中的变量(fields)是一个变量(variable), 它也叫 实例变量(instance variable)

构造器中的形式参数(parameters)又是一个变量(variable), 它也叫 剖解临时变量(temporary variable), 它只有在构造器被运行时才存在


当构造器运行结束时, 形式参数就会消失.

形式参数只在构造器运行的几秒钟存在, 当形式参数中的数据被存储到变量(fields)中, 它的使命就完成了!

Methods 方法
  • Methods implement the behaviour of objects. 方法就是对象的行为

比如说隔壁老王来打飞机了, "隔壁老王"是对象, "打飞机"就是老王的方法

  • Methods have consistent structure comprised of a header and a [body](#the body). 方法有固定的格式, 详细内容往下读
  • Accessor methods provide information about an object. 用访问器获取与对象有关的数据
  • Mutator methods alter the state of an object. 用更改器更新对象的属性
  • Other sorts of methods accomplish a variety of tasks. 略略略略略~~~
Method structure
public int getPrice()
{
    return price;
}
The header:
  • the header tells us: 上面这串代码告诉我们的信息有:
    1. the visibilities to objects of other classes; 对其它类的对象的可见性
    2. whether the method returns a result; 方法是否返回数据
    3. the name of the method; 方法名
    4. whether the method takes parameters. 方法有没有形式参数
The body
  • The body encloses the method’s statements. 大括号里的代码就是方法体
Accessor (get) methods 访问器

  • An accessor method always has a return type that is not void. 访问器返回的值不可能是空的
  • An accessor method returns a value (result) of the type given in the header. 访问器返回的数据类型是可以从header中看出来的
  • The method will contain a return statement to return the value. 看图, 那串代码就是用来返回值的
  • NB: Returning is not printing! 返回数据并不等于输出数据
// return price; 不等于 System.out.print(price);
// 如果现在看不懂没关系, 只要不断学习, 在不久的将来会明白的
Mutator methods 更改器

  • Have a similar method structure: header and body. 跟别的方法一样, 有头有脑袋

  • Used to mutate (e.g. change) an object’s state. 用于更改对象属性

  • Achieved through changing the value of one or more fields. 更改器就是用来更改实例变量的

    1. They typically contain one or more assignment statements.

      An assignment statement gives a value to a variable

    2. Often receive parameters. 总是接受形式参数

  • These have a simple, distinctive form: 更改器的格式

    • void return type 返回值为 “void”
    • method name related to the field name 更改器的方法名就是 set + 变量名
    • single formal parameter, with the same type as the type of the field更改器的形式参数就是实例变量, 类型也一样
    • a single assignment statement 简单的基础结构
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/345990.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号