http://www.javaworld.com/article/2979739/learn-java/java-101-classes-and-objects-in-java.html?page=3

class Book
{
// ... static int count;
}

This example declares a count integer field that stores the number of Book objects created. The declaration begins with the static keyword to indicate that there is only one copy of this field in memory. Each Book object can access this copy, and no object has its own copy. For this reason, count is known as a class field.

The previous fields were not assigned values. When you don't explicitly initialize a field, it's implicitly initialized with all of its bits set to zero. You interpret this default value as false (for boolean), '\u0000' (for char), 0 (for int), 0L (for long), 0.0F (for float), 0.0 (for double), or null (for a reference type).

class Book
{
// ... static void showCount()
{
System.out.println("count = " + count);
}
}

This example declares a showCount() method that will output the value of the count field. The declaration begins with the static keyword to indicate that this method belongs to the class and cannot access individual object state; no objects need to be created. For this reason, showCount() is known as a class method.

Method overloading

Java lets you declare methods with the same name but with different parameter lists in the same class. This feature is known as method overloading. When the compiler encounters a method-call expression, it compares the called method's comma-separated list of arguments with each overloaded method's parameter list as it looks for the correct method to call.

Two same-named methods are overloaded when their parameter lists differ in number or order of parameters. Alternatively, two same-named methods are overloaded when at least one parameter differs in type. For example, consider the following four draw() methods, which draw a shape or string at the current or specified draw position:

void draw(Shape shape)
{
// drawing code
} void draw(Shape shape, double x, double y)
{
// drawing code
} void draw(String string)
{
// drawing code
} void draw(String string, double x, double y)
{
// drawing code
}

When the compiler encounters draw("abc");, it will select the third method because it offers a matching parameter list. However, what will the compiler do when it encounters draw(null, 10, 20);? It will report a "reference to draw is ambiguous" error message because there are two methods from which to choose.

You cannot overload a method by changing only the return type. For example, you couldn't specify int add(int x, int y) and double add(int x, int y) because the compiler doesn't have enough information to distinguish between these methods when it encounters add(4, 5); in source code. The compiler would report a "redefinition" error.

Constructors: Initializing objects

