Java 编程中实例的含义是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20461907/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 02:06:44  来源:igfitidea点击:

what is meaning of instance in programming?

javajavascriptc++

提问by user3057928

I don't get it for long. Any alternative word similar to 'instance' that is easier to understand? For non-programmer, how you explain instance? instance is like example in normal peoples' world. I can't understand what is it if I don't even got its meaning.

我不明白它很长时间。任何类似于“实例”的替代词更容易理解?对于非程序员,你如何解释实例?实例就像普通人世界中的例子。如果我什至不明白它的意思,我就无法理解它是什么。

采纳答案by broofa

"instance" is best understood as it relates to "class" in programming. "Classes" are used to define the properties and behavior of a categoryof things. E.g. A "Car" class might dictate that all cars be defined by their make, model, year, and mileage.

“实例”最好理解为它与编程中的“类”有关。“类”用于定义一事物的属性和行为。例如,“汽车”类可能会规定所有汽车都由它们的品牌、型号、年份和里程来定义。

But you cant provide specifics about a particular car (for example, that 1978 Chevy Impala with 205,000 miles on it that your uncle Mickey drives) until you create an "instance" of a Car. It's the instancethat captures the detailed information about one particular Car.

但是,在创建汽车的“实例”之前,您无法提供有关特定汽车的详细信息(例如,您的叔叔 Mickey 驾驶的 1978 年雪佛兰 Impala 行驶了 205,000 英里)。它是捕获有关一辆特定汽车的详细信息的实例

回答by iWumbo

An instance is basically an object. In actual english, it can mean differently. In this case instance in english can mean 'To Refer' or 'Reference'. These objects instances in programming are also a reference to the source code.

实例基本上是一个对象。在实际的英语中,它的含义可能有所不同。在这种情况下,英语中的实例可能意味着“参考”或“参考”。编程中的这些对象实例也是对源代码的引用。

回答by Willy2414

Here is a pretty standard definition:

这是一个非常标准的定义:

An instance, in object-oriented programming (OOP), is a specific realization of any object. An object may be varied in a number of ways. Each realized variation of that object is an instance. The creation of a realized instance is called instantiation.

Each time a program runs, it is an instance of that program. In languages that create objects from classes, an object is an instantiation of a class. That is, it is a member of a given class that has specified values rather than variables. In a non-programming context, you could think of "dog" as a class and your particular dog as an instance of that class.

在面向对象编程 (OOP) 中,实例是任何对象的特定实现。对象可以以多种方式变化。该对象的每个已实现的变体都是一个实例。已实现实例的创建称为实例化。

每次程序运行时,它都是该程序的一个实例。在从类创建对象的语言中,对象是类的实例化。也就是说,它是具有指定值而不是变量的给定类的成员。在非编程环境中,您可以将“狗”视为一个类,而您的特定狗则是该类的一个实例。

http://whatis.techtarget.com/definition/instance

http://whatis.techtarget.com/definition/instance

Here is a good conversation about instances that may help you out: https://softwareengineering.stackexchange.com/questions/99202/is-it-called-class-or-object-instance

以下是关于可能对您有所帮助的实例的精彩对话:https: //softwareengineering.stackexchange.com/questions/99202/is-it-called-class-or-object-instance

回答by JMercer

int main()
{
    int a;     //An instance of integer
    int a,b;   //two instances of integer
    struct1 a; //An instance of struct1
    return 0;
}

回答by Kentot

An object from an object or reference from an object.

来自对象的对象或来自对象的引用。

回答by Floris

Going outside the world of programming for a second: you know what people are. You are an "instance" of the class "people" - I can talk about people in general (the class of objects), or if I have a specific one in mind, I talk of an "instance". An instance can have properties that are not automatically a consequence of being a member of the class. All humans have a heart, but not all humans have your name and date of birth.

暂时离开编程世界:你知道人是什么。你是“人”类的“实例”——我可以谈论一般的人(对象类),或者如果我有一个特定的想法,我会谈论一个“实例”。实例可以具有并非作为类成员而自动产生的属性。所有人都有一颗心,但并非所有人都有你的名字和出生日期。

