java程序的构成_Java对象构成所有Java应用程序的基础

java程序的构成

An object in Java — and any other "object-oriented" language — is the basic building block of all Java applications and represents any real-world object you might find around you: an apple, a cat, a car or a human.

Java对象(以及任何其他“面向对象” 语言 )是所有Java应用程序的基本组成部分,代表了您可能在周围发现的任何现实对象:苹果,猫,汽车或人。

The two characteristics that an object always has are state and behavior. Consider a person object. Its state might include hair color, sex, height, and weight, but also feelings of anger, frustration or love. Its behavior could include walking, sleeping, cooking, working, or anything else that a person might do.

对象始终具有的两个特征是状态行为 。 考虑一个人对象。 它的状态可能包括头发的颜色,性别,身高和体重,还包括愤怒,沮丧或爱的感觉。 它的行为可能包括散步,睡觉,做饭,工作或其他任何人可能做的事情。

Objects form the very core of any object-oriented programming language.

对象构成任何面向对象编程语言的核心。

什么是面向对象编程? ( What is Object Oriented Programming? )

Hundreds of books have been written to describe the intricacies of object-oriented programming, but basically, OOP is based on a holistic approach emphasizing re-use and inheritance, which streamlines development time. More traditional procedural languages, such as Fortran, COBOL, and C, take a top-down approach, breaking down the task or problem into a logical, orderly series of functions.

已经写了数百本书来描述面向对象编程的复杂性,但是从根本上说,OOP基于强调重用和继承的整体方法,从而缩短了开发时间。 更传统的过程语言,例如Fortran,COBOL和C,采用自上而下的方法,将任务或问题分解为逻辑,有序的一系列功能。

For example, consider a simple ATM application used by a bank. Before writing any code, a Java developer first will create a roadmap or plan on how to proceed, usually beginning with a list of all the objects that need to be created and how they will interact. Developers may use a class diagram to clarify the relationships between objects. Objects required for use in an ATM transaction might be Money, Card, Balance, Receipt, Withdrawal, Deposit and so on. These objects need to work together to complete the transaction: making a deposit should result in a balance report and perhaps a receipt, for instance. Objects will pass messages between them in order to get things done.

例如,考虑银行使用的一个简单的ATM应用程序。 在编写任何代码之前, Java开发人员将首先创建路线图或计划如何进行的,通常从需要创建的所有对象及其交互方式的列表开始。 开发人员可以使用类图来阐明对象之间的关系。 在ATM交易中需要使用的对象可能是货币,卡,余额,收据,取款,存款等。 这些对象需要共同努力才能完成交易:进行存款应产生余额报告,例如收据。 对象将在它们之间传递消息以完成任务。

对象和类 ( Objects and Classes )

An object is an instance of a class: here is the crux of object-oriented programming and the idea of re-use. Before an object can exist, a class on which it can be based must exist. 

对象是类的实例:这是面向对象编程的关键和重用的思想。 在对象可以存在之前,必须存在一个可以作为对象基础的类。

Perhaps we want a book object: to be precise, we want the book The Hitchhiker's Guide to the Galaxy. We first need to create a class Book. This class could be the basis for any book in the world.

也许我们想要一本书的对象:确切地说,我们想要一本书《银河旅行者指南》 。 我们首先需要创建一个类Book。 此类可能是世界上任何书籍的基础。

It might look something like this:

它可能看起来像这样:


public class Book {
String title;
  public int setAuthor()
{
return author;
}

The class Book has a title and an author with methods that allow you to set or get either of these items (it would have more elements as well, but this example is just an excerpt). But this is not yet an object — a Java application can't yet do anything with it. It needs to be instantiated to become an object that can be used. 

Creating an Object

The relationship between an object and a class is such that many objects can be created using one class. Each object has its own data but its underlying structure (i.e., the type of data it stores and its behaviors) are defined by the class.

We can create several objects from a book class. Each object is called an instance of the class.

Book HitchHiker = new Book("The HitchHiker's Guide to the Galaxy", "Douglas Adams");Book ShortHistory = new Book("A Short History of Nearly Everything", "Bill Bryson");Book IceStation = new Book("Ice Station Zebra", "Alistair MacLean");

These three objects can now be used: they can be read, purchased, borrowed or shared. 

翻译自: https://www.thoughtco.com/object-2034254

java程序的构成

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值