As well as explicitly assigning values to fields, a class can declare one or more blocks of code for more extensive object initialization. Each code block is a constructor. Its declaration consists of a header followed by a brace-delimited body. The header consists of a class name (a constructor doesn't have its own name) followed by an optional parameter list:

className ( [parameterList] )
{
// constructor body
}

The following example declares a constructor in the Book class. The constructor initializes a Book object's title and pubYear fields to the arguments that were passed to the constructor's _title and _pubYear parameters when the object was created. The constructor also increments the count class field:

class Book
{
// ... Book(String _title, int _pubYear)
{
title = _title;
pubYear = _pubYear;
++count;
} // ...
}

The parameter names have leading underscores to prevent a problem with the assignments. For example, if you renamed _title to title and specified title = title;, you would have merely assigned the parameter's value to the parameter, which accomplishes nothing. However, you can avoid this problem by prefixing the field names with this.:

class Book
{
// ... Book(String title, int pubYear)
{
this.title = title;
this.pubYear = pubYear;
++count;
} // ... void setTitle(String title)
{
this.title = title;
} void setPubYear(int pubYear)
{
this.pubYear = pubYear;
} // ...
}

A parameter (or local variable) name that's identical to an instance field name shadows (meaning hides or masks) the field. Keyword this represents the current object (actually, its reference). Prepending this. to the field name removes the shadowing by accessing the field name instead of the same-named parameter.

Although you can initialize fields such as title and pubYear through the assignments shown above, it's preferable to perform the assignments via setter methods such as setTitle() and setPubYear(), as demonstrated below:

class Book
{
// ... Book(String title, int pubYear)
{
setTitle(title);
setPubYear(pubYear);
++count;
} // ...
}

Constructor calling

Classes can declare multiple constructors. For example, consider a Book constructor that accepts a title argument only and sets the publication year to -1 to indicate that the year of publication is unknown. This extra constructor along with the original constructor are shown below:

class Book
{
// ... Book(String title)
{
setTitle(title);
setPubYear(-1);
++count;
} Book(String title, int pubYear)
{
setTitle(title);
setPubYear(pubYear);
++count;
} // ...
}

But there is a problem with this new constructor: it duplicates code setTitle(title); located in the existing constructor. Duplicate code adds unnecessary bulk to the class. Java provides a way to avoid this duplication by offering this() syntax for having one constructor call another:

class Book
{
// ... Book(String title)
{
this(title, -1); // Do not include ++count; here because it already
// executes in the second constructor and would
// execute here after this() returns. You would end
// up with one extra book in the count.
} Book(String title, int pubYear)
{
setTitle(title);
setPubYear(pubYear);
++count;
} // ...
}

The first constructor uses keyword this followed by a bracketed argument list to call the second constructor. The single parameter value is passed unchanged as the first argument, and -1 is passed as the second argument. When using this(), remember that it must be the first piece of code in a constructor; otherwise, the compiler reports an error.

Objects: Working with class instances

Once you have declared a class, you can create objects from it. An object is nothing more than a class instance. For example, now that the Book class has been declared, you can create one or more Book objects. Accomplish this task by specifying the new operator followed by a Book constructor, as follows:

Book book = new Book("A Tale of Two Cities", 1859);

new loads Book into memory and then calls its constructor with arguments "A Tale of Two Cities" and 1859. The object is initialized to these values. When the constructor returns from its execution, new returns a reference (some kind of pointer to an object) to the newly initialized Book object. This reference is then assigned to the book variable.

Constructor return type

If you were wondering why a constructor doesn't have a return type, the answer is that there is no way to return a constructor value. After all, the new operator is already returning a reference to the newly-created object.

After creating a Book object, you can call its getTitle() and getPubYear() methods to return the instance field values. Also, you can call setTitle() and setPubYear() to set new values. In either case, you use the member access operator (.) with the Book reference to accomplish this task:

System.out.println(book.getTitle()); // Output: A Tale of Two Cities
System.out.println(book.getPubYear()); // Output: 1859
book.setTitle("Moby Dick");
book.setPubYear(1851);
System.out.println(book.getTitle()); // Output: Moby Dick
System.out.println(book.getPubYear()); // Output: 1851
Messaging objects

Calling a method on an object is equivalent to sending a message to the object. The name of the method and its arguments are conceptualized as a message that is being sent to the object on which the method is called.

You don't have to create any Book objects to call class methods. Instead, you prepend the class name and member access operator to the class method's name when calling these methods:

Book.showCount(); // Output: count = 1

Finally, it's possible to get and set the values of Book's instance and class fields. Use an object reference to access an instance field and a class name to access a class field:

System.out.println(book.title); // Output: Moby Dick
System.out.println(Book.count); // Output: 1
book.pubYear = 2015;
System.out.println(book.pubYear); // Output: 2015

I previously mentioned that instance methods affect only the objects on which they are called; they don't affect other objects. The following example reinforces this truth by creating two Book objects and then accessing each object's title, which is subsequently output:

Book book1 = new Book("A Tale of Two Cities", 1859);
Book book2 = new Book("Moby Dick", 1851);
Book book3 = new Book("Unknown");
System.out.println(book1.getTitle()); // Output: A Tale of Two Cities
System.out.println(book2.getTitle()); // Output: Moby Dick
System.out.println(book3.getPubYear()); // Output: -1
Book.showCount(); // Output: count = 3

Information hiding and access levels

A class's body is composed of interface and implementation. The interface is that part of the class that's accessible to code located outside of the class. The implementation is that part of the class that exists to support the interface. Implementation should be hidden from external code so that it can be changed to meet evolving requirements.

Consider the Book class. Constructor and method headers form this class's interface. The code within the constructors and methods, and the various fields are part of the implementation. There is no need to access these fields because they can be read or written via the getter and setter methods.

However, because no precautions have been taken, it's possible to directly access these fields. Using the previous book reference variable, you could specify book.title and book.pubYear, and that would be okay with the Java compiler. To prevent access to these fields (or at least determine who can access them), you need to take advantage of access levels.

An access level is an indicator of who can access a field, method, or constructor. Java supports four access levels: private, public, protected, and package (the default). Java provides three keywords that correspond to the first three access levels:

  1. private: Only code in the same class as the member can access the member.
  2. public: Any code in any class in any package can access the member.
  3. protected: Any code in the same class or its subclasses can access the member.

If there is no keyword then package access is implied. Package access is similar to public access in that code outside of the class can access the member. However, unlike public access, the code must be located in a class that belongs to the same package (discussed later in the Java 101 series) as the class containing the member that is to be accessed.

You can prevent external code from accessing Book's title and pubYear fields so that any attempt to access these fields from beyond Book will result in a compiler error message. Accomplish this task by prepending private to their declarations, as demonstrated below:

[REPRINT] Java 101: Classes and objects in Java的更多相关文章

  1. Java Date Classes

    References: [1] http://tutorials.jenkov.com/java-date-time/index.html [2] https://docs.oracle.com/ja ...

  2. Top 15 Java Utility Classes

    In Java, a utility class is a class that defines a set of methods that perform common functions. Thi ...

  3. Java Nested Classes(内部类~第一篇英文技术文档翻译)

    鄙人最近尝试着翻译了自己的第一篇英文技术文档.Java Nested Classes Reference From Oracle Documentation 目录 嵌套类-Nested Classes ...

  4. Linq to Objects for Java

    好几年不写博客了,人也慢慢变懒了.然而想写了却不知道写点啥,正好最近手头有点小项目就分享一下经历. 现在 java 的大环境下,基本都是围着 spring 转,加上一堆其他的库.有了架子就开始搞业务了 ...

  5. Java Virtual Machine (JVM) objects 虚拟机实例的产生 退出 两种线程

    Apache Spark is built around a distributed collection of immutable Java Virtual Machine (JVM) object ...

  6. Core Java Volume I — 1.2. The Java "White Paper" Buzzwords

    1.2. The Java "White Paper" BuzzwordsThe authors of Java have written an influential White ...

  7. 【Java】-NO.20.Exam.1.Java.1.001- 【1z0-807】- OCEA

    1.0.0 Summary Tittle:[Java]-NO.20.Exam.1.Java.1.001-[1z0-807] Style:EBook Series:Java Since:2017-10- ...

  8. 【Java】-NO.16.EBook.4.Java.1.007-【疯狂Java讲义第3版 李刚】- Java基础类

    1.0.0 Summary Tittle:[Java]-NO.16.EBook.4.Java.1.007-[疯狂Java讲义第3版 李刚]-  Java基础类 Style:EBook Series:J ...

  9. 20165230 《Java程序设计》实验二(Java面向对象程序设计)实验报告

    20165230 <Java程序设计>实验二(Java面向对象程序设计)实验报告 一.实验报告封面 课程:Java程序设计 班级:1652班 姓名:田坤烨 学号:20165230 成绩: ...

随机推荐

  1. java 时间戳转为时间

    Date date = new Date(Long.parseLong(String.valueOf("1560235259477")));SimpleDateFormat for ...

  2. 远程桌面连接,运维工程师-必备软件【MultiDesk】

    实习时,在本地一家大型女装公司做桌面运维,服务器碰得到少,大部分时间都是在维护同事的电脑桌面,什么360全家桶了,毒霸了,都是通过远程工具 teamviewer 去搞定的. 后来做了前端开发,免不了自 ...

  3. iview input实现默认获取焦点并选中文字

    1. 业务背景 配置页面,可新建和复制任务:当复制任务的时候,要把名字的input框默认获取焦点,并全选任务名.效果如下: 2. 代码实现 <template> <Form :mod ...

  4. 超全Altium Designer16 总结--Altium Designer

    原址:http://blog.csdn.net/qq_29350001/article/details/52199356 以前是使用DXP2004来画图的,后来转行.想来已经有一年半的时间没有画过了. ...

  5. 【Java】java获取json中某个字段

    import com.alibaba.fastjson.JSONObject; public class JsonTest { public static void main(String[] arg ...

  6. vue搭配的UI框架 pc端 + 移动端

    PC桌面端UI框架: 1,iview      (最新,用户评分高功能多炫酷 解决和避免了其他UI框架出现的一些小问题) 2, bootstrap  (使用用户最多样式死板没特色) 3,Element ...

  7. [杂题]:group(状压DP+轮廓线)

    题目描述 $pure$在玩一个战略类游戏.现在有一个士兵方阵,每行有若干士兵,每个士兵属于某个兵种.行的顺序不可改变,且每一行中士兵的顺序也不可改变.但由于每一行都有$C$个位置($C$不小于任一行的 ...

  8. [CSP-S模拟测试]:gcd(莫比乌斯反演)

    题目描述 有$n$个正整数$x_1\sim x_n$,初始时状态均为未选.有$m$个操作,每个操作给定一个编号$i$,将$x_i$的选取状态取反.每次操作后,你需要求出选取的数中有多少个互质的无序数对 ...

  9. 汇编指令ADD

    格式: ADD OPRD1,OPRD2 功能: 两数相加(不带进位) 例子: add ax,bx add ax,ax 解释:

  10. Windwos 08R2_DNS全面图文详解

    目录 目录 前言 软件环境 DNS域名服务器 DNS服务器原理 DNS域名空间 DNS区域 DNS服务器的类别 DNS查询模式 缓存文件 配置DNS服务器 DNS服务的应用 创建DNS正向解析区域 在 ...