I hope that clears it up a bit?

我希望这能澄清一点?

回答by Pete Becker

Loosely speaking, there are patternsfor making things and there are instancesof those patterns.

粗略地说,有制作东西的模式,也有这些模式的实例

A class is a pattern for creating objects. The objects that are created with it are instances of the class.

类是用于创建对象的模式。用它创建的对象是类的实例。

class C { };
C c;          // instance of C
C d;          // instance of C

A function template is a pattern for creating functions. The functions that are created with it are instances of the template. This is usually done implicitly, and referred to as "implicit instantiation".

函数模板是用于创建函数的模式。用它创建的函数是模板的实例。这通常是隐式完成的,称为“隐式实例化”。

template <class T> void f(T) { }
f(int);       // implicit instantiation of f<int>
f(double);    // implicit instantiation of f<double>

A class template is a pattern for creating classes. The classes that are created with it are instances of the template.

类模板是用于创建类的模式。用它创建的类是模板的实例。

template <class T> class X { };
X<int> xi;    // X<int> is instance of X, xi is instance of X<int>
X<double> xd; // X<double> is instance of X, xd is instance of X<double>

回答by naruto

with a simple example : we have a blueprint (class) represents student (object) with fields like name, age, course (class member). And we have 2 students here, Foo and Bob. So, Foo and Bob is 2 different instances of the class (Student class) that represent object (Student people).
credit:Alfred's Computing Weblog

举一个简单的例子:我们有一个蓝图(班级)代表学生(对象),其中包含姓名、年龄、课程(班级成员)等字段。我们这里有 2 个学生,Foo 和 Bob。因此,Foo 和 Bob 是代表对象(学生人)的类(学生类)的 2 个不同实例。
信用:阿尔弗雷德的计算博客

as far i have understood instance a pointer to object of class.

据我所知,实例是指向类对象的指针。

ps: i could be wrong.

ps:我可能是错的。

回答by Aukins Moruri

To understand what an instance is, we must first understand what a class is.

要了解什么是实例,我们必须首先了解什么是类。

A classis simply a modeling tool provided by a programming language for use in representing real world objects in a program or application.

是通过简单编程语言用于表示一个程序或应用程序现实世界对象提供建模工具。

The class is structured to accommodate an object's properties(member variables) and its operations(member functions/methods).

该类的结构可以容纳对象的属性(成员变量)及其操作(成员函数/方法)。

An Instanceon the other hand is simply a variation of an object created from a class. You create an object variant (Instance) using a constructorwhich is a method within a class specifically defined for this purpose.

一个实例在另一方面是简单地从一个类创建的对象的变型。您可以使用构造函数创建对象变体 ( Instance),该构造函数是专门为此目的定义的类中的方法。

Consider a Car, if you wanted to represent it in your application you would define a class identified as Car which contains the properties of the car and the operations that the car can perform.

考虑一辆车,如果你想在你的应用程序中表示它,你可以定义一个标识为 Car 的类,它包含汽车的属性和汽车可以执行的操作。

It would look something close to this, supposing it was done in Java programming language:-

假设它是用 Java 编程语言完成的,它看起来与此类似:-

public class Car{
    //the properties of the car
    private String make;
    private int year;
    private int gear;
    private int speed;
    ...

    //constructor used to create instances of the car
    public Car(String carMake, int yearManf){
        year = yearManf;
        make = carMake;
    }

    //Car Operation/methods

    public void setGear(int gearValue){
        gear = gearValue
    }
    public void applyBrake(int decrement){
        speed -= decrement;
    }
    public void accelerate(int increment){
        speed += increment;
    }   
    ...
}

Create an instance of a car:-

创建汽车的实例:-

Car BMW = new Car("385 i", 2010);

BMW here is an instance of a car.

宝马在这里是汽车的一个实例。

回答by Trump

Instance is a variablethat holds the memory address of the Object.

Instance 是一个变量,用于保存 Object 的内存地